I want to do some validations when clicking on OK button. For example, if selected folder is temporary folder, when I click on OK button, error message should be displayed and the Folder Browser Dialog should not be closed. How can I do, this function can be realized. Could you give me an example?
Asked
Active
Viewed 1,528 times
0
-
The FolderBrowserDialog is a sealed class and its behaviour has been defined and canned for you to use. Which means that if you want to run validations as part of an object you will have to either wrap the provided class with the one you want to expose, or you will have to implement your own dialog (there are lots of samples of how to implement it, if you are interested I can share the links). – rodrigoelp Jul 30 '14 at 02:54
-
Thanks for your anwser. But I want to know is there any method to make the Folder Browser Dialog is not closed? – StartLearn Jul 31 '14 at 03:15
-
The short answer is no. When you call `ShowDialog` on a `FolderBrowserDialog` its return value is set based on the dismissing mechanism of the dialog (pressing 'x' at the top, Escape, OK or Cancel buttons) and there is no interception at all for you to cancel the dismiss of the dialog itself. Is a pretty simple implementation. If you want to create something similar you will need to extend CommonDialog and build it from there (or download some code online). – rodrigoelp Aug 01 '14 at 08:26
-
Dear rodrigoelp, thanks for your answer. I have understood. Can you share the links? – StartLearn Aug 05 '14 at 08:11
-
Here is a sample that has been implemented for WPF http://wpffolderbrowser.codeplex.com/SourceControl/latest#library/WPFFolderBrowserDialog.cs – rodrigoelp Aug 13 '14 at 05:49
2 Answers
2
FolderBrowserDialog
does not expose any Validation
event or OnValidate
method like other WinForms components.
FolderBrowserDialog
does have OwnerWndProc
which you could use to extend the dialog, however I discourage this as it adds needless complexity.
I think the best option is to re-open the form in event of validation error, like so:
Boolean isValid = false;
while( !isValid ) {
DialogResult result = fbd.ShowDialog(this);
if( result != DialogResult.OK ) return;
isValid = IsFolderValid( fbd.SelectedPath );
if( !isValid ) {
MessageBox.Show(this, "Selected folder is invalid, please select a different folder or click Cancel.");
}
}

Dai
- 141,631
- 28
- 261
- 374
1
I'd suggest you to let the FolderBorwserDialog
close, check the selected path, if it is invalid then show the error message, and finally upon closing the error message display the file dialog again. This can be done in a while loop, for example:
FolderBrowserDialog fbd = new FolderBrowserDialog();
while (true)
{
if (fbd.ShowDialog() == DialogResult.OK)
{
if (Valid(fbd.SelectedPath))
break;
else
MessageBox.Show("Something");
}
else
break;
}

Transcendent
- 5,598
- 4
- 24
- 47