. NET to call each other between form

Source: Internet
Author: User
Tags define

. NET to call each other between form



With. NET, some of the previous programming ideas have changed. Remember that in the VB6 era, we often create several form and invoke it directly using the Load method. Here it is. NET ERA, VB also introduced the concept of class. So the way the form is invoked also changes. For example:




Public frm as New Form1



frm. Show ()




Because Form1 here is a class, not an object. We define an object frm of a Form1 class, instantiate it by the new method, and display it. Sometimes beginners will use:




Public frm as Form




This prompts you to use a member of the FRM without instantiating an error. The new method is to create a new Form1 instance. If you do not use the new method, you will need to pass the handle of the FORM1 instance that you have created to the Frm object by other means. The good thing about this is that we can see a lot of Form1 objects in the program as a generic form, and of course we can just use one. At this time, many people can naturally use Form1 public members. But sometimes you need to use FORM2 members in Form1, which takes into account the problem of two form relationships. Here is an example:



In the MFC type of MDI project, we know that there will be mainfrm and childfrm concepts. Now use one. NET of WinForm Engineering examples. The main frame is a mainfrm (set it to an MDI container) and contains a subform such as frmhome,frmreport,frmoption. At the beginning of the program, we need to load the frmhome into the mainfrm. The different subforms are then displayed based on different messages. Also, members between these subforms need to be called each other. It is time for us to define several public members frmhome,frmreport,frmoption in the MAINFRM.




Public Frmhome as New frmhome



Public Frmreport as New Frmreport



Public frmoption as New frmoption







Private Sub mainfrm_load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load



Frmhome.mdiparent = Me



Frmhome.show ()



Frmhome.dock = DockStyle.Fill



End Sub




Now we want to call the members of the Frmhome in the Frmreport, and from the structure below, we can think of the mainfrm as a bridge. You can define an object for the main frame on each subform, or you can define a global variable to hold the MAINFRM handle.











If you define a MainFrm object on a subform, we need to pass the handle of the created program's main frame to the object:




Dim Pmainfrm as MainFrm



Pmainfrm = CType (me.mdiparent, MAINFRM)




So we can use the Pmainfrm.frmhome member variable. This is somewhat similar to the common definition of a PMAINFRM pointer in MFC's MDI to find the main frame.




cmainframe* pmainfrm = (cmainframe*) gettoplevelframe ();



cmainframe* pmainfrm = (cmainframe*) AfxGetApp ()->m_pmainwnd;




It can also be used if it is just a call between two forms. As long as you know the relationship between the two form, it's usually a child,parent,owner sort of relationship. For example:




Dim frm as Form1



frm = CType (Me.owner, Form1)









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.