Prevent Error Message from Appearing When Hitting Cancel of Opening a File

user2781018

This seems so simple, but I have been trying to get this to work properly, but I just can't. Here is the original code:

ofd = new OpenFileDialog();

if (ofd.ShowDialog() == DialogResult.OK)
{
     mFileName = ofd.FileName;
}

Problem is, if the user hits the cancel button on the dialog box, then a bunch of my other code continues to execute, such as opening the database connection and such, which is unnecessary since the user selected cancel. So I then tried:

if (ofd.ShowDialog() == DialogResult.Cancel)
{
    return;
}
else if (ofd.ShowDialog() == DialogResult.OK)
{
    mFileName = ofd.FileName;
}

While this prevents the problem before, another arises. If the user selects cancel, it stops. But then it will open a second dialog box, which it shouldn't.

I can't seem to only open one dialog box, if the user hits cancel return, else if the user hits open, then it continues. Thanks.

CodingIntrigue

The problem here is that you are calling ShowDialog() multiple times. It returns a DialogResult enum so you can store it in a variable the like this:

DialogResult result = ofd.ShowDialog();

Then you can work on it multiple times without having to show the dialog again:

if(result == DialogResult.OK) {
    // OK!
}
else if(result == DialogResult.Cancel) {
    return; // Exit function
}
else {
    // Anything else you need to do
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error when "Cancel" while opening a file in PyQt4

From Dev

Excel VBA How to prevent user from hitting cancel in msoFileDialogSaveAs

From Dev

Prevent VSTO Office Customisation installer prompt from appearing when Opening a document

From Dev

Prevent cat from opening a binary file

From Dev

Error when opening saved file

From Dev

Throwing error message when opening terminal

From Dev

Error message when opening new window in Tkinter

From Dev

Throwing error message when opening terminal

From Dev

How do I Prevent the file manager from opening when I click to open a file from outside the file manager

From Dev

Error message when opening the terminal "-bash: /usr/bin/tclsh: No such file or directory"

From Dev

x-nautilus-desktop error message when opening Nautilus from desktop

From Dev

Prevent new session when opening picture file with PHP

From Dev

howto prevent gedit always opening a new unnamed file when launched

From Dev

Error message when clicking Exit or Cancel in "Application.GetOpenFilename"

From Dev

Encoding error when opening an Excel file with xlrd

From Dev

Emacs polymode gives error when opening file

From Dev

Error when opening file for reading in c#

From Dev

XML file to Excel, error when opening

From Dev

phpexcel Fatel Error when opening file

From Dev

Cryptic error in lxml when opening file

From Dev

Memory error when opening the file with read()

From Java

Prevent users from submitting a form by hitting Enter

From Dev

Eclipse: Prevent "File Search" from opening different files in the same tab

From Dev

How to prevent notification from opening an activity or removed when clicked in Android?

From Dev

How to prevent AndroidStudio from opening LogCat (tab) when pressing "Run"?

From Dev

prevent bootstrap modal from opening when a button is clicked

From Dev

How to prevent USC from opening a terminal when updating the cache?

From Dev

Prevent Excel from converting values to dates when opening HTML files

From Dev

How to prevent Preview from opening files when it launches

Related Related

  1. 1

    Error when "Cancel" while opening a file in PyQt4

  2. 2

    Excel VBA How to prevent user from hitting cancel in msoFileDialogSaveAs

  3. 3

    Prevent VSTO Office Customisation installer prompt from appearing when Opening a document

  4. 4

    Prevent cat from opening a binary file

  5. 5

    Error when opening saved file

  6. 6

    Throwing error message when opening terminal

  7. 7

    Error message when opening new window in Tkinter

  8. 8

    Throwing error message when opening terminal

  9. 9

    How do I Prevent the file manager from opening when I click to open a file from outside the file manager

  10. 10

    Error message when opening the terminal "-bash: /usr/bin/tclsh: No such file or directory"

  11. 11

    x-nautilus-desktop error message when opening Nautilus from desktop

  12. 12

    Prevent new session when opening picture file with PHP

  13. 13

    howto prevent gedit always opening a new unnamed file when launched

  14. 14

    Error message when clicking Exit or Cancel in "Application.GetOpenFilename"

  15. 15

    Encoding error when opening an Excel file with xlrd

  16. 16

    Emacs polymode gives error when opening file

  17. 17

    Error when opening file for reading in c#

  18. 18

    XML file to Excel, error when opening

  19. 19

    phpexcel Fatel Error when opening file

  20. 20

    Cryptic error in lxml when opening file

  21. 21

    Memory error when opening the file with read()

  22. 22

    Prevent users from submitting a form by hitting Enter

  23. 23

    Eclipse: Prevent "File Search" from opening different files in the same tab

  24. 24

    How to prevent notification from opening an activity or removed when clicked in Android?

  25. 25

    How to prevent AndroidStudio from opening LogCat (tab) when pressing "Run"?

  26. 26

    prevent bootstrap modal from opening when a button is clicked

  27. 27

    How to prevent USC from opening a terminal when updating the cache?

  28. 28

    Prevent Excel from converting values to dates when opening HTML files

  29. 29

    How to prevent Preview from opening files when it launches

HotTag

Archive