自定义MessageBox DialogResult

时间:2016-11-28 03:33:18

标签: c# winforms error-handling messagebox

我有一个带有自定义按钮的自定义c#MessageBox另外,我覆盖了Show()方法,这里是我的大部分代码:

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实例参考,使用类型名称的QualifyIt

所以我该怎么办?

1 个答案:

答案 0 :(得分:1)

您没有将Show方法的结果与DialogResult进行比较。

而不是使用

if (CustomMessageBox.Show("Title", "TITLEEE", CustomMessageBox.CustomMessageBoxButtons.OkCancel) == CustomMessageBox.MsgBox.result.Yes)

尝试使用

if (CustomMessageBox.Show("Title", "TITLEEE", CustomMessageBox.CustomMessageBoxButtons.OkCancel) == DialogResult.Yes)