9

First time I'm implementing a FolderBrowserDialog in WPF and I'm not loving it one bit...

Aside from the issues I had figuring out that Windows.Forms wasn't referenced in my project, now I'm having trouble trying to see what the DialogResult return value is...

With an OpenFileDialog, in the past I've done it thusly:

OpenFileDialog ofd = new OpenFileDialog();
Nullable<bool> result = ofd.ShowDialog();

if (result == true)
{
    // all went well, carry on and do your thing here
}

Unfortunately, I'm now getting errors with this saying something about conversions from type DialogResult to bool and whatever have you.

Can't seem to find anything on how to complete this step in using the dialog in WPF, can anyone shed some light?

Thanks in advance!

EDIT

Here's my code as amended without the type conversion error. I'm not sure what value to check result against. Typically I'd use DialogResult.OK except that doesn't appear as a valid value here.

    private void btnBrowse_Click(object sender, RoutedEventArgs e)
    {
        if (cmbTemplate.SelectedItem == "Blockbusters")
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            DialogResult result = fbd.ShowDialog();

            //
            // ERROR: 'System.Nullable<bool>' does not contain a definition for 'OK'
            // and no extention method 'OK' accepting a first argument of type
            // 'System.Nullable<bool>' could be found.
            //
            if (result == DialogResult.OK)
            {
                txtSource.Text = fbd.SelectedPath;
            }
        }
    }
Ortund
  • 8,095
  • 18
  • 71
  • 139
  • 2
    The Windows Forms `Dialog` does not return nullable bool like the WPF dialogs do, but [`DialogResult`](http://msdn.microsoft.com/en-us/library/system.windows.forms.dialogresult.aspx). Just a hint. :) – Patryk Ćwiek Feb 11 '13 at 13:59
  • 1
    If you can't understand the errors about conversions between different types maybe you should go back to OOP and C# basics before trying to do something in WPF, which is a complex framework not suitable for unexperienced developers. – Federico Berasategui Feb 11 '13 at 14:57
  • @HighCore while your suggestion may have been the most pertinent, it's also irrelevant. I understand the error and that's why I tried setting result to a DialogResult object, but that still isn't working in the if statement - hence my confusion – Ortund Feb 12 '13 at 06:40
  • you can use var instead of Nullable, var result = ofd.ShowDialog(); – fhnaseer Feb 12 '13 at 06:49

4 Answers4

17

Okay so it turns out all answers other answers here were right.

They just missed out one thing and I think that was my fault...

Every time I saw DialogResult in Intellisense when trying to use it in my if statement (as I've been told to use, I saw this:

bool? Window.Dialog.Result
Gets or sets the dialog result value, which is the value that is returned from the
System.Windows.Window.ShowDialog() method.

Exceptions:
System.InvalidOperationException

This particular DialogResult object isn't the one I was looking for.

What finally worked was the following:

DialogResult result = fbd.ShowDialog();

if (result == System.Windows.Forms.DialogResult.OK)
{
    // do work here
}

It's worth noting that I do have System.Windows.Forms referenced in my usings which is why I never thought to reference the class from System as in the above snippet. I thought it was using this anyway.

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • As an aside, I highly recommend against doing things like `cmbTemplate.SelectedItem == "Blockbusters"` in WPF. [UI is not Data](http://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#14382137), therefore you should not treat UI elements as data elements and define your program logic based on the state of these elements. – Federico Berasategui Feb 12 '13 at 16:02
  • Well noted and typically I don't, but as I'm new to WPF, I haven't found a better way of doing it (not that I've tried very hard). Although the text for each item in the combobox isn't meant to be changed at any point during execution so I'm not too worried about it really – Ortund Feb 13 '13 at 08:43
0

DialogResult is an enumeration and defines values to indicate the return values of dialogs.

In your code you should check for DialogResult.OK to init your variable with the path selected in the dialog. DialogResult.OK is returned when the "OK"-Button is pressed in the dialog, otherwise is DialogResult.Cancel returned.

if (result == DialogResult.OK){
  txtSource.Text = fbd.SelectedPath;
}
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • As taken from the question: `Typically I'd use DialogResult.OK except that doesn't appear as a valid value here.` – Ortund Feb 12 '13 at 07:12
0

Late answer here but why not just . .

private void SelectFolder()
{
    var dialog = new FolderBrowserDialog();
    var status = dialog.ShowDialog(); // ShowDialog() returns bool? (Nullable bool)
    if (status.Equals(true))
    {
        SelectedFolderPath = dialog.SelectedPath;
    }
}

You can see the result in debugging session. It returns false when the Cancel button is clicked.

flamyoad
  • 519
  • 7
  • 15
-1

DialogResult.(OK, Cancel whatever you want to check),

if (result == DialogResult.OK) // DialogResult.(Your desired result, select from the list it generates)
{
    txtSource.Text = fbd.SelectedPath;
}
fhnaseer
  • 7,159
  • 16
  • 60
  • 112
  • As taken from the question: `Typically I'd use DialogResult.OK except that doesn't appear as a valid value here.` – Ortund Feb 12 '13 at 07:14