In application development, it is often necessary to communicate data between multiple forms, write a few examples, and summarize several commonly used communication methods:
The main form Form1 is a ListBox, when you click to select a column, two controls pop up in the form Form2,form2, one is a textbox, the text of the selected column is displayed, the other is a button, the modified value is returned when clicked, and the text of the corresponding column is modified in Form1. The Form2 is closed at the same time.
Method One: Pass the value
First thought of, the Form2 constructor receives a string type argument, that is, the text of the selected row in Form1, sets the Form2 TextBox control's text to the string, which completes the Form1 to Form2. When the Form2 acceptchange button is pressed, you need to modify the value of the corresponding column in the listbox in the Form1, so you can consider that the ListBox control in the Form1 is also passed in Form2, all the modifications are done in Form2, according to this idea, The FORM2 code is as follows:
- Publicpartial class Form2:form
- {
- private string text;
- private ListBox lb;
- private int index;
- //constructor receives three parameters: Select row text, ListBox control, select Row index
- Public Form2 (string text,listbox lb,int index)
- {
- this.text = text;
- this.lb = lb;
- This.index = index;
- InitializeComponent ();
- this.textBox1.Text = Text;
- }
- private void Btnchange_click (object sender, EventArgs e)
- {
- string text = This.textBox1.Text;
- this.lb.Items.RemoveAt (index);
- This.lb.Items.Insert (index, text);
- This . Close ();
- }
- }
Form1 in new Form 2 o'clock in this case:
- Public partial class Form1:form
- {
- int index = 0;
- string text = null;
- Public Form1 ()
- {
- InitializeComponent ();
- }
- private void listbox1_selectedindexchanged (object sender, Eventargse)
- {
- if (this.listBox1.SelectedItem! = null)
- {
- Text = this.listBox1.SelectedItem.ToString ();
- index = This.listBox1.SelectedIndex;
- //Construction Form2 simultaneous transfer of parameters
- Form2 Form2 = new Form2 (text, listBox1, index);
- Form2. ShowDialog ();
- }
- }
OK, method One solution is this, the advantage is intuitive, what is needed to pass what, the shortcomings are also obvious, if the form 1 needs to modify the 100 control, is the structure of the time to pass 100 parameters in? Moreover, if other forms still need to play Form2, then the FORM2 will be discarded, only for use in Form 1, unless the overloaded constructor is written, not conducive to the reuse of code, continue to see the next method.
Method Two: Inherit this method I tried many times, inheritance can do, but trouble does not say, is not convenient, so personally think that if in order to manipulate the data and the use of inheritance, is not appropriate, but since it is a method, throw out to see, the actual role of ≈0. Form2:
- Declaration Form2 inherited from Form1
- public partial Classform2:form1
- {
- Publicint index;
- Public ListBox lb;
- Public Form2 (string text)
- {
- //Set inherited listbox to invisible
- this.listbox1.visible=false;
- InitializeComponent ();
- this.textBox1.Text = Text;
- }
- private void Btnchange_click (object sender, EventArgs e)
- {
- string text = This.textBox1.Text;
- this.lb.Items.RemoveAt (index);
- This.lb.Items.Insert (Index,text);
- This . Close ();
- }
- }
Form1:
- Public partial class Form1:form
- {
- public int index = 0;
- public String text = null;
- Public Form1 ()
- {
- InitializeComponent ();
- }
- private void listbox1_selectedindexchanged (object sender, Eventargse)
- {
- if (this.listBox1.SelectedItem! = null)
- {
- Text = this.listBox1.SelectedItem.ToString ();
- index = This.listBox1.SelectedIndex;
- Form2 Form2 = new Form2 (text);
- //After constructing the Form2, assign values to the parameters in the Form2
- form2.lb =This.listbox1;
- Form2.index = index;
- Form2. Show ();
- }
- }
- }
Here are a few questions to note, which method of assignment is required for each attribute in Form2? From Java too much to know, Java inheritance in the subclass of the use of the keyword super can access the base class public methods and parameters, and C # in the super replaced by base, that means we can be in the FORM2 so that parameter assignment?
- this.lb=Base.listbox1;
- this.index=Base.index;
OK, the second way of writing no problem, you can save the index value, but to the ListBox control, so the assignment will be problematic, through the test I found that Base.listbox1 point, is the subclass inherits the ListBox1 object, not the base class own ListBox1 object. So we guessed that the Base.index value is also the index of the sub-class? Test the discovery is true, so this.index=base.index is equal to not write, remove the same can be used, because the index is inherited by Form2, so we can understand that C # in the form of inheritance, through the base. Control cannot manipulate the base class control. Method Three: Event callback since C # has an event this thing, why not, and the event in the form of communication, has a more convenient role, we know that the event is actually the capture of the state, in the end I will give an example of capturing state, first look at the example of data manipulation. Form2:
- Defines a delegate that requires a string type argument
- Publicdelegate void MyDelegate (string text);
- Public partial class Form2:form1
- {
- //Define events for this delegate
- public event MyDelegate MyEvent;
- Public Form2 (string text)
- {
- InitializeComponent ();
- this.textBox1.Text = Text;
- }
- private void Btnchange_click (object sender, EventArgs e)
- {
- //Trigger event and return the modified text
- MyEvent (this.textBox1.Text);
- This . Close ();
- }
- }
Form1:
- Public partial class Form1:form
- {
- public int index = 0;
- public String text = null;
- Public Form1 ()
- {
- InitializeComponent ();
- }
- private void listbox1_selectedindexchanged (object sender, Eventargse)
- {
- if (this.listBox1.SelectedItem! = null)
- {
- Text = this.listBox1.SelectedItem.ToString ();
- index = This.listBox1.SelectedIndex;
- Form2 Form2 = new Form2 (text);
- //Register the MyEvent event for the Form2_myevent method
- Form2. MyEvent + = new MyDelegate (form2_myevent);
- Form2. Show ();
- }
- }
- //Processing
- void Form2_myevent (string text)
- {
- this.listBox1.Items.RemoveAt (index);
- This.listBox1.Items.Insert (index, text);
- }
- }
As you can see, it is convenient to use events, and there is no need to pass so many parameters, no inheritance, and improved code reuse, so it is recommended to do so under general requirements.
Several processing methods of communication between C # forms