C # Form Pass value

Source: Internet
Author: User

Make a clean look:

1. Static variable transfer value, very simple for simple non-instance

Public CALSS form1:form{public    static int A;} public class form2:form{   Form1. A=1; }

2. Transfer value of the delegate

public class form1:form{public  int a{get;set;}  public static action<int> delegate for public  Action<int>setintadel   //instance of  setintadelforclass;//class public void Setinta (int a) {    a=a;  }  public void Form_Load (object sender, EventArgs e) {  setintadelforclass=setinta;  Setintadel   =setinta;  }} public class form2:form{public  void setfom1inta{   Form1.setintadelforclass (10);//The A variable of Form1 is set to 10 by the delegate of the class  Form1 fm1=new Form1 ();  Fm1.setintadel (12);//The FM1 of a new instance is set to a variable by the delegate of the instance  

3. Use the Onwer property to fit the value of the dialog box

public class form1:form{public  int a{get;set;}     private void Button1_Click (object sender, EventArgs e) {    a=10;    Form2 fm2=new Form2 ();    FM2. ShowDialog (this);}   } public class form2:form{    private void Button1_Click (object sender, EventArgs e)        {            Form1 fm = (FORM1) this. Owner;            MessageBox.Show (FM. a);//Read FORM1 's a            FM. a=11;//write Form1 ' a                                        }}

Of course, you can also use a delegate to continue passing values

4. Refactoring the form constructor, when initializing the value, only suitable for initialization, not convenient

5. Method of Delegate + Event

The following code is a click Form1 button Make Form2 button display Form1.textbox content can pass many values at one time, the step is to declare an event in form a, B form to implement the same method signature method for the event assignment value, B callback the method FORM1 code://----------------- ---------------------->>--------------------Form1---------------Public partial class form1:form{public Strin            G B//Get TextBox1 's Text {get {return textbox1.text;}            set {TextBox1.Text = value; }} public delegate void Eventargsaccept (object sender, Accepteventargs e);//delegate public to declare an event signature Stati            C Event Eventargsaccept accept;//is equivalent to instantiating an event private void Button1_Click (object sender, EventArgs e) {            Accepteventargs AE = new Accepteventargs ();                        ae.b = b;            if (accept! = null) {accept (this,ae); }}}} public class Accepteventargs:eventargs {//Encapsulates EventArgs class, add transitive properties public string B {get; set; }}//------------------->>----------------------------------EndCode of Form1-----------Form2, Implementing a method of the same signature, such as our accept signature is the method name (object A,accepteventargs b);//---------------------  ----------------->>---------------------------------------Code of Form2---public partial class form2:form{ private void Form2_load (object sender, EventArgs e) {form1.accept + = form1_accept;//Event assignment for Form1 when Form1  When this event is performed, the method is executed} void Form1_accept (Object Sender,accepteventargs e) {//Implement a method for the same method signature This.button1.Text =                 e.b; }}//------------------------>>---------------The specific principle, I think because the delegate is a function pointer, so you can pass the value can save the position of the function pointer? So can you tag the corresponding instance, and execute the method of the other instance? Have not seen the compilation principle, publish their own views, do not mislead the public

  

6. Read and write through global data, suitable for login verification

AppDomain.CurrentDomain.SetData ("User", "xiaoming"); AppDomain.CurrentDomain.GetData ("user");

  

C # Form Pass value

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.