Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShowDialog doesn't return DialogResult

Tags:

c#

winforms

I made a form2 that shows and there are buttons which return DialogResult but I have no idea why this doesn't work:

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!
}

Form2:

String Name;

public String GetName
{
    get { return Name; }
}

private void button1_Click(object sender, EventArgs e)
{
    button1.DialogResult = DialogResult.OK;
    this.Close();
}
like image 855
user1337 Avatar asked Sep 02 '26 18:09

user1337


2 Answers

I think you should use

private void button1_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
    this.Close();
}
like image 140
Abhinav Avatar answered Sep 04 '26 08:09

Abhinav


Just set button1 to the AcceptButton on the Form object. You won't need a single line of code.

like image 28
Mike Perrenoud Avatar answered Sep 04 '26 10:09

Mike Perrenoud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!