ShowDialog不会返回DialogResult

时间:2013-11-01 18:43:15

标签: c# winforms

我创建了一个显示的form2,并且有返回DialogResult的按钮,但我不知道为什么这不起作用:

Form1中:

private void buttonEvent_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    if (form2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        labelEvent.Text = hEvent.GetName; //Breakpoint here but it doesn't stops!
}

窗体2:

String Name;

public String GetName
{
    get { return Name; }
}

private void button1_Click(object sender, EventArgs e)
{
    button1.DialogResult = DialogResult.OK;
    this.Close();
}

2 个答案:

答案 0 :(得分:7)

我认为你应该使用

private void button1_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
    this.Close();
}

答案 1 :(得分:1)

只需将button1设置为AcceptButton对象上的Form即可。您不需要一行代码。