简体   繁体   English

如何获取,在C#Winform应用程序中单击表单上的哪个按钮

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

I have a winform form that asks user, whether to save the changes or not, the form contains two buttons YES or NO, Now this form is poped when user fills any details in another form when leaving the unsaved changes, I ask to user whether to save the unsaved data or not. 我有一个winform表单,询问用户是否保存更改,该表单包含两个按钮“是”或“否”,现在当用户在保留未保存的更改时在另一个表单中填写任何详细信息时会弹出此表单,请问用户是否保存未保存的数据。 Now on the basis of my messagesBox form dialog result if user types YES i save the changes, if he types NO unsaved changes are discarded and if he clicks [X] button of messageBox form then nothing is done. 现在,根据我的messagesBox表单对话框的结果,如果用户输入YES,我将保存更改,如果他键入NO,则未保存的更改将被丢弃,并且如果他单击messageBox表单的[X]按钮,则不会执行任何操作。

messageBox表单

What i have tried so far: 到目前为止我尝试过的是:

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

The main problem lies with clicking of [X] button on messageBox form. 主要问题在于单击messageBox表单上的[X]按钮。

My question is simple how can i get the button names (ie Which button in clicked on form), so that is can use its Dialog result in my main form. 我的问题很简单,我如何获取按钮名称(即单击表单中的哪个按钮),以便可以在主表单中使用其Dialog结果。 Thankyou! 谢谢!

You can just create a bool variable, lets say choice, in your Form_MessageBox class and in the click event handlers of yes and no buttons, set it to true when user clicks yes and to false when user clicks no. 您可以在Form_MessageBox类以及yes和no按钮的click事件处理程序中创建一个bool变量(可以说是选择),在用户单击yes时将其设置为true,在用户单击no时将其设置为false。 AND in your Main form you can just do like this: 在主表单中,您可以这样做:

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