Continue to open OpenFileDialog until you select the correct file

I have code that opens OpenFileDialog, I check the file size to make sure that it does not exceed a certain limit. But if the user has selected a large file, I need to warn him and bring him back to the dialog box to select another file, or click the "Cancel" button.

Here is what I tried:

OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Jpeg files, PDF files, Word files|*.jpg;*.pdf;*.doc;*.docx"; while (dialog.ShowDialog() != DialogResult.Cancel) { var size = new FileInfo(dialog.FileName).Length; if (size > 250000) { MessageBox.Show("File size exceeded"); continue; } } 

EDIT: I also tried the following code, but it opens a dialog every time ShowDialog is called. Thus, if the user selects a file whose size is 3 times the limit, a dialog box will appear 3 times.

  OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Jpeg files, PDF files, Word files|*.jpg;*.pdf;*.doc;*.docx"; dialog.FileOk += delegate(object s, CancelEventArgs ev) { var size = new FileInfo(dialog.FileName).Length; if (size > 250000) { XtraMessageBox.Show("File size"); dialog.ShowDialog(); } }; if (dialog.ShowDialog() == DialogResult.OK) { XtraMessageBox.Show("File Selected"); } 
+4
source share
4 answers

You're halfway, the FileOk event is what you want to use. What you are missing sets the e.Cancel property to true. This keeps the dialog open and allows you to display it again and again. Like this:

  OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Jpeg files, PDF files, Word files|*.jpg;*.pdf;*.doc;*.docx"; dialog.FileOk += delegate(object s, CancelEventArgs ev) { var size = new FileInfo(dialog.FileName).Length; if (size > 250000) { MessageBox.Show("Sorry, file is too large"); ev.Cancel = true; // <== here } }; if (dialog.ShowDialog() == DialogResult.OK) { MessageBox.Show(dialog.FileName + " selected"); } 
+7
source

ev.Cancel = true; Check if the next part of the code matches?

  public void SomeMethod() { OpenFileDialog dialog = new OpenFileDialog(); dialog.FileOk += new CancelEventHandler(dialog_FileOk); dialog.Filter = "Jpeg files, PDF files, Word files|*.jpg;*.pdf;*.doc;*.docx"; dialog.ShowDialog(); } void dialog_FileOk(object sender, CancelEventArgs e) { OpenFileDialog dialog = sender as OpenFileDialog; var size = new FileInfo(dialog.FileName).Length; if (size > 250000) { MessageBox.Show("File size exceeded"); e.Cancel = true; } } 
+1
source

Yes, how important your requirement is, this is normal, but in general, opening a dialog box after showing a hint for the Size parameter is not the best way. Instead, a tooltip should be displayed; it is best to display a size check error from the main window. And the user must select the correct file again by opening the File dialog again in accordance with the principles of HCI usability.

0
source

Add a FileDialog.FileOk handler and check the file size inside them.

0
source

Source: https://habr.com/ru/post/1438216/


All Articles