user2102327
user2102327

Reputation: 109

add prompt to OpenFileDialog

I am writing a Windows Forms application in which the user is asked to successively select a file corresponding to a task: one specific file for each task. As it works now, the user is prompted to select a file corresponding to the current task and the task name is specified on the previous window, before the OpenFileDialog is invoked. I am afraid that after a number of iterations, the user will not recall the task he is currently selecting the file for. The obvious solution is to present the prompt containing the task name on the same Window with the OpenFileDialog. As the OpenFileDialog is a sealed class, it cannot be inherited from. Is there a way to do it short of coding my own file selection tool?

Upvotes: 0

Views: 586

Answers (1)

Felix D.
Felix D.

Reputation: 5143

You could set the Title of your Dialog to display info on what task is related.

OR

Why not show a MessageBox in advance (sth. like this) ?

MessageBox.Show("Please select file for TASK 00001", "Select File");

//Execution will continue on UserClick on OK on that MessageBox => User should have read 
that message and should know what task is related

OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "SELECT FILE FOR <YOUR TASKNAME GOES HERE>";
if(dialog.ShowDialog() == DialogResult.OK)
{
    string path = dialog.FileName;
    //Continue with your logic
}

enter image description here

UPDATE:

Have a look at the documentation for OpenFileDialog. There is a Event called FileOK which is fired on selecting a file. You could implement sth. like this:

private void Dialog_FileOk(object sender, CancelEventArgs e)
{
    DialogResult result = MessageBox.Show("ARE U SURE ?", "?", MessageBoxButtons.YesNoCancel);
    switch (result)
    {
        case DialogResult.Cancel:
            e.Cancel = true;
            break;
        case DialogResult.Yes:
            break;
        case DialogResult.No:
            e.Cancel = true;
            break;
    }            
}

Don't forget to attach to that event:

dialog.FileOk += Dialog_FileOk;

enter image description here

You could ask "Is this the correct file for TASK XYZ" for example. Also with e.Cancel = true; you can abort the selection and the dialog remains opened.

Upvotes: 2

Related Questions