remi coupé
remi coupé

Reputation: 9

How to open a text file in button click event

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.

Upvotes: 0

Views: 1479

Answers (2)

lorisleitner
lorisleitner

Reputation: 698

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 ...).

Upvotes: 1

nvoigt
nvoigt

Reputation: 77285

ShowDialog returns a bool? in WPF. So:

OpenFileDialog op = new OpenFileDialog();

var result = op.ShowDialog();

if (result.GetValueOrDefault());
{
    txtpath.Text = op.FileName;
}

Upvotes: 1

Related Questions