如何从我制作的消息框中返回是或否?

Gal*_*ali 2 c# winforms

我制作了一个消息框,一个包含消息标签和标题标签的表单.

和一个OK按钮(什么都不做).

我可以从我的程序中的任何形式上升此消息框.

我需要一个带有Yes按钮和No按钮的消息框,并知道它是否按下了,Yes或者No我该怎么办?

这样的代码示例是什么?

Aus*_*nen 16

来自MSDN MessageBox.Show(...):

// Initializes the variables to pass to the MessageBox.Show method.

string message = "You did not enter a server name. Cancel this operation?";
string caption = "Error Detected in Input";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;

// Displays the MessageBox.

result = MessageBox.Show(message, caption, buttons);

if (result == System.Windows.Forms.DialogResult.Yes)
{
    // Closes the parent form.
    this.Close();
}
Run Code Online (Sandbox Code Playgroud)


Gab*_*ana 6

在按钮的点击事件处理程序中使用

this.DialogResult = System.Windows.Forms.DialogResult.Yes;
Run Code Online (Sandbox Code Playgroud)

代替

this.Close();
Run Code Online (Sandbox Code Playgroud)

然后在您的调用代码中正常处理它。