简体   繁体   中英

How can I programmatically click a button on a MessageBox

I have a radiobutton when i check it, it should give a message

DialogResult click = MessageBox.Show("Would you like to convert the actual values to US Customary ?\n Clicking No changes just the unit system.", "Change Unit Systems to US Customary", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

if (click == DialogResult.Yes)
{
    //some code 
}

how can I answer this message from internal code in a button to be NO?

I don't want the message to appear

If you seriously don't want it then just perform your action when the check changes for that radio button.

private void radioButton1_CheckedChanged(Object sender, EventArgs e)
{
   //Validate to make sore it was not just deselected
   if (radioButton1.Checked)
   {
     //Do whatever I want when the use checks this radio button
   {
}

When you click on radio button you send the appropiate delegate function(action) and Messagebox result. The action handles yes/no cases.

public void HandleMessageBoxResult(DialogResult result, Action<DialogResult> action)
{
    action(result);
}


public void Radio1Action(DialogResult result)
{
    switch (result)
    {
        case DialogResult.Yes:
            MessageBox.Show("Yes Clicked for Radio1");
            break;
        case DialogResult.No:
            MessageBox.Show("No Clicked for Radio1");
            break;
    }
}

public void Radio2Action(DialogResult result)
{
    switch (result)
    {
        case DialogResult.Yes:
            MessageBox.Show("Yes Clicked for radioButton2");
            break;
        case DialogResult.No:
            MessageBox.Show("No Clicked for radioButton2");
            break;
    }
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    if(radioButton1.Checked)
    {
        DialogResult result = MessageBox.Show("Message for radioButton1", "title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
        HandleMessageBoxResult(result, Radio1Action);
    }
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton2.Checked)
    {
        DialogResult result = MessageBox.Show("Message for radioButton2", "title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
        HandleMessageBoxResult(result, Radio2Action);
    }
}

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