For simple communication between forms, the VB6.0 method can meet our requirements. However, in some complicated architecture design applications, this method is a little too slow, at the same time, this method also has a disadvantage, that is, it only applies to pass. the form added by the. NET Form Wizard takes effect, but the custom form type cannot be added to the forms object set. In addition, like other methods such as constructor parameter passing, they will reference their members in a large number of forms, resulting in great coupling between them, it is not conducive to the independence of form modules, which is not in line with the idea of a good software design model.
If we want to access a custom member in another form, we must set the Member's visibility to public or make the member public through the attribute. If we want to access the custom member in another form, we can still say that the member is made public through the attribute, but if you set the visibility to public, this will inevitably undermine the type encapsulation principle, and this approach is also in our. NET development is quite happy, especially for initial contact. NET developers first think of setting the Member's visibility to public when accessing another type of members. Of course, this is not an error, however, it is obviously inappropriate to take this approach as the primary inspiration, at least from the object-oriented perspective.
In. net, another powerful mechanism is provided for us to implement form communication. This is the delegate. Delegation can be understood as a type of safe function pointer. The implementation of events in. NET is based on delegation. I will not introduce delegation in detail in this article. Later I will introduce this concept in detail. In this example, we add an item to The ListBox control in another form to describe this method. Therefore, two forms, one mainfrm form and one childfrm form are required. In addition, a middle class is required to serve as a bridge between mainfrm and childfrm. I will provide the C # code for your reference.
The first is the mainfrm form. In the mainfrm form, drag a ListBox control. The mainfrm. CS code is as follows:
Below is the corresponding C # code, mainfrm. CS:
Public partial class mainfrm: Form
{
Private void mainfrm _ load (Object sender, eventargs E)
{
Middle. sendevent + = new middle. sendmessage (this. domethod );
}
Public void domethod (string getstr)
{
Lsmain. Items. Add (getstr );
}
}
Childfrm. CS:
Public partial class childfrm: Form
{
Public childfrm ()
{
Initializecomponent ();
}
Private void btnadd_click (Object sender, eventargs E)
{
Middle. dosendmessage (this. textbox1.text );
Txtitem. Text = "";
Txtitem. Focus ();
}
}
Middle. CS:
Public static class middle
{
Public Delegate void sendmessage (string Str );
Public static event sendmessage sendevent;
Public static void dosendmessage (string Str)
{
Sendevent (STR );
}
}
Let's also modify the code of program. CS:
Static class Program
{
[Stathread]
Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Childfrm child = new childfrm ();
Child. Show ();
Application. Run (New mainfrm ());
}
}
According to the above code, we can see that C # must first declare the event Delegate prototype and then declare the event based on the delegate. From this point of view, VB. net is more concise, in fact, VB. net compiler automatically defines a delegate object for us, and the delegate is the same as the Il code generated by the delegate declared by C # code, you can view this point through the ildasm intermediate code viewer. Event, VB. net adds the event name through the raiseevent keyword, while C # directly uses the event name. Finally, it binds the Event code, VB. net is through the addhandler keyword, C # Through the overloaded + = Operator, for the above two points, the compiler will also generate consistent il code for us.
Of course, the above example is relatively simple, but we can implement complex form communication through delegation. For example, we can transmit complex data types, and at the same time, we can design intermediate Communication classes with better structures. However, we should also remind you not to use delegation unless you do not move. It will increase the complexity of the program, and you should consider the method based on your own needs.
Turn: http://www.isabout.net/article.asp? Id = 215