pass a value from one form to another

后端 未结 7 1192
Happy的楠姐
Happy的楠姐 2020-11-27 21:10

I have two forms, and I need pass a value from form1.textbox1 to form2.variable

Form1:

string Ed = \"\", En = \"\"; 

public string En1
{
    get { r         


        
7条回答
  •  甜味超标
    2020-11-27 21:52

    This question may be old, but anyways...

    Another way to do it is, to have a constructor in form2 that accepts an argument of the same type of the data you want to pass to, and, when button in form1 is clicked, you create the instance of form2 using the constructor that accepts the argument, and pass the data to it.

    //Form1
    Form2 form2;
    button1_clic(object sender, eventArgs e)
    {
            form2 = new Form2(textbox1.text);
            form2.Showdialog();
    }
    
    //Form2
    string var = string.empty;
    public Form2(string val)
    {
            InitializeComponents();
    
            var = val;
    }
    

提交回复
热议问题