0

At this point I am note sure what I am doing wrong. I wrote these lines of code

private void btnBrowse_Click(object sender, RoutedEventArgs e) 
{
    try 
    {
        OpenFileDialog op = new OpenFileDialog();
        op.ShowDialog();
        if (op.ShowDialog() == DialogResult.OK) 
        {
            txtpath.Text = op.FileName;
        }
    }
    catch { }
}

But this isn't working due to an error which states

'bool' does not contain a definition for 'OK'

It should be read out in a listbox.

nvoigt
  • 75,013
  • 26
  • 93
  • 142

2 Answers2

1

It has to be like this. ShowDialog() will block until the dialog is closed.

OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog().GetValueOrDefault()) 
{ 
    txtpath.Text = op.FileName; 
}

Please format your questions correctly and tell us what errors you get (like compiler errors, exceptions, strange behaviour ...).

lorisleitner
  • 688
  • 1
  • 5
  • 20
  • 1
    As nvoigt states in his answer: In WPF the method does _not_ return a DialogResult. See https://msdn.microsoft.com/en-us/library/ms614336(v=vs.110).aspx – Fildor Jan 05 '18 at 13:07
1

ShowDialog returns a bool? in WPF. So:

OpenFileDialog op = new OpenFileDialog();

var result = op.ShowDialog();

if (result.GetValueOrDefault());
{
    txtpath.Text = op.FileName;
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • [MS docs](https://msdn.microsoft.com/en-us/library/ms614336(v=vs.110).aspx): _"In the current implementation, the derived classes (OpenFileDialog and SaveFileDialog) will only return true or false."_ - So `if(op.ShowDialog())` should be enough, right? – Fildor Jan 05 '18 at 13:09
  • 1
    @Fildor It still means that the type is `bool?` and you need to get the value. The compiler does not know that the nullable bool will in fact always be non-null. – nvoigt Jan 05 '18 at 13:12
  • Ah, thanks. Always forgetting that it doesn't work in that direction. – Fildor Jan 05 '18 at 13:15