Keep opening OpenFileDialog until selecting valid

2019-03-31 08:40发布

问题:

I have code that opens the OpenFileDialog, I'm checking the size of the file to make sure it doesn't exceed specific limit. But, if the user selected a big sized file I need to warn him and lead him back to the dialog to select a different file or click cancel.

This is what I've 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 the dialog each time the ShowDialog is called. So, if the user selected a file 3x the size the limit, the dialog 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");
        }

回答1:

You are half-way there, the FileOk event is what you want to use. What you are missing is setting the e.Cancel property to true. That keeps the dialog opened and avoids you having to display it over and over 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");
        }


回答2:

ev.Cancel = true; Check if following piece of code serves your purpose?

    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;
          }

    }


回答3:

Yes as far as your requirement is concern, this is OK but in general opening Dialog after showing a prompt for Size is not the best way. Instead a prompt should be displayed, best is to display the validation error on the size from the main window. And it should be User's duty to select the proper file again by opening the File Dialog again according to Usability principles of HCI.



回答4:

Add a handler to FileDialog.FileOk and let verify the file size inside their.