Methods for mutual access between forms in. net

Source: Internet
Author: User

In our programs, forms often need to access each other's members, call each other's methods, or stimulate each other's events. In my experience, I will briefly discuss several methods of mutual access between forms in. net. The following provides sample code for C #. NET and VB. NET. The form1 and form2 classes are used to represent the master and slave forms respectively.

1. The internal Member access method is suitable for accessing members in the form object created by the main form. You only need to add the internal or friend qualifier to the Members that allow access to the main form from the form. For example:

C # Master

Private void showform2info (){

Form 2 F 2 = new form2 ();

F2.show ();

MessageBox. Show (f2.myname );

}

C # slave

Internal string myname = "A form2 object ";

VB master

Private sub showform2info ()

Dim F2 as new form2

F2.show ()

Msgbox (f2.myname)

End sub

VB slave

Friend myname as string = "A form2 object"

2. Use the above method in turn to allow access from the form to create members in its main form. You need to add a step: pass in a reference to the main form from the form constructor, in order to find the main form for creating it (do not forget to call the initializecomponent () function in the newly created constructor ). When the main form is created from the form object, its reference is passed in. For example:

C # Master

Private void showform2 (){

Form 2 F 2 = new form2 (this );

F2.show ();

}

 

Internal string myname = "A form1 object ";

C # slave

Private form 1 F 1;

Public form2 (form1 _ F1 ){

Initializecomponent ();

F1 = _ F1;

}

 

Private void showform1info (){

MessageBox. Show (f1.myname );

}

VB master

Private sub showform2 ()

Dim F2 as new form2 (me)

F2.show ()

End sub

 

Friend myname as string = "A form1 object"

VB slave

Private F1 as form1

Public sub new (byval _ F1 as form1)

Mybase. New ()

Initializecomponent ()

F1 = _ F1

End sub

 

Private sub showform1info ()

Msgbox (f1.myname)

End sub

Note: My friend wuzhenzhi pointed out that it is better to pass in the reference of the main form through attributes or methods than in the constructor, because "for a form, there is nothing left, however, if it is a user control, we often drag it from the toolbar like dragging the button control, then the system will call the default constructor, instead of your overloaded version, this method fails." This suggestion makes sense.

3. Access members in the main form from the form. If the main form has only one instance or the accessed object is global, you can set the members required to be accessed in the main form to static, add the static or shared qualifier:

C # Master

Private void showform2 (){

Form 2 F 2 = new form2 ();

F2.show ();

}

 

Internal static string myname = "A form1 object ";

C # slave

Private void showform1info (){

MessageBox. Show (form1.myname );

}

VB master

Private sub showform2 ()

Dim F2 as new form2 ()

F2.show ()

End sub

 

Friend shared myname as string = "A form1 object"

VB slave

Private sub showform1info ()

Msgbox (form1.myname)

End sub

4. You can also use the. NET event delegation mechanism to implement more flexible Mutual calls. In the following example, an event is triggered from the form. The event entrusting mechanism is actually far more powerful than this example. You can look for a book to see its detailed usage.

C # Master

Private void showform2 (){

Form 2 F 2 = new form2 ();

F2.show ();

F2.sendmessage + = new form2.delegateofsendmessage (showmessage );

}

 

Private void showmessage (string Str ){

MessageBox. Show (STR );

}

C # slave

Public Delegate void delegateofsendmessage (string Str );

Public event delegateofsendmessage sendmessage;

 

Private void send (){

Sendmessage ("A message from form2 ");

}

VB master

Private sub showform2 ()

Dim F2 as new form2

F2.show ()

Addhandler f2.sendmessage, addressof showmessage

End sub

 

Private sub showmessage (byval STR as string)

Msgbox (STR)

End sub

VB slave

Public Delegate sub delegateofsendmessage (byval STR as string)

Public event sendmessage as delegateofsendmessage

 

Private sub send ()

Raiseevent sendmessage ("A message from form2 ")

End sub

5. For the same reason, refer to methods 2 and 4 to activate events in the form of the main form. Please experiment with yourself. Of course, the code will look messy. A better solution is to encapsulate the event into an interface and implement it at the event publisher and subscriber. This technology is particularly important in. Net remoting remote object operation technology, there is a good article for your reference: remoting event handling full contact (http://www.cnblogs.com/wayfarer/articles/75213.html)

6. Friends who are used to global variables in VB6 may think. net does not support global variables, which is inconvenient for interaction between forms. For more information, see this article :《. net form between the interaction "(http://blog.csdn.net/warsgrobe/archive/2005/07/29/438013.aspx), which describes the use of VB. net to simulate global variables. In addition, my friend wuzhenzhi proposed a global variable solution in the global module, you can refer to his article: On the default start form operation method (http://wuzhenzhi.blogdriver.com/wuzhenzhi/875776.html)

7. On the second day of this article, wuzhenzhi reminded me: in fact, there is a ready-made method in. Net to allow access to members of the main form from the form, that is, the form. showdialog method with the owner parameter. This owner is a reference to the main form. This shortcut method is applicable to the dialog box-type slave form (that is, the master form cannot be operated when the slave form is operated), but not to the child form such as MDI. Note that the owner is an iwin32window and must be forcibly converted to the main form class.

C # Master

Private void showform2 (){

Form 2 F 2 = new form2 ();

F2.showdialog (this );

}

 

Internal string myname = "A form1 object ";

C # slave

Private void showform1info (){

MessageBox. Show (form1) (This. Owner). myname );

}

VB master

Private sub showform2 ()

Dim F2 as new form2 ()

F2.showdialog (me)

End sub

 

Friend myname as string = "A form1 object"

VB slave

Private sub showform1info ()

Msgbox (ctype (Me. Owner, form1). myname)

End sub

8. How can I use the MDI subform? The most feared thing is to look at the above methods to form a mindset and apply them directly. Note: contradictions are both universal and special. There are also ready-made methods for the MDI child form, that is, the mdiparent attribute of the child form is called to access the main form. We all know that we should keep the integrity of the article here.

C # Master

Private void showform2 (){

Form 2 F 2 = new form2 ();

F2.mdiparent = this;

F2.show ();

}

 

Internal string myname = "A form1 object ";

C # slave

Private void showform1info (){

MessageBox. Show (form1) (This. parentform). myname );

}

VB master

Private sub showform2 ()

Dim F2 as new form2

F2.mdiparent = me

F2.show ()

End sub

 

Friend myname as string = "A form1 object"

VB slave

Private sub showform1info ()

Msgbox (ctype (Me. parentform, form1). myname)

End sub

OK. Let's talk about it here .. Net is a powerful and flexible system. As long as you are willing to use your brains, you will surely find a lot of such skills.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.