Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Can you remain in a dialog box after pressing OK?

I have a class that is a dialog box and OnOK is called (within the dialog class) when the user presses the OK button. I check to see if what they did was valid and if not I want to have the dialog box stay up so they can correct the changes. Right now it just falls through and the dialog box is done. Does anyone know if I can "cancel" out of the OK state and stay in the dialog box?

thanks,
Scottyo
0 Kudos
Message 1 of 8
(5,767 Views)
The default implementation of CDialog::OnOK calls CDialog::EndDialog, which closes the modal dialog. If the user input is not valid, you can omit the call to the base class implementation of CDialog::OnOK to cancel the action and stay in the dialog box.

- Elton
0 Kudos
Message 2 of 8
(5,764 Views)
Yea, that is true if you are using MFC.

I am using Forms. Sorry, I failed to mention that I'm using C#. My dialog class inherits from Windows.Forms.

Scotty
0 Kudos
Message 3 of 8
(5,760 Views)
It sounded like you were talking about MFC since you mentioned an OnOK method. There is no OnOK method on the Windows Forms Form class. Is this a method that you defined? Is it an event handler? How is it getting called and what is it doing?

- Elton
0 Kudos
Message 4 of 8
(5,755 Views)
Ok. I have a button click event handler called "button1_Click". "button1" is my "OK" button on my dialog box.

In there I check to see if the user is wanting to overwrite an existing "setup" file. If so, I bring up a MessageBox with Yes and Cancel buttons and ask them if they really want to overwrite. If they press Cancel on the MessageBox I want to stay in the dialog box to reselect another setup file name.
0 Kudos
Message 5 of 8
(5,751 Views)
How is your form getting closed? Clicking the button should not automatically close the form. Are you calling the Close method somewhere in the event handler?

Another way that you could handle this is to add an event handler for the Form Closing event. Closing is a cancellable event and you can prevent the form from closing by setting the Cancel property of the event arguments of this event to true.

- Elton
0 Kudos
Message 6 of 8
(5,744 Views)
You can get the result of the MessageBox and act on it accordingly.

Here is an example:

result = MessageBox.Show(this, message, caption, buttons,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);

if(result == DialogResult.Yes)
{

// Closes the parent form.

this.Close();

}


So, check DialogResult and that should allow you to determine which route you want to take.
0 Kudos
Message 7 of 8
(5,746 Views)
Through all of the suggestions I figured it out. In the Forms Designer I was setting the DialogResult property of the Ok button to be "OK". This is why it was automatically exiting. The default is None. I set it back to the default. In the event handler for both the Ok and Cancel buttons I programatically set the DialogResult and issued Close() if necessary. Thanks for the help from all, this dogged me once before in another instance and I really wanted to get this resolved.

Below is my code:

private void button1_Click(object sender, System.EventArgs e)
{
foreach( string setupname in SaveComboBox.Items )
{
if( setupname == SaveName )
{
string str = string.Format("Setup name: {0} exists! Overwrite?", SaveName);
DialogResult res = MessageBox.Show(str, "Save UserSetups", MessageBoxButtons.YesNo);
if( res == DialogResult.Yes )
{
// Delete usersetup in collection first for overwrite process
_UserSetupCollection.Remove(SaveName);
break;
}
else
return;

}
}

this.DialogResult = DialogResult.OK;

this.Close();

}

///
/// This is the Cancel button event handler
///

///
///
private void button2_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;

this.Close();
}
0 Kudos
Message 8 of 8
(5,652 Views)