3

I am trying to use the Ookii dialog pack to spawn the new Vista style folder selection dialog. That all works with this simple code:

VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
dlg.SelectedPath = Properties.Settings.Default.StoreFolder;
dlg.ShowNewFolderButton = true;
dlg.ShowDialog();

However, I cannot see any way of knowing when the user has selected a folder as there are no events on this object. I could poll for changes in SelectedPath, but that seems a terribly inefficient way of doing things.

Is there some generic C# trick I have missed to enable me to know when a user has selected a folder and therefore update other fields appropriately?

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
Richard Benson
  • 1,477
  • 12
  • 20

2 Answers2

7

Try

VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
dlg.SelectedPath = Properties.Settings.Default.StoreFolder;
dlg.ShowNewFolderButton = true;

if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string path = dlg.SelectedPath;
}
Tobias
  • 35,886
  • 1
  • 20
  • 23
Florian Gl
  • 5,984
  • 2
  • 17
  • 30
  • I was so wrapped up in looking for an event, that I didn't consider the possibility that it might just return a value... thanks! – Richard Benson Sep 28 '12 at 13:36
  • 1
    This does not work (cannot compare bool? with DialogResult) – Everyone Feb 19 '17 at 09:11
  • 4
    Should be: 'if (dlg.ShowDialog() ?? false)' – mdziadowiec May 22 '17 at 14:40
  • 4
    @Everyone The last community edit changed it to compare with WinForms `DialogResult`, which is incorrect here because `ShowDialog()` indeed returns `bool?` when using `Ookii.Dialogs.Wpf.VistaFolderBrowserDialog`(question is tagged WPF). Use either `if (dlg.ShowDialog() == true)` or `if (dlg.ShowDialog() ?? false)`. – Koby Duck Jan 06 '18 at 20:07
-1

This answer uses Ookii.Dialogs.WPF NuGet package you can install from right inside visual studio. At the top left click Project -> Manage NuGet Packages... -> Go to browse tab and search OOkii.Dialogs.Wpf then install it. Now this code will work. :)

You can directly copy paste this.

VistaFolderBrowserDialog FolderSelect = new VistaFolderBrowserDialog(); //This starts folder selection using Ookii.Dialogs.WPF NuGet Package
FolderSelect.Description = "Please select the folder"; //This sets a description to help remind the user what their looking for.
FolderSelect.UseDescriptionForTitle = true;    //This enables the description to appear.        
if ((bool)FolderSelect.ShowDialog(this)) //This triggers the folder selection screen, and if the user does not cancel out...
{                
    TextBoxGameFolder.Text = FolderSelect.SelectedPath; //...then this happens.
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Dawnbomb
  • 1
  • 1
  • 5