developer
developer

Reputation: 5478

WPF MessageBox close without any action

I have a confirmation message box for the user in one of my apps. Below is the code for that,

MessageBoxResult res= System.Windows.MessageBox.Show("Could not find the folder, so the D:  Drive will be opened instead.");
                if (res == MessageBoxResult.OK)
                {
                    MessageBox.Show("OK");
                }
              else
                {
               MessageBox.Show("Do Nothing"); 
                }

Now, when the user clicks on the OK button, I want certain code to execute but when they click on the red cross at the upper right corner, I just want the messagebox to close without doing anything. In my case I get 'OK' displayed even when I click on the red cross icon at the upper right corner. Is there a way I can have 'Do Nothing' displayed when I click on the cross. I not want to add any more buttons.

Upvotes: 2

Views: 3452

Answers (2)

user354583
user354583

Reputation:

Yes there is a very simple way, just add the "MessageBoxButtons.OKCancel" param to your MessageBox.Show method. this way you will have two buttons (OK and Cancel). this way if the user clicks the cancel button or the red cross the DialogResult.Cancel message will be returned. the following code described the solution:

System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show("Could not find the folder, so the D:  Drive will be opened instead.", 
            "", System.Windows.Forms.MessageBoxButtons.OKCancel);

        if (result == System.Windows.Forms.DialogResult.OK)
            MessageBox.Show("OK");
        else
            MessageBox.Show("Do nothing.");

Upvotes: 2

SLaks
SLaks

Reputation: 887305

No, there isn't.

You could make your own custom dialog form.

Upvotes: 1

Related Questions