Summary Several methods of communication between C # form

Application development, often need to communicate data between multi-form, write a few examples to summarize several common means of communication:
Form1 is the main form a ListBox, when you click to select a column, the pop-up window Form2, Form2 two controls, one TextBox, select the text of the column, and the other is a button, the value will be modified when clicked return, and modify the text column to Form1, Form2 closed simultaneously.
Between C # form a communication method: by value
First thought, Form2 constructor receives a string parameter type, i.e. Form1 selected rows of text, the Text Form2 TextBox control is set for the string, to complete the transfer to the value of Form2 Form1. When Form2's AcceptChange button is pressed, you need to modify the value of Form1 ListBox in the corresponding column, so you can consider while Form1 in the ListBox control when parameters are passed Form2, all work is done in Form2 modifications in accordance with this idea, Form2 code show as below:
 
  
  1. publicpartial class Form2 : Form     
  2.     {     
  3.         private string text;     
  4.         private ListBox lb;     
  5.         private int index;     
  6.     
  7.        // constructor accepts three parameters: the selected lines of text, ListBox controls, select the row index     
  8.         public Form2(string text,ListBox lb,int index)     
  9.         {     
  10.             this.text = text;     
  11.             this.lb = lb;     
  12.             this.index = index;     
  13.             InitializeComponent();     
  14.             this.textBox1.Text = text;     
  15.         }     
  16.     
  17.         private void btnChange_Click(object sender, EventArgs e)     
  18.         {                
  19.     
  20.             string text = this.textBox1.Text;     
  21.             this.lb.Items.RemoveAt(index);     
  22.             this.lb.Items.Insert(index, text);     
  23.             this.Close();     
  24.         }     
  25.     }   
