winforms C#我试着在2个窗体之间传递,值没有传递

bgtovc5b  于 2023-02-24  发布在  C#
关注(0)|答案(1)|浏览(212)

我有2个窗体,每次都通过不同的按钮(button1或button2)打开窗体2。
表格2:通过单击按钮并选择复选框,将设置值
Form1:将根据按下的按钮接收值。
此时,Form1没有接收回该值
Form1

public partial class Form1 : Form
    {
public string valFrom1;
public string valFrom2;
private void button1_Click(object sender, EventArgs e)
{
     var form1 = new Form2();
     form1.Show();
     valFrom1 = form1.value;
}
private void button2_Click(object sender, EventArgs e)
{
    var form2 = new Form2();
    form2.Show();
    valFrom2 = form2.value;
}

Form2

public partial class Form2 : Form
    {
 public  string value { get; set; }

    public void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                value= "1";
            }
            if (checkBox2.Checked)
            {
                value = "2";
            }
            if (checkBox3.Checked)
            {
                value = "3";
            }
        }
deyfvvtc

deyfvvtc1#

你应该使用ShowDialog()代替Show() .编辑这个函数:

private void button2_Click(object sender, EventArgs e)
{
   var form2 = new Form2();
   form2.ShowDialog();
   valFrom2 = form2.value;
}

相关问题