Get return value from pressed button

2020-07-24 16:18发布

问题:

I have a form that pops up at a specific event. It draws up buttons from an array and sets the Tag value to a specific value. So if you are to press or click this button the function should return the Tag value.

How can I do this? And how do I know which button was clicked? At this moment the code returns DialogResult, but I want to return the Tag value from the function. How shall I modify my code so that it can do this?

public static DialogResult SelectBox(string title, string[] btnArray, string[] btnValueArray)
{
    Form form = new Form();

    Button[] buttonArray;
    buttonArray = new Button[5];

    form.Text = title;

    for (int i = 0; i < btnArray.Length; i++)
    {
        buttonArray[i] = new Button();
        buttonArray[i].Text = btnArray[i];
        buttonArray[i].Tag = new int();
        buttonArray[i].Tag = btnValueArray[i];

        buttonArray[i].TabStop = false;
        buttonArray[i].Location = new System.Drawing.Point(0, i * 40);
        buttonArray[i].Size = new System.Drawing.Size(240, 40);
    }

    form.ClientSize = new Size(240, 268);
    form.Controls.AddRange(new Control[] { buttonArray[0], buttonArray[1], buttonArray[2] });
    form.FormBorderStyle = FormBorderStyle.FixedDialog;
    form.StartPosition = FormStartPosition.CenterScreen;
    form.MinimizeBox = false;
    form.MaximizeBox = false;

    DialogResult dialogResult = form.ShowDialog();
    return dialogResult;
}

回答1:

Add a private variable in the form:

private object SelectedTag;

Add a button click handler:

private void Button_Click(object sender, EventArgs e) {
    SelectedTag = ((Button)sender).Tag;
}

Assign the handler to each button you create:

..
buttonArray[i].OnClick += form.Button_Click;
..

Then in your static function, simply return form.SelectedTag instead of the dialogresult.



回答2:

You could call the same click event for all buttons. then in your handler:

private void ButtonClick(object sender, EventArgs args)
{
  Button oButton = (Button) sender;

  object data = oButton.Tag;
}


回答3:

The DialogResult property already tells you which button was clicked. You can set each individual button to return a different DialogResult, and then check for that at the bottom of the function.

And if you want to return the clicked button's Tag property instead, you need to change the function's return value to Object (because the Tag property is of type Object).



回答4:

You can add a ButtonClick event handler in a TestForm, set the button's tag to the Form's tag. Here is the sample.

Main Form:

        private void Form1_Load(object sender, EventArgs e)
        {
            Object tag;
            SelectBox("test", new String[] { "One", "Two", "Three" }, new String[] {"one value", "Two value", "three value" }, out tag);
            MessageBox.Show(tag.ToString());
        }

        public static DialogResult SelectBox(string title, string[] btnArray, string[] btnValueArray, out Object tagValue)
        {
            TestForm form = new TestForm();

            Button[] buttonArray;
            buttonArray = new Button[5];

            form.Text = title;

            for (int i = 0; i < btnArray.Length; i++)
            {
                buttonArray[i] = new Button();
                buttonArray[i].Text = btnArray[i];
                buttonArray[i].Tag = new int();
                buttonArray[i].Tag = btnValueArray[i];

                buttonArray[i].TabStop = false;
                buttonArray[i].Location = new System.Drawing.Point(0, i * 40);
                buttonArray[i].Size = new System.Drawing.Size(240, 40);
                // subscribe to button click event..
                // the handler is in TestForm
                buttonArray[i].Click += form.HandleOnButtonClick;
            }

            form.ClientSize = new Size(240, 268);
            form.Controls.AddRange(new Control[] { buttonArray[0], buttonArray[1], buttonArray[2] });
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;

            DialogResult dialogResult = form.ShowDialog();
            // set the out args value
            tagValue = form.Tag;

            return dialogResult;
        }

TestForm that whose instance we create in the SelectBox dialog:

public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }      

        public void HandleOnButtonClick(Object sender, EventArgs e)
        {
            Button button = sender as Button;

            if (button != null)
                this.Tag = button.Tag;
        }
    }

Edit:

If you want to capture every button's value then expose a Dictionary<String, Object> Tags property.