开发者

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.


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.");


No, there isn't.

You could make your own custom dialog form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