0% found this document useful (0 votes)
53 views22 pages

10-C# Forms Interaction

The document discusses passing data between forms in a Windows Forms application. It describes two main ways to pass data: using properties and using get/set methods. Properties are best for simple data types, while get/set methods are better for complex types that require processing. The document provides examples of creating properties and methods in one form to set and get data, and then calling those properties/methods from another form to pass the data between them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views22 pages

10-C# Forms Interaction

The document discusses passing data between forms in a Windows Forms application. It describes two main ways to pass data: using properties and using get/set methods. Properties are best for simple data types, while get/set methods are better for complex types that require processing. The document provides examples of creating properties and methods in one form to set and get data, and then calling those properties/methods from another form to pass the data between them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Forms

Interaction
MIS 21 – Introduction to Applications Development
Creating a New Form
 Right
click the project in the Solution
Explorer
 Click Add > Windows Form
 Enter a name for the form and click
Add
Displaying Forms
 Need to create an instance of the new
form inside the active form
 To display the form, use the methods:
 Show()
 ShowDialog()
Displaying Forms
 Show()
 Non-Modal
 Users can switch between the 2 forms

 ShowDialog()
 Modal
 Users should interact with the new form before
returning to the old form
 Preferred because it forces the user to complete the
new form
Displaying Forms
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void showFormBtn_Click(object sender, EventArgs e)


{
Form2 a = new Form2();
a.ShowDialog();
}
}
Passing Data Between Forms
 Two ways to pass data
 Properties
 Use for simple data types (strings, int, etc)
 No processing of data
 Get/Set methods
 Use for complex data types (arrays, classes)
 Usually has processing of data involved
 e.g. looping through the array
Passing Data Between Forms
 Two directions
 Parent Form  Child Form
 Child Form  Parent Form
Passing Data
Using Properties
Form1  Form2
Create a Property in Form2
public partial class Form2 : Form
{
public string MyName {
get {
return nameLabel.Text;
}
set {
nameLabel.Text = value;
}
}

public Form2()
{
Create a Property in Form2
A property allows you to get and set
the value of a variable
 In
the previous example, we use the
nameLabel's text as the value of the
MyName
Set the Property's Value in Form1
private void showFormBtn_Click(object sender, EventArgs e)
{
Form2 a = new Form2();
a.MyName = form1TextBox.Text;
a.ShowDialog();
}

Always set the property before calling Show() or


ShowDialog()
Assigns the text to MyName which assigns it to
nameLabel
Form2  Form1
Create a Property for the Value to be Passed

public partial class Form2 : Form


{
public String MyFruit {
get {
return form2TextBox.Text;
}
}

public Form2()
{

No need for set { }. We only need to read form2TexBox


Add a Close button for Form2
private void showFormBtn_Click(object sender, EventArgs e)
{
Close();
}

Allows the user to return to Form1


Display the value in Form1 using the Property

private void showFormBtn_Click(object sender, EventArgs e)


{
Form2 a = new Form2();
a.MyName = form1TextBox.Text;
a.ShowDialog();
fruitLabel.Text = a.MyFruit;
}

Place after ShowDialog()


C# will stop at ShowDialog() until Form2 is closed. It will
then continue with the code below it
Other Tips
private void showFormBtn_Click(object sender, EventArgs e)
{
Form2 a = new Form2();
a.MyName = form1TextBox.Text;
Hide();
a.ShowDialog();
Show();
fruitLabel.Text = a.MyFruit;
}

Use Hide() to conceal Form1 and call Show() after


a.ShowDialog() to reveal it
Passing Data
Using Get/Set
Methods
Passing Data Using Get/Set Methods

 Processis same with Properties


except we create Methods
 Define the get/set methods in Form2
Create a set Method
public void setListBox(int[] nums)
{
listBox1.Items.Clear();
foreach (int x in nums)
{
listBox1.Items.Add(x);
}
}
Create a get Method
public int[] getListBoxContents()
{
int numItems = listBox1.Items.Count;
int[] a = new int[numItems];

for (int i = 0; i < numItems; i++)


{
a[i] = int.Parse(listBox1.Items[i].ToString());
}

return a;
}
Using the get/set Methods
private void showFormBtn_Click(object sender, EventArgs e)
{
Form2 a = new Form2();
int[] numsFromForm1 = { 1, 2, 3, 4, 5 };
a.setListBox(numsFromForm1);
a.ShowDialog();

int[] numsFromForm2 = a.getListBoxContents();


}

You might also like