Vivek S Bhujbal
Vivek S Bhujbal

Reputation: 35

Dialog Result Always returning "Cancel" on button click

I have a form PatientInformation which has a datagridview for displaying existing records in the Database. I also have another form for updating the records present in the datagridview.

Hence I need to refresh the datagridview for taking the updates into consideration. I have 2 buttons (Cancel,Update) on the Update Form. Now the problem lies here: My Dialog result is returning 'Cancel' always even after I click the Update button.

My form also contains a method for Form Closed event. Is this what is causing the problem?

My calling statements are as follows :

diagResult = patientUpdateVlObject.ShowDialog();
if (diagResult.ToString() == "Update")
{
   dtgrdviewSearchOutput.Refresh();
   DipslayMessage("Record Updated Successfuly!!");
}
else
   //Do nothing.

and my form closed method in the Update Form is as follows :

private void PatientUpdate_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Dispose();
    }

Upvotes: 0

Views: 1099

Answers (2)

Zignd
Zignd

Reputation: 7025

You shouldn't use diagResult.ToString() == "Update", because when you do so you're converting the control name to string.

To fix such problem you should modify your code to something like that:

private void buttonMessageBox_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Click \"OK\" if you agree with this", "MessageBoxTest", MessageBoxButtons.OKCancel)
        == DialogResult.OK)
    {
        MessageBox.Show("User clicked in \"OK\"");
    }
    else
    {
        MessageBox.Show("User clicked in \"Cancel\"");
    }
}

In this example I removed the ToString method and now I'm comparing it to DialogResult.OK (the MessageBox.Show Method returns a DialogResult object, so you can't compare it to a string as you did).

Upvotes: 1

MeTitus
MeTitus

Reputation: 3428

Of course, you're checking the name:

http://msdn.microsoft.com/en-us/library/2chz8edb.aspx

        if (diagResult == DialogResult.OK)
        {
           dtgrdviewSearchOutput.Refresh();
           DipslayMessage("Record Updated Successfuly!!");
        }

Upvotes: 0

Related Questions