C # Summary of passing parameters between winform forms

Source: Internet
Author: User

Summary of parameters passed between forms:

Solution 1: The form is a class. You can define global variables in the Form class. The type is public and static.

Example: public static string str = "";

Note that it is a static variable. If it is:

Public string str = "";

A problem may occur. Non-static variables can only be accessed after the object is instantiated. If this value is a dynamic value, this value is str when another window is called, it is always ""; so it is best to set it as a static member variable and use a class to access it.

Solution 2: Define the constructor in the Form class, which means that if form1 needs to use a variable of form2, define a constructor in form1:

Public Form1 (string str)

{

// When instantiating form1, pass the str parameter and call this parameter.

}

In my opinion, this method is the most suitable method to call when form1 is created in form2. In turn, if form1 is a main window, this solution is not the best.

Solution 3: PASS Parameters through events

The event mechanism has always been hard to understand. For example, to hold a meeting or arrange a meeting, you must first select the meeting time, location, and other factors (define the event), and then call the phone, notify you by email (event publishing). Once the other party receives the event, it will attend the meeting according to the previously set meeting time and place (Event Response ), the event is probably completed in three steps:

Step 1: Define the delegate:Public delegate void Mydelegate (string str) // set the parameter 'str' based on actual conditions.

Step 2: declare the event variable: public event mydelegate MyEvent;

Step 3: release an event. This is done by the Speaker and defines a function to stimulate the event.

For example:

Public void Handle ()

{

MyEvent (str );

}

Finally, the event receiver can subscribe to events.

Xx. MyEvent + = new MyDelegate (event processing function, defined by the receiver)

Xx. Handle ();

// The general operation is to press the tab key after "+ =". the. net platform will automatically generate a processing function for you, for example, the following processing function:

Void xx_MyDelegate (string str)

{

// The str parameter is the parameter passed by the publisher, including the meeting time and location;

// Process the code

Sample Code:

Window 1:

Using System;

Using System. Windows. Forms;

Namespace Test_Event
{
Public partial class Form1: Form
{
// Form1 Constructor
Public Form1 ()
{
InitializeComponent ();

}
Public static string str = "";
Private void button#click (object sender, EventArgs e)
{
Str = this. textBox1.Text;
Form2 f2 = new Form2 ();
F2.Show ();
F2.myevent + = new Form2.dd (f2_myevent); // subscribe to events
// F2.hand ();// Excitation function
}

Void f2_myevent (string str)
{
This. textBox1.Text = str;
}
}
}

Window 2:

Using System;
Using System. Windows. Forms;

Namespace Test_Event
{
Public partial class Form2: Form
{

Public Form2 ()
{
InitializeComponent ();
}
Public delegate void dd (string str); // defines the delegate
Public event dd myevent;// Event variable

// Event-triggered Functions

Public void hand ()
{
Myevent (this. textBox1.Text );
}
Private void button#click (object sender, EventArgs e)
{
// Hand ();
This. Close ();
}
Private void Form2_Load (object sender, EventArgs e)
{
This. textBox1.Text = Form1.str;
}

}
}

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.