简体   繁体   中英

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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM