Compile the C # Windows Application Program We often encounter this problem. How do we transmit data between two forms? For example, if you use C # as a text editor, there is a search function (that is, to search for the text in the opened text). Click search to bring up the Search dialog box and enter the content to search, then, you can find the text in the opened text. Here, the communication between the two forms is used. I thought about the relevant materials and came up with some ideas and methods.
Some may think this is very simple. If the main framework is form1, the Search dialog box is form2. you can directly declare a form1 instance in the form2 class: form1 F1 = new form1 (); then you can use F1 to call the fields and functions in form1. In fact, this is not the case. The new form1 instance you declare is not the original form1 object. In this way, the fields and functions in the new form1 instance are operated, it has nothing to do with the first open form1.
How can we complete communication between two forms? What we need to do is to pass the current form1 instance to form2. If so, the problem will be well solved.
Method 1: First, we define in form2:
Private form1 mf_form
We changed the form2 constructor to a parameter
Public form2 (Form1 myform)
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
This. mf_form = myform; // when form1 declares form2, The form1 instance is passed.
//
// Todo: add Any constructor after initializecomponent callsCode
//
}
In form1, I declare where form2 is to be used as follows:
Form2 F2 = new form2 (this); // This indicates the current instance of form1, that is to say, the current form1 instance is passed to the form2 class through the form2 Constructor (in fact, I have seen a stupid way on the Internet, that is, the information to be passed in the constructor is as follows: string or number, which is very limited and cannot be passed. We can directly pass the instance to complete the transfer of more information .)
In this way, you can use myform in form2 to operate on the original form1 window. However, you need to define the fields and functions in form1 to be operatedPublicFormat (This may be insecure.At this time, myform is actually the first to open form1. You can use this instance for communication between two forms.
Method 2: in fact, C # provides ready-made attributes for communication between forms. What we can think of is that Microsoft also thinks that the language they create is actually human.
Use the following code to declare form2 in the form1 class:
Form2 F2 = new form2 (); // The constructor in the form2 class does not change or has no parameters
F2.owner = This; /// This indicates the current instance of the form1 class.
// You can also use the Function Method to add one to the current instance.Subsidiary window code: This. addownedform (F2 );
Write the following code in the form2 class definition:
Form1 F1 = This. owner;
In this way, F1 corresponds to the original form1 instance, and thus can be used for communication. However, we still need to define the domains and functions accessed between different classes as public. Ah, security is indeed a problem !!
Solve it slowly... (change public to protected internal to allow access to the inherited class or the class in the same assembly)
Source: http://www.cnblogs.com/crhacker/archive/2005/04/10/134933.html