Search code examples
c#openfiledialog

OpenFileDialog() - How to set the MultiSelect option based on the file filter?


OpenFileDialog() - How to set the MultiSelect option based on the file filter?

My OpenFileDialog can select 2 types of files. This is the Filter used:

"LFA or log files (.lfa, .log)|.lfa;.log"

With MultiSelect property set to false.

New requirement change is: User should be allowed to select multiple log files but only one LFA file.

If I set MultiSelect to true, it will allow select multiple log and lfa files.

Please advise is there any way to implement this feature?


Solution

  • After your answer, if you don't want the UI of the file dialog to close and reopen, you can actually do it. Having your IsValidFileSelection, it should be a matter of doing:

    dlgFileBrowse.FileOk += (s,e) => {
       var dlg = s as OpenFileDialog;
       if (dlg == null) return;
       if (!IsValidFileSelectiom(dlg.FileNames))
       {
          // Or whatever
          MessageBox.Show("Please select one log/lfa file or multiple log files.");
          e.Cancel = true;
       }         
    };
    

    Before calling OpenDialog()