ShowDialog Question

  • Thread starter Thread starter Tom Bean
  • Start date Start date
T

Tom Bean

I'm using ShowDialog() to display a form as a modal dialog box. I have set
the form's minimizeBox property to true, however, the emulator always shows
an 'OK' button in the upper right-hand corner. According to the
documentation, this setting should result in an 'X' button being displayed.

When the user dismisses the form by clicking the 'OK' button, I display a
'YesNoCancel' MessageBox in the Closing event handler to determine what
DialogResult to return. If 'No' is selected, I set the form's DialogResult
property to 'Cancel', however, ShowDialog() always returns 'OK'.

What do I need to do to have the DialogResult, returned by ShowDialog(), be
the value I set it to in OnClosing()?

My application is written in C# in Visual Studio .NET 2003.

Thanks,
Tom
 
You might take a look at this alternative: If you set the ControlBox
property of your form to false you don't have any button at all in your
title bar. If you have enough space to have some buttons on your client area
you don't have to show an extra MessageBox. In the handlers of your buttons
just set the DialogResult and it will be passed as return value to the
caller of ShowDialog, e.g. your main form.
 
Maarten,

In some cases, I don't have enough space to add buttons in my client area.

Are you not able to change the DialogResult in a form displayed using
ShowDialog()? Why does a form displayed using ShowDialog() always have an
OK button?

Tom
 
The thing is that a form displayed using ShowDialog is a modal form. Modal
dialogs should be closed before you can continue working on the form from
which the modal dialog was activated. The "X" just minimizes the window, but
you would not be able to input data in your form anyway because the modal
form has not yet been closed. I will take a look tonight for ways to pass
your DialogResult unless somebody else beats me of course :-)

--
Regards,

Maarten Struys
PTS Software bv
----
 
Maarten,

I can't get the 'X' to display in a form displayed using ShowDialog(). The
window always has an 'OK' button in the upper right-hand corner.

Tom
 
Tom,

What Maarten is trying to explain is that the "X" doesn't mean Cancel - it means
Minimize, and the OK in the upper right corner of the dialog doesn't mean Save,
it means Close. So to get DialogResult.Cancel, you need a Cancel button the user
can click, and when he does then set this.DialogResult = DialogResult.Cancel in
the button's click event handler. I understand your concern that space is at a
premium on a device with a small screen, but you can't do it the way you want
to. Instead, you should consider using tab pages so you have room for both an OK
and a Cancel button on the form.
 
Tom,

That was the idea. ShowDialog only shows the OK button because that
terminates the Dialog, allowing the user to continue on the form from which
it was started.
To pass the results of your 'YesNoCancel' MessageBox, handle the Validating
event and assign its DialogResult to your form's DialogResult in that event
handler.

public class Form2 : System.Windows.Forms.Form
{

private DialogResult msgBoxresult;

public Form2()
{
.......
}

private void Form2_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
msgBoxresult = MessageBox.Show("Are you sure?", "Question",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.None,
MessageBoxDefaultButton.Button1);
}

private void Form2_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
this.DialogResult = msgBoxresult;
}
}
 
Maarten,

Thanks, handling the Validating event allows me to set the DialogResult of
the form after it closes to the user choice from the message box. I had
already added a variable representing the user choice and now I can pass its
value back.

Thanks again,
Tom
 
Maarten,

I seem to be catching the Validating event every time something happens in
any of the controls on my form, e.g. clicking the down arrow on a ComboBox.
That was causing the 'YesNoCancel' MessageBox() to pop. I got around it by
checking the msgBoxresult variable to make sure it wasn't set to 'None'.

Is that the normal way Validating events are raised?

Tom
 
Back
Top