简体   繁体   English

c#/ WPF openFile对话框打开两次

[英]c#/WPF openFileDialog box opens twice

When I click the button to open a file the OpenFileDialog box opens twice. 当我单击按钮打开文件时, OpenFileDialog框打开两次。 The first box only opens the image files and the second box text files. 第一个框仅打开图像文件,第二个框打开文本文件。 If the user decides to close out the first box without choosing a file the second box pops up as well. 如果用户决定关闭第一个框而不选择文件,则第二个框也会弹出。 I'm not sure what I am over looking on this issue. 我不确定在这个问题上我正在忙些什么。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

OpenFileDialog of = new OpenFileDialog();   
of.Filter = "All Image Formats|*.jpg;*.png;*.bmp;*.gif;*.ico;*.txt|JPG Image|*.jpg|BMP image|*.bmp|PNG image|*.png|GIF Image|*.gif|Icon|*.ico|Text File|*.txt";

if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
   image1.Source = new BitmapImage(new Uri(of.FileName));

    // enable the buttons to function (previous page, next page, rotate left/right, zoom in/out)
    button1.IsEnabled = false;
    button2.IsEnabled = false;
    button3.IsEnabled = false;
    button4.IsEnabled = false;
    button5.IsEnabled = true;
    button6.IsEnabled = true;
    button7.IsEnabled = true;
    button8.IsEnabled = true;                          
}
catch (ArgumentException)
{
    // Show messagebox when argument exception arises, when user tries to open corrupted file
    System.Windows.Forms.MessageBox.Show("Invalid File");
}
 }
 else if(of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 {
try
{
    using (StreamReader sr = new StreamReader(of.FileName))
    {
        textBox2.Text = sr.ReadToEnd();
        button1.IsEnabled = true;
        button2.IsEnabled = true;
        button3.IsEnabled = true;
        button4.IsEnabled = true;
        button5.IsEnabled = true;
        button6.IsEnabled = true;
        button7.IsEnabled = true;
        button8.IsEnabled = true;

    }
}
catch (ArgumentException)
{
    System.Windows.Forms.MessageBox.Show("Invalid File");
}
} 

You're calling ShowDialog() twice: 您两次调用ShowDialog()

if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
       //...
    }
    else if(of.ShowDialog() == System.Windows.Forms.DialogResult.OK)

Just call it once, and save the results: 只需调用一次,然后保存结果:

var dialogResult = of.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK)
{
       //...
}
// Note that the condition was the same!
else if(dialogResult != System.Windows.Forms.DialogResult.OK)

Edit: 编辑:

If you want the second dialog to only show when the first is handled, you can do: 如果要仅在处理第一个对话框时显示第二个对话框,可以执行以下操作:

var dialogResult = of.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK)
{
       //...

    // Do second case here -  Setup new options
    dialogResult = of.ShowDialog(); //Handle text file here
}

You are calling ShowDialog both in the if and else if condition. 您在ifelse if条件中都在调用ShowDialog That method is responsible for showing the dialog and only has the nice side effect of also telling you what the user clicked. 该方法负责显示对话框,并且只具有很好的副作用,也可以告诉您用户单击了什么。

Store the method's result in a variable and check that in your if..elseif conditions instead. 将方法的结果存储在变量中,然后在if..elseif条件中进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM