自定义的MessageBox的DialogResult
问题描述:
我有同样的自定义按键的自定义C#MessageBox的,和我推翻Show()方法,这里是我的大部分代码:自定义的MessageBox的DialogResult
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
当我打电话给我的留言专栏它抛出我的CustomMessageBox.MsgBox.result.Yes
一个错误说
无法与一个WinForms实例引用来访问,QualifyIt与类型名
所以我能做些什么?
答
您没有将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枚举Form,而不是My MessageBox one,因此使用'DialogResult.Yes'完成的任何表达式对于MyMessageBox都不起作用 – Hydralisk
那么'Show'方法的返回类型是'DialogResult'。所以,'Show'方法给出的返回值只能与'DialogResult'枚举成员进行比较。 –