Get return value from pressed button

夙愿已清 提交于 2019-12-04 17:13:07

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!