简体   繁体   English

打开文件对话框并打开其他文件

[英]Open File Dialog and Opening different File

I am working on Opening a file(s). 我正在打开文件。 Requirement is, if user select Text in the filter, user can select mulitiple files. 要求是,如果用户在过滤器中选择“文本”,则用户可以选择多个文件。 But if user selects any image files, user is allowed to select single file. 但是,如果用户选择任何图像文件,则允许用户选择单个文件。

One way of doing is, capturing dialog.FileOk deleagate and intial checks. 一种方法是捕获dialog.FileOk释放和初始检查。

Do we have any other ways of achiving the same. 我们还有其他方法可以实现相同目标吗?

Thank you, 谢谢,

OpenFileDialog does not provide events for FileSelected, or FilterChanged. OpenFileDialog不为FileSelected或FilterChanged提供事件。 So, with default OpenFileDialog you can only subscribe to FileOk event and cancel or accept it after user made selections. 因此,使用默认的OpenFileDialog,您只能订阅FileOk事件,并在用户做出选择后取消或接受它。 If you want to change settings on the fly (eg disabling multiple files selection when user choose images in filter) then you should extend OpenFileDialog . 如果您想即时更改设置(例如,当用户在过滤器中选择图像时禁用多个文件选择),则应扩展OpenFileDialog Unfortunately default one is sealed, thus you can't just inherit from that. 不幸的是,默认值是密封的,因此您不能仅仅继承它。 So, take a look on these articles: 因此,请看以下文章:

Use the following code: 使用以下代码:

private void button1_Click(object sender, System.EventArgs e)
{
    openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
    openFileDialog1.FilterIndex = 1;

   if(openFileDialog1.ShowDialog() == DialogResult.OK)
   {
      System.IO.StreamReader sr = new 
         System.IO.StreamReader(openFileDialog1.FileName);
      MessageBox.Show(sr.ReadToEnd());
      sr.Close();
   }

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

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