Form1 in new window 2:00 write:
 
  
  1. public partial class Form1 :Form     
  2.     {     
  3.         int index = 0;     
  4.         string text = null;     
  5.         public Form1()     
  6.         {     
  7.             InitializeComponent();     
  8.         }     
  9.     
  10.         private void listBox1_SelectedIndexChanged(object sender, EventArgse)     
  11.         {     
  12.             if (this.listBox1.SelectedItem != null)     
  13.             {     
  14.                 text = this.listBox1.SelectedItem.ToString();     
  15.                 index = this.listBox1.SelectedIndex;     
  16.     
  17.                // constructor while passing parameters Form2     
  18.                 Form2 form2 = new Form2(text, listBox1, index);     
  19.                 form2.ShowDialog();     
  20.             }     
  21.         }   
OK, a method of solution is the way benefits are intuitive, what you need to pass, shortcomings are obvious, if you need to modify the Form 1 is one hundred controls, are constructed when it passed 100 parameters into account? Moreover, if other forms still need to play the Form2, Form2 that can scrap it, only for the use of Form 1, unless the write overloaded constructor is not conducive to code reuse, continue to look one way.
Communication between C # form Method Two: Inheritance
This way I tried many times, inheritance can indeed do, but trouble is not that is not convenient, so personally I think that if the operating data for use inherit each other, is not appropriate, but since it is a way to throw out a look, The actual effect ≈0.
Form2: 
 
  
  1. // Declare inherited from Form1 Form2     
  2.     
  3. public partial classForm2 : Form1     
  4.     {     
  5.          publicint index;     
  6.     
  7.         public ListBox lb;     
  8.         public Form2(string text)     
  9.         {       
  10.     
  11.            // will inherit over listBox set to invisible     
  12.     
  13.             this.listBox1.Visible=false;     
  14.             InitializeComponent();     
  15.             this.textBox1.Text = text;     
  16.         }     
  17.         private void btnChange_Click(object sender, EventArgs e)     
  18.         {     
  19.             string text = this.textBox1.Text;     
  20.             this.lb.Items.RemoveAt(index);     
  21.             this.lb.Items.Insert(index,text);              
  22.             this.Close();     
  23.     
  24.         }     
  25.     }   
Form1:
 
  
  1. public partial class Form1 :Form     
  2.     {     
  3.         public int index = 0;     
  4.         public string text = null;     
  5.         public Form1()     
  6.         {     
  7.             InitializeComponent();     
  8.         }     
  9.     
  10.         private void listBox1_SelectedIndexChanged(object sender, EventArgse)     
  11.         {     
  12.             if (this.listBox1.SelectedItem != null)     
  13.             {     
  14.                 text = this.listBox1.SelectedItem.ToString();     
  15.                 index = this.listBox1.SelectedIndex;     
  16.                 Form2 form2 = new Form2(text);     
  17.     
  18.                // After construction completion Form2, Form2 each parameter is assigned     
  19.                 form2.lb =this.listBox1;                    
  20.                 form2.index = index;     
  21.                 form2.Show();     
  22.             }     
  23.         }     
  24.     }   
Here are some issues that need attention, Form2 in which each attribute required assignment method? From Java to the over-all know, Java use the keyword super inherited in a subclass can access methods and parameters in the base class public, and C #, super into the base, that means we can not be so in Form2 it is parameter assignment?
 
  
  1. this.lb=base.listBox1;     
  2.     
  3. this.index=base.index;    
OK, not the wording of the second question, you can save the index value, but for the ListBox control, so the assignment will be a problem, I have found through testing, base.listBox1 points, is a subclass of inherited listBox1 objects, not the base class listBox1 own objects. Therefore, we speculate that base.index value is not also point to index a subclass of it? Test and found that indeed is the case, therefore this.index = base.index equal did not write, can still be removed, because the index is the same as Form2 inherited, so we can understand, C # form in succession, by base. Controls is not operating base class for controls.
Communication between C # Method three forms: event callback
Since C # events have this thing, so why do it, and the event in the form of communication, has a more convenient role, we know that the event is actually a state of capture, in the end I will give an example of a captured state, look at the examples of each data operation.
Form2:
 
  
  1. // define a delegate type parameter string needs     
  2.     
  3. publicdelegate void MyDelegate(string text);     
  4.     
  5. public partial class Form2 :Form1     
  6.     {     
  7.     
  8.        // define the delegate event     
  9.         public event  MyDelegate MyEvent;      
  10.         public Form2(string text)     
  11.         {      
  12.             InitializeComponent();     
  13.             this.textBox1.Text = text;     
  14.         }     
  15.         private void btnChange_Click(object sender, EventArgs e)     
  16.         {     
  17.     
  18.            // trigger event and the modified text return     
  19.             MyEvent(this.textBox1.Text);     
  20.             this.Close();     
  21.         }     
  22.     }   
Form1: 
 
  
  1. public partial class Form1 :Form     
  2.     {     
  3.         public int index = 0;     
  4.         public string text = null;     
  5.         public Form1()     
  6.         {     
  7.             InitializeComponent();     
  8.         }     
  9.     
  10.         private void listBox1_SelectedIndexChanged(object sender, EventArgse)     
  11.         {     
  12.             if (this.listBox1.SelectedItem != null)     
  13.             {     
  14.                 text = this.listBox1.SelectedItem.ToString();     
  15.                 index = this.listBox1.SelectedIndex;     
  16.                 Form2 form2 = new Form2(text);     
  17.     
  18.                MyEvent event // Register form2_MyEvent method     
  19.                 form2.MyEvent += new MyDelegate(form2_MyEvent);     
  20.                 form2.Show();     
  21.             }     
  22.         }     
  23.     
  24.        //deal with     
  25.     
  26.         void form2_MyEvent(string text)     
  27.         {     
  28.             this.listBox1.Items.RemoveAt(index);     
  29.             this.listBox1.Items.Insert(index, text);     
  30.         }     
  31.     }   
As can be seen, it is convenient to use the event to do, and do not need to pass so many parameters, need not have inheritance and improved code reuse, so in general needs, we recommend the use of so.
 
=================

C # several methods of communicating data between a form

http://www.cnblogs.com/crhacker/archive/2005/04/10/134933.html

writing # C Windows application when we often encounter such a problem, how to pass data between the two forms it? For example, to do with C # in a text editor, which has a search function (ie the search text inside the text I opened), point search pop-up search box, type what you're searching for and then OK, you can search me open the text inside the text, and here used the mutual communication between the two forms. I checked the relevant information thought, get some ideas and methods.
    Maybe some people will think this is very simple way. If the main frame is Form1, opens a search dialog is stated Form2 Form1 class instance in the Form2: Form1 f1 = new Form1 () ; then can call Form1 domains and by F1. Function of. In fact not the case, a new instance of Form1 Form1 not the original objects that you stated, and this operation is a new Form1 domains and functions, and the first to open Form1 is not related.
How it should be done two forms of communication it? We need to do is to pass the current instance of Form1 to Form2, if that is the case, the problem is solved.
Method 1: First, we define in Form2:
Private Form1 mF_Form
we change the constructor of Form2 to have parameters of
public Form2 ( Form1 myForm )
{
//
// Windows Form Designer support necessary
//
InitializeComponent ();
this.mF_Form = myForm; ///// Form1 put such instances when the transfer of Form2 Form1 affirmed come
@
@ the TODO: Add any constructor code after InitializeComponent call
//
}

in Form1 in Form2 me where you used to affirm as follows:
Form2 F2 = new new Form2 (the this); //// the this here refers to the Form1 current instance is the current instance of the Form1 by the constructor of Form2 passed to the Form2 class (in fact, seen on the internet relatively stupid way is to pass the information to be passed in the constructor function as: strings or numbers, etc., to do so is very limited, can not pass the other, all we can examples of direct transfer to transfer more complete information.)
This allows the operation to the original window Form1 Form2 used in the myForm. But Form1 you want to operate in the fields and functions defined as public in the form of ( This may be insecure ), myForm at this time is the real beginning to open Form1, you can use this instance to both forms of communicating.
Method 2: In fact, C # provides a ready-made properties for communication between form, Oh, we can think of, Microsoft also thought, they created the language in fact can indeed be said to be humane.
Affirmed Form2 Form1 class when using the following code:
Form2 f2 = new Form2 (); ////// class constructor Form2 is not changed, or no arguments
f2.owner = this ; //// herein this refers to the current class instance Form1.
// function method may also be used, to the current instance of window to add a subsidiary Code: this.AddOwnedForm (f2);
write the following code defined Form2 class:
Form1 f1 = this.owner;
this is the original corresponding to f1 Form1 examples, and also can use this to communicate a. But still want access between domains and functions of different classes defined as public, hey, safety is really a problem! !

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/12/28/2305561.html

Guess you like

Origin blog.csdn.net/weixin_34095889/article/details/93053043