C# 自定义MessageBox对话框结果
我有一个自定义的c#MessageBox,它还带有自定义按钮,我重写了Show()方法,下面是我的大部分代码:C# 自定义MessageBox对话框结果,c#,winforms,error-handling,messagebox,C#,Winforms,Error Handling,Messagebox,我有一个自定义的c#MessageBox,它还带有自定义按钮,我重写了Show()方法,下面是我的大部分代码: public partial class CustomMessageBox : Form { public CustomMessageBox() { InitializeComponent(); } #region Variables public static CustomMessageBox MsgBox; public static Dia
public partial class CustomMessageBox : Form
{
public CustomMessageBox()
{
InitializeComponent();
}
#region Variables
public static CustomMessageBox MsgBox;
public static DialogResult result;
public enum CustomMessageBoxButtons { Ok, OkCancel }
public enum CustomMessageBoxTxtBoxState { VisibleChar, PasswordChar, VisibleCharReadOnly }
#endregion
public static DialogResult Show(string text, string title, CustomMessageBoxButtons buttons)
{
MsgBox = new CustomMessageBox();
MsgBox.txtbox_content.Text = text;
MsgBox.lbl_Title.Text = title;
result = DialogResult.No;
if (buttons == CustomMessageBoxButtons.Ok)
{
MsgBox.btn_ok.Location = new Point(86, 70);
MsgBox.btn_cancel.Visible = false;
}
MsgBox.ShowDialog();
return result;
}
这里是自定义按钮的事件
private void btn_ok_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void btn_cancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void btn_close_Click(object sender, EventArgs e)
{
this.Close();
}
问题就在这里
private void flatButton1_Click(object sender, EventArgs e)
{
if (CustomMessageBox.Show("Title", "TITLEEE", CustomMessageBox.CustomMessageBoxButtons.OkCancel) ==**CustomMessageBox.MsgBox.result.Yes**)
{
CustomMessageBox.Show("Aceptaste", "AGREED", CustomMessageBox.CustomMessageBoxButtons.Ok);
}
else
{
CustomMessageBox.Show("Rechazaste", "dENIED", CustomMessageBox.CustomMessageBoxButtons.Ok);
}
}
#endregion
当我调用messageBox时,它在CustomMessageBox.MsgBox.result.Yes上抛出一个错误
无法使用WinForms实例引用访问,请改为使用类型名称限定它
那么我该怎么办呢?您没有将Show方法的结果与DialogResult进行比较
而不是使用
if (CustomMessageBox.Show("Title", "TITLEEE", CustomMessageBox.CustomMessageBoxButtons.OkCancel) == CustomMessageBox.MsgBox.result.Yes)
试用
if (CustomMessageBox.Show("Title", "TITLEEE", CustomMessageBox.CustomMessageBoxButtons.OkCancel) == DialogResult.Yes)
这样做,并且根本不起作用,DialogResult.Yes
似乎引用了存储在我的主窗体中的本地DialogResult枚举,而不是我的MessageBox枚举,因此使用DialogResult完成的任何表达式。Yes
将不适用于MyMessageBox,那么,Show
方法的返回类型是DialogResult
。因此,Show
方法给出的返回值只能与DialogResult
enum成员进行比较。