Send values from one form to another form

后端 未结 19 2490
眼角桃花
眼角桃花 2020-11-21 11:45

I want to pass values between two Forms (c#). How can I do it?

I have two forms: Form1 and Form2.

Form1 contains one button. When I click on that button, Fo

相关标签:
19条回答
  • 2020-11-21 12:11

    Define a property

    public static class ControlID {  
        public static string TextData { get; set; }
    }
    

    In the form2

    private void button1_Click(object sender, EventArgs e)
    {  
        ControlID.TextData = txtTextData.Text;   
    }
    

    Getting the data in form1 and form3

    private void button1_Click(object sender, EventArgs e)
    {   
        string text=  ControlID.TextData;   
    }
    
    0 讨论(0)
  • 2020-11-21 12:11

    Declare a public string in form1

    public string getdata;
    

    In button of form1

    form2 frm= new form2();
    this.hide();
    form2.show();
    

    To send data to form1 you can try any event and code following in that event

    form1 frm= new form1();
    form1.getdata="some string to be sent to form1";
    

    Now after closing of form2 and opening of form1, you can use returned data in getdata string.

    0 讨论(0)
  • 2020-11-21 12:13

    You can use this;

    Form1 button1 click

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        this.Hide();
        frm2.Show();
    }
    

    And add this to Form2

    public string info = "";
    

    Form2 button1 click

    private void button1_Click(object sender, EventArgs e)
    {
    
        info = textBox1.Text;
        this.Hide();
        BeginInvoke(new MethodInvoker(() =>
        {
            Gogo();
        }));
    }
    
    public void Gogo()
    {
        Form1 frm = new Form1();
        frm.Show();
        frm.Text = info;
    }
    
    0 讨论(0)
  • 2020-11-21 12:14
    private void button1_Click(object sender, EventArgs e)
    {
            Form2 frm2 = new Form2(textBox1.Text);
            frm2.Show();    
    }
    

     public Form2(string qs)
        {
            InitializeComponent();
            textBox1.Text = qs;
    
        }
    
    0 讨论(0)
  • 2020-11-21 12:14

    declare string in form1 public string TextBoxString;

    in form1 click event add

    private void button1_Click(object sender, EventArgs e)
        {
            Form1 newform = new Form1();
            newform = this;
            this.Hide();
            MySecform = new Form2(ref newform);
            MySecform.Show();
        }
    

    in form2 constructer

    public Form2(ref Form1 form1handel)
        {
            firstformRef = form1handel;
            InitializeComponent();
        }
    

    in form2 crate variable Form1 firstformRef;

    private void Submitt_Click(object sender, EventArgs e)
        {
            firstformRef.TextBoxString = textBox1.Text;
            this.Close();
            firstformRef.Show();
    
        }
    
    0 讨论(0)
  • 2020-11-21 12:16

    After a series of struggle for passing the data from one form to another i finally found a stable answer. It works like charm.

    All you need to do is declare a variable as public static datatype 'variableName' in one form and assign the value to this variable which you want to pass to another form and call this variable in another form using directly the form name (Don't create object of this form as static variables can be accessed directly) and access this variable value.

    Example of such is,

    Form1

    public static int quantity;
    quantity=TextBox1.text; \\Value which you want to pass
    

    Form2

    TextBox2.Text=Form1.quantity;\\ Data will be placed in TextBox2
    
    0 讨论(0)
提交回复
热议问题