window| Interaction | Data is easy to master in. NET data interaction between Windows Forms (i)
Zhzuo (Autumn Maple)
Windows Forms is a new platform based on the. NET Framework for Microsoft Windows application development. This framework provides a structured, object-oriented, extensible set of classes that allows you to develop rich Windows applications. A Windows form represents an instance of the System.Windows.Forms.Form class in the. NET architecture.
The C # Classification under the CSDN Technology Forum. NET Plate often see someone ask how to pass data between two forms, access to modify the values in the other form. It's not a profound thing for an experienced programmer, and for beginners, these basic things are often a problem, and there is such a phenomenon, often more complex things they will, to use what to learn what, in fact, did not really understand to grasp it, the foundation is not solid, So there is a desire to write through their own experience in the form of programming, writing some of this article, to learn. NET friend reference, also take this opportunity to communicate with friends, write unreasonable place please friends to give valuable advice, below I divided three parts.
A Using a constructor with parameters
The first thing we need to do is to create two new forms, the following is the layout of the two forms, very simple:
< First example >
Description: Form1 The main form, including controls: text box TextBoxFrm1, multiple marquee checkBoxFrm1 and button buttonedit;
Form2 is a subform that contains controls: text box TextBoxFrm2, multiple marquee checkBoxFrm2, and button buttonok,buttoncancel.
When we create a new form, the designer generates the default constructor:
Public Form2 ()
{
InitializeComponent ();
}
It does not take parameters, since we want to Form1 some of the data in the Form2, why not in the Form2 constructor to make a fuss?
Let's say we want to implement the value of the text box in Form2 to display the TextBoxFrm1 in Form1, and modify the constructor of the subform:
Public Form2 (String text)
{
InitializeComponent ();
This.textBoxFrm2.Text = Text;
}
Add the Change button click event in Form1, the handler function is as follows:
Form2 formchild = new Form2 (this.textBoxFrm1.Text);
Formchild.show ();
}
We passed the This.textBoxFrm1.Text as a parameter to the subform constructor, opened in a modeless mode, so that the open Formchild text box shows the "main form" text, is not very simple, next we pass a Boolean data to the subform.
Public Form2 (String Text,bool checkedvalue)
{
InitializeComponent ();
This.textBoxFrm2.Text = Text;
this.checkBoxFrm2.Checked = CheckedValue;
}
In the main form of the Modify button click Processing, I adopted the mode of opening the window, in fact, in this example does not see any difference,
Form2 formchild = new Form2 (this.textboxfrm1.text,this.checkboxfrm1.checked);
Formchild.showdialog ();
}
The results are predictable, but there is a clear shortage of data that cannot be passed to the main form when the subform is modified, which means the main form is unaffected by the subform. And in the actual development process, we often use the subform to modify the data in the main form, how to solve it?
There are two types, value types, and reference types in. Net. Value types are inherited from ValueType, and ValueType are inherited from object, and for reference types it inherits the object type directly. Let's see how to modify the data in the Form1 by Form2.
Or let's modify the Form2 code.
Private TextBox TextBoxFrm12;
Private CheckBox CheckBoxFrm12;
Public Form2 (TextBox heckbo,checkbox heckbox)
{
InitializeComponent ();
This.textBoxFrm2.Text = Heckbo. Text;
this.checkBoxFrm2.Checked = Heckbox. Checked;
THIS.TEXTBOXFRM12 = HECKBO;
THIS.CHECKBOXFRM12 = Heckbox;
}
Now we've passed two reference types of data: TextBox type, and checkbox, plus two class data members TEXTBOXFRM12, CHECKBOXFRM12 used to save the variables from the constructor, respectively, in the Form2. But they do not belong to the Form2 controls container. Modify Form2 's OK button click event Function:
The code above we have made the changes to TextBoxFrm1 and CheckBoxFrm2 in the main form by assigning TextBoxFrm2 text and checkboxfrm2.checked to TEXTBOXFRM12 and CheckBoxFrm12, because the textbox Frm1 and TEXTBOXFRM12 are the same references, and CheckBoxFrm2 and CHECKBOXFRM12 are.
So far the functionality is implemented, but it's not always reasonable to have two form controls passed, and now I give a proper example.
Two forms were modified:
< second example >
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.
This time we use ArrayList to pass data and define class data members in FORM1:
Private ArrayList listData1;
Added memory allocations to listData1 in the constructor, and data is eventually bound to ListBoxFrm1,
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;
}
In addition, modify the Click event handler function to modify the button as follows:
Compiles the program, modifies the data in the subform, and when it closes, the main form displays the updated data.
Here's a bit to remind you, comparing two examples, we all pass the reference type, one is string, the other is ArrayList, why can't the string type modify the main form's data? In fact. NET does not modify the original value, the original value does not change, but instead regenerates a new string, the following is a good description.
public class Zzconsole
{
[STAThread]
static void Main (string[] args)
{
String str1 = "abc";
String str2 = str1;
STR1 = "123";
Console.WriteLine (STR1);
Console.WriteLine ("--------------");
Console.WriteLine (STR2);
Console.WriteLine ("--------------");
ArrayList al1 = new ArrayList ();
Al1. ADD ("abc");
ArrayList Al2 = al1;
Al2. ADD ("123");
foreach (Object o in Al1)
Console.WriteLine ((String) o);
Console.WriteLine ("--------------");
foreach (Object o in Al2)
Console.WriteLine ((String) o);
Console.ReadLine ();
}
}
Run a look at the output to see that, in addition to the value type of data operations to use the REF keyword.
To sum up, we implemented the data interaction between forms through the constructor with parameters, the code seems to be more clear, in the actual development process, you can take dataset,datatable, or DataView as a parameter, of course, if just want to modify a row, Can pass a DataRow or DataRowView. In the following article we look at how to use the other two methods to achieve 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.