Mutual reference between Windows Forms

Source: Internet
Author: User

If you want to use winform to write something, you may encounter a depressing problem. If you want to transfer values between forms, you may encounter difficulties. Find a solution on the Internet and create one. Suppose you want to create form2 in form1 and form2 to access form1, the simplest way is to add

Public Form1 parent;

When form1 is created as follows:

Form2 F2 =   New Form2 ();
F2.parent =   This ;
F2.show ();

In form2, you can use parent to access form1.

However, if form1 and form2 have no parent-child relationship, form2 is not created in form1, and form2 is not created in form1. How can we get the instance of the other party? Form1 is used as an example. We create another form and use it to instantiate other forms, such as starter.

Add the static variables F1 and the static method getform1 () in starter ():

Class starter
Public Partial Class Starter: Form
{
Public   Static Form1 F1;

Public   Static Form1 getform1 ()
{
If (F1 =   Null )
{
F1= NewForm1 ();
}
Return F1;
}

Public Starter ()
{
Initializecomponent ();
Getform1 (). Show ();
}
}

You can use starter. getform1 () to get F1. Set starter to minimize startup. If it is not displayed in the status bar, we will not see it.

PS: We also found that any event's xxxing () processing function can include eventargs e of the event. if cancel is set to true, the event is canceled. It is only now that this useful stuff is known... -_-

Related Article

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.