2016-11-28 40 views
1

我有同樣的自定義按鍵的自定義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與類型名

所以我能做些什麼?

回答

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) 
+0

做到這一點,並且不工作在所有的'DialogResult.Yes'似乎被稱爲,存儲在我的主要本地的DialogResult枚舉Form,而不是My MessageBox one,因此使用'DialogResult.Yes'完成的任何表達式對於MyMessageBox都不起作用 – Hydralisk

+0

那麼'Show'方法的返回類型是'DialogResult'。所以,'Show'方法給出的返回值只能與'DialogResult'枚舉成員進行比較。 –

相關問題