10-C# Forms Interaction
10-C# Forms Interaction
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();
}
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();
}
public Form2()
{
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();