DialogResults and Validation

J

jehugaleahsa

Hello:

I noticed that if you set a button's DialogResult in Windows Forms,
clicking it immediately closes the window. I would like to validate my
form _before_ it closes. Technically, I could set the dialog result
manually inside of a click event handler. I was wondering what was the
standard approach to implement simple dialog windows.

Do people generally set the DialogResult value for the buttons? Do
they set the Accept and Cancel Button for the forms? How is validation
performed? I don't want to validate each control; just the form all at
once when they user clicks okay.

Thanks,
Travis
 
J

Jeff Johnson

I noticed that if you set a button's DialogResult in Windows Forms,
clicking it immediately closes the window. I would like to validate my
form _before_ it closes. Technically, I could set the dialog result
manually inside of a click event handler. I was wondering what was the
standard approach to implement simple dialog windows.

Do people generally set the DialogResult value for the buttons? Do
they set the Accept and Cancel Button for the forms? How is validation
performed? I don't want to validate each control; just the form all at
once when they user clicks okay.

By setting the DialogResult property for a button, you give your form the
ability to automatically close itself when that button is clicked. You can,
however, step in inside the event handler and manually set this.DialogResult
= DialogResult.None (note that you're setting the DialogResult property of
the FORM here, not the button) to prevent the form from closing if you
determine that validation has failed. If you do this kind of validation,
however, I would recommend not setting the DialogResult property of the
button at all and handling everything manually.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello:

I noticed that if you set a button's DialogResult in Windows Forms,
clicking it immediately closes the window. I would like to validate my
form _before_ it closes. Technically, I could set the dialog result
manually inside of a click event handler. I was wondering what was the
standard approach to implement simple dialog windows.

Do people generally set the DialogResult value for the buttons? Do
they set the Accept and Cancel Button for the forms? How is validation
performed? I don't want to validate each control; just the form all at
once when they user clicks okay.

Thanks,
Travis

Hi,

Settng DialogResult should not close the form, IIRC.

For validation you can use the Closing event, you can cancel the close
process if you need to
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

Settng DialogResult should not close the form, IIRC.

For validation you can use the Closing event, you can cancel the close
process if you need to

Correction, setting the DialogResult in a window that was displayed
using ShowDialog does close the window
 
J

jehugaleahsa

Hello:

I noticed that if you set a button's DialogResult in Windows Forms,
clicking it immediately closes the window. I would like to validate my
form _before_ it closes. Technically, I could set the dialog result
manually inside of a click event handler. I was wondering what was the
standard approach to implement simple dialog windows.

Do people generally set the DialogResult value for the buttons? Do
they set the Accept and Cancel Button for the forms? How is validation
performed? I don't want to validate each control; just the form all at
once when they user clicks okay.

Thanks,
Travis

Thanks for everyone's suggestions.

I have decided to make all but the OK button set the DialogResult. I
just do my validation in the button click event, setting the
DialogResult and calling Close if everything is okay.

The only reason I didn't like the Close event handler was because then
every closing action made me validate it. When two buttons Cancel and
only one OKays, it seemed like the wrong place for the logic.

I was playing with the idea of disabling the OK button and only
enabling it when the controls are valid. The problem was that the
controls have to lose focus. Once the user fills in all the fields,
they don't know what to do to enable the button (when all they need to
do is tab away). It is better to let them click the button and give
them an error message.

Okay, okay... here is another question. Say I had a cascading drop
down list. So, when the user enters in one field, it populates the
next. Should my code automatically refresh the next drop down list or
should I force the user to click a refresh button? What's the
standard, here?
 
J

Jeff Johnson

I have decided to make all but the OK button set the DialogResult. I
just do my validation in the button click event, setting the
DialogResult and calling Close if everything is okay.

Setting the DialogResult property to any value besides None will
automatically close the dialog; no need to call Close() manually.
The only reason I didn't like the Close event handler was because then
every closing action made me validate it. When two buttons Cancel and
only one OKays, it seemed like the wrong place for the logic.
I was playing with the idea of disabling the OK button and only
enabling it when the controls are valid. The problem was that the
controls have to lose focus. Once the user fills in all the fields,
they don't know what to do to enable the button (when all they need to
do is tab away). It is better to let them click the button and give
them an error message.

When it comes to error messages, I agree that you should only display them
once, on final validation (MS Access is a great offender in this category),
but that doesn't mean you can't validate on the fly. Relying on the Validate
event (or whatever it's called; I don't use it) does come with the
requirement that the control lose focus, but there are other ways to
validate. You can monitor the TextChanged event of a text box, for example.
When you have a required text value, you can use that event to see if the
Text property is of length 0 or not and enable the OK button accordingly.
Little things like this enhance your UI because if the OK button is
disabled, they know that something's incomplete, but not what. In this case
you should probably provide some other feedback (unless it's painfully
obvious) because now they can't click OK to get the error message. So it's a
judgement call.
Okay, okay... here is another question. Say I had a cascading drop
down list. So, when the user enters in one field, it populates the
next. Should my code automatically refresh the next drop down list or
should I force the user to click a refresh button? What's the
standard, here?

If at all feasible, automatically refresh. If the refresh would be costly in
time or system resources, use a button. Another judgement call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top