Easily master data interactions between Windows Forms in. NET (ii)

Source: Internet
Author: User
Tags foreach constructor modifier trim
window| Interactive | data

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:

private void Buttonedit_click (object sender, System.EventArgs e)

{

Form2 formchild = new Form2 ();

Formchild.owner = this;

Formchild.showdialog ();

This.listBoxFrm1.DataSource = null;

This.listBoxFrm1.DataSource = this.listdata1;

}

We set the Formchild.owner to this so that the subform and the main form are connected,

Of course, we can also change the following:

private void Buttonedit_click (object sender, System.EventArgs e)

{

Form2 formchild = new Form2 ();

Formchild.showdialog (this);

This.listBoxFrm1.DataSource = null;

This.listBoxFrm1.DataSource = this.listdata1;

}

But this does not work, the current main form of the LISTDATA1 variable outside access,

Private ArrayList listData1;

Must be modified to public access modifier,

Public ArrayList listData1;

can also be implemented by attributes.

Public ArrayList ListData1

{

Get{return this.listdata1;}

}

Here I use attributes, feel the grammar more flexible, clear.

Here's a change to the Form2,

The constructor is restored to its original appearance.

Public Form2 ()

{

InitializeComponent ();

}

In addition, a new form's Load event is added to its event handler function to get the data from the main form.

private void Form2_load (object sender, System.EventArgs e)

{

Form1 pareform = (Form1) this. Owner;

This.listdata2 = pareform.listdata1;

foreach (Object o in This.listdata2)

THIS.LISTBOXFRM2.ITEMS.ADD (o);

}

One would ask, why not put the above code in the constructor? The following is not better,

Public Form2 ()

{

InitializeComponent ();

Form1 pareform = (Form1) this. Owner;

This.listdata2 = pareform.listdata1;

foreach (Object o in This.listdata2)

THIS.LISTBOXFRM2.ITEMS.ADD (o);

}

Then I'll be wrong with you, because when the main form modifier button is clicked, start executing

Form2 formchild = new Form2 ();

In the case of Form2, execution in the constructor

Form1 pareform = (Form1) this. Owner;

And then the this. Owner is not a value, is a null reference, then the following code must also be problematic,

This.listdata2 = pareform.listdata1;

foreach (Object o in This.listdata2)

THIS.LISTBOXFRM2.ITEMS.ADD (o);

When the entire FORM2 instantiation is complete, the

Formchild.owner = this;

This code, so the Form2_load event is used.

How can you not use the Form2_load event? Wait for the following to modify the code to implement it.

The following subform code does not change,

private void Buttonadd_click (object sender, System.EventArgs e)

{

if (This.textBoxAdd.Text.Trim (). LENGTH>0)

{

THIS.LISTDATA2.ADD (This.textBoxAdd.Text.Trim ());

THIS.LISTBOXFRM2.ITEMS.ADD (This.textBoxAdd.Text.Trim ());

}

Else

MessageBox.Show ("Please enter the added content!");

}

private void Buttondel_click (object sender, System.EventArgs e)

{

int index = This.listBoxFrm2.SelectedIndex;

if (index!=-1)

{

This.listData2.RemoveAt (index);

This.listBoxFrm2.Items.RemoveAt (index);

}

Else

MessageBox.Show ("Please select Delete item!");

}

private void Buttonok_click (object sender, System.EventArgs e)

{

This. Close ();

}

All right, the result is the same as in the first one, the sub-window physically modifies the value of the main form.

2. Using custom properties or methods

Let's talk about how to use custom properties or methods to do data modification without using the Form2_load event.

The main form's Modify button clicks the processing function as follows:

private void Buttonedit_click (object sender, System.EventArgs e)

{

Form2 formchild = new Form2 ();

Formchild.listdata2 = this.listdata1;

Formchild.showdialog ();

This.listBoxFrm1.DataSource = null;

This.listBoxFrm1.DataSource = this.listdata1;

}

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.




Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.