Search code examples
c#winformserror-handlingmessagebox

Custom MessageBox DialogResult


I have a custom c# MessageBox with custom Buttons Also, and I overrode Show() method, here is the most of my code:

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;
}

Here the Custom Button's Events

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();
}

Problem is Here

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

When I call my messageBox it throws me an error on CustomMessageBox.MsgBox.result.Yes saying

Cannot be accessed with a WinForms instance Reference, QualifyIt with a type Name Instead

so What can I do ?


Solution

  • You are not comparing result of Show method with DialogResult.

    Instead of using

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

    Try using

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