Easily master data interactions between Windows Forms in. NET (ii)
Zhzuo (Autumn Maple)
Easily mastering the data interaction between Windows Forms in. NET (a) We talked about using a constructor with parameters to implement data transfer between forms, which I think is a bit more, and let's look at two other implementations.
Two Add a property or method to a form
1. Use the Owner property of the form class
Gets or sets the form that owns this form. To make a form owned by another form, assign a reference to its Owner property to the form that will be the owner. When a form is owned by another form, it is minimized and closed with the owner form. For example, if Form2 is owned by a form FORM1, closing or minimizing Form1 will also turn off or minimize Form2. And the attached form never appears behind its owner form. You can use satellite forms to find and replace windows such as Windows, which should not disappear when the owner form is selected. To determine the form owned by a parent form, use the Ownedforms property.
It's on the SDK help document, and we'll use it here.
First, use the second example in the first article, and the form is as follows:
Description: In this example, our two forms add a ListBox to display the contents of the ArrayList.
Controls in the main form: Listboxfrm1,buttonedit;
Controls in a subform: Listboxfrm2,textboxadd,buttonadd,buttonedit,buttonok.
The main form is also defined as a class data member,
Private ArrayList listData1;
Instantiate it in the constructor, populate the data, and finally bind to ListBoxFrm1.
The constructor functions are as follows:
Public Form1 ()
{
InitializeComponent ();
This.listdata1 = new ArrayList ();
THIS.LISTDATA1.ADD ("dotnet");
This.listData1.Add ("C #");
This.listData1.Add ("asp.net");
THIS.LISTDATA1.ADD ("WebService");
THIS.LISTDATA1.ADD ("XML");
This.listBoxFrm1.DataSource = this.listdata1;
}
The Modify button handler function for the main form:
And we removed the ListData1 property of the main form,
Public ArrayList ListData1
//{
Get{return this.listdata1;}
//}
Instead of adding the ListData2 property to the subform,
Public ArrayList ListData2
{
Set
{
This.listdata2 = value;
foreach (Object o in This.listdata2)
THIS.LISTBOXFRM2.ITEMS.ADD (o);
}
}
You can also change the property to a method,
public void Setlistdata (ArrayList listdata)
{
This.listdata2 = Listdata;
foreach (Object o in This.listdata2)
THIS.LISTBOXFRM2.ITEMS.ADD (o);
}
The Modify button processing function in the main form should also be changed accordingly:
Formchild.listdata2 = this.listdata1;
To
Formchild.setlistdata (THIS.LISTDATA1);
To sum up, we build a bridge between master and slave forms by using the owner attribute of form class, which is similar to the function of passing the main form as the constructor parameter of the subform to the implementation, and using the properties and methods to complete the data interaction, which I think is very practical. In particular, it is used to pass data without instantiating a class or having an instance already in it. In the next article, we'll talk about how to use static classes to do data interaction.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.