Send values from one form to another form

2018-12-31 02:44发布

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, Form2 should open and Form1 should be in inactive mode (i.e not selectable).

Form2 contains one text box and one submit button. When I type any message in Form2's text box and click the submit button, the Form2 should close and Form1 should highlight with the submitted value.

How can i do it? Can somebody help me to do this with a simple example.

19条回答
低头抚发
2楼-- · 2018-12-31 03:38

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;
}
查看更多
浅入江南
3楼-- · 2018-12-31 03:40
private void button1_Click(object sender, EventArgs e)
{
        Form2 frm2 = new Form2(textBox1.Text);
        frm2.Show();    
}

 public Form2(string qs)
    {
        InitializeComponent();
        textBox1.Text = qs;

    }
查看更多
路过你的时光
4楼-- · 2018-12-31 03:40

Form1 Code :

private void button1_Click(object sender, EventArgs e)
{
            Form2 f2 = new Form2();
            f2.ShowDialog();
            MessageBox.Show("Form1 Message :"+Form2.t.Text); //can put label also in form 1 to show the value got from form2
}

Form2 Code :

        public Form2()
        {
            InitializeComponent();
            t = textBox1;                        //Initialize with static textbox
        }
        public static TextBox t=new TextBox();   //make static to get the same value as inserted
        private void button1_Click(object sender, EventArgs e)
        {

            this.Close();

        }

It Works!

查看更多
梦该遗忘
5楼-- · 2018-12-31 03:40

This is very simple. suppose you have 2 window form Form1 and Form2 and you want to send record of textbox1 from Form1 to Form2 and display this record in label1 of Form2; then in Form2 create a label which name is label1 and go to the property of label1 and set 'Modifiers'=public and in Form one create a textBox with id textBox1 and a button of name submit then write the following code on button click event

button1_Click(object sender, EventArgs e)
{
  Form2 obj=new Form2();
  obj.label1.text=textBox1.text.ToString();
  obj.show();
}

thats it... for this way you can bind dataset record to another form's datagridview......

查看更多
不流泪的眼
6楼-- · 2018-12-31 03:41

Constructors are the best ways to pass data between forms or Gui Objects you can do this. In the form1 click button you should have:

Form1.Enable = false;
Form2 f = new Form2();
f.ShowDialog();

In form 2, when the user clicks the button it should have a code like this or similar:

this.Close();
Form1 form = new Form1(textBox1.Text)
form.Show();

Once inside the form load of form 1 you can add code to do anything as you get the values from constructor.

查看更多
爱死公子算了
7楼-- · 2018-12-31 03:41

How to pass the values from form to another form

1.) Goto Form2 then Double click it. At the code type this.

public Form2(string v)
{
  InitializeComponent();
  textBox1.Text = v;
}

2.) Goto Form1 then Double click it. At the code type this. //At your command button in Form1

private void button1_Click(object sender, EventArgs e)
{
   Form2 F2 = new Form2(textBox1.Text);
   F2.Show();
}
查看更多
登录 后发表回答