Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form.ShowDialog OK button doesn't work

Tags:

c#

showdialog

In my MainForm I'm declaring a second form:

Form3_addrow testDialog;

I've got a button and method OnClick:

private void button2_Click(object sender, EventArgs e)
{
    ShowMyDialogBox();
}

And ShowMyDialogBox() method:

public void ShowMyDialogBox()
{
    testDialog= new Form3_addrow(tran_in);

    DialogResult dr = testDialog.ShowDialog(this);

    if (dr == DialogResult.Cancel)
    {
        testDialog.Close();
    }
    else if (dr == DialogResult.OK)
    {
        testDialog.Close();
    }
}

When I click the button, testDialog is displayed, but when I click the OK button, testDialog doesn't close... the CANCEL button works though. Why doesn't my form close when I click the OK button?

like image 697
bred_one Avatar asked Feb 14 '23 01:02

bred_one


2 Answers

An alternative to setting the DialogResult in the button click handler would be to set the DialogResult on the OK button itself - in form design you can set the result to OK (this works without needing a click handler at all for the button.)

It sounds like your cancel button already has this property set

like image 170
NDJ Avatar answered Feb 26 '23 20:02

NDJ


You need to set the DialogResult in the button click handler on your form. See this SO question

EDIT: And close the form in the handler as well. Missed the forest for the trees.

like image 25
Matt Avatar answered Feb 26 '23 20:02

Matt