簡體   English   中英

如何獲取,在C#Winform應用程序中單擊表單上的哪個按鈕

[英]how to get, which button is clicked on form in c# winform application

我有一個winform表單,詢問用戶是否保存更改,該表單包含兩個按鈕“是”或“否”,現在當用戶在保留未保存的更改時在另一個表單中填寫任何詳細信息時會彈出此表單,請問用戶是否保存未保存的數據。 現在,根據我的messagesBox表單對話框的結果,如果用戶輸入YES,我將保存更改,如果他鍵入NO,則未保存的更改將被丟棄,並且如果他單擊messageBox表單的[X]按鈕,則不會執行任何操作。

messageBox表單

到目前為止我嘗試過的是:

 public partial class Form_MessageBox : Form
    {

    public static bool checkedLiveviewAlso=false;       
    private static Form_MessageBox msgBox;
    private static readonly SoundPlayer obj_SoundPlayer = new SoundPlayer();
    private DialogResult dialogResult;
    private string buttonName = "Cancel";
    private bool Flag = false;

    public static Form_MessageBox MSGBOX
    {
        get { return msgBox; }
    }
    public string ButtonName
    {
        get { return buttonName;}   set{ buttonName = value;}
    }

    private static  Form_MessageBox GetFormMessageBox()
    {
        if (msgBox == null)
        {
            msgBox = new Form_MessageBox();

        }
        msgBox.vmS_ApplySameLiveview.Visible = false;
        return msgBox;
    }


    //Yes button event handler
    private void bt_Yes_Click(object sender, EventArgs e)
    {
        ButtonName = "Yes";
        Flag = true;
        msgBox.dialogResult = DialogResult.Yes;
        msgBox.Focus();
        msgBox.Close();
    }

    //No button click event handler.
    private void bt_No_Click(object sender, EventArgs e)
    {
        ButtonName = "No";
        Flag = true;
        msgBox.dialogResult = DialogResult.No;
        msgBox.Focus();
        msgBox.Close();
    }

     //Form Closing event handler
     private void Form_MessageBox_FormClosing(object sender, FormClosingEventArgs e)
    {
        if(sender is Form_MessageBox)
        {
            ButtonName = "Cancel";
            msgBox.DialogResult = DialogResult.Cancel;
            //msgBox.Close();
        }
        //msgBox.Visible = true;
        //e.Cancel = true;
    }

   //Static show method of messageBox form which is called my another form.
   public static DialogResult Show(string message, string titleText, MessageBoxButtons msgBoxButtons, MessageBoxIcon icon)
    {
        msgBox = GetFormMessageBox();// new Form_MessageBox();
        try
        {


            Form_Loading.CloseForm();
           // string Message = locRM.GetString(message);

                msgBox.set_message_property(message.Trim(), titleText, msgBoxButtons, icon);

            msgBox.ShowDialog();

            return msgBox.dialogResult;
        }

        finally
        {
            //msgBox.Dispose();
        }
    }

主要問題在於單擊messageBox表單上的[X]按鈕。

我的問題很簡單,我如何獲取按鈕名稱(即單擊表單中的哪個按鈕),以便可以在主表單中使用其Dialog結果。 謝謝!

您可以在Form_MessageBox類以及yes和no按鈕的click事件處理程序中創建一個bool變量(可以說是選擇),在用戶單擊yes時將其設置為true,在用戶單擊no時將其設置為false。 在主表單中,您可以這樣做:

if(msgBox.choice)
{ 
    //perform operation for yes here
}
else
{ 
    //Perform operations for no here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM