tags:

views:

133

answers:

2

Hi, 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.

A: 

No, there isn't.

You could make your own custom dialog form.

SLaks
so does that mean that there is no difference between clicking on the Ok button and clicking on the red cross button on the upper right corner? Doesnt red cross icon mean closing without doing anything?
developer
No, there isn't.
SLaks
+1  A: 

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.");
Nima Rikhtegar
He doesn't want to add any more buttons.
SLaks
if thats the case then as you mentioned eariler, there is no way to solve this problem.
Nima Rikhtegar