繁体   English   中英

多个GroupBox中每个按钮的Button_click事件(以代码形式)

[英]Button_click event(in code) for every button in multiple GroupBox

我有多个groupbox ,其中一个包含了一个问题,一些答案和button在每个答案GroupBox 我在Form_Load方法中创建了GroupBox及其控件,而不是手动创建。 如何处理每个button button_click事件? 我认为没有必要为每个button编写此handle方法,因为只有两个不同的button_handler:对于只有一个正确答案的问题,对于多个答案可以正确的问题。 我的问题如下:

我的GroupBox结构:

int loc = 20;
            for (int i = 0; i < 10; i++)
            {
                GroupBox gb = new GroupBox();
                gb.Name = "GroupBox" + (i + 1);
                gb.Size = new Size(500, 200);
                gb.Location = new Point(40, loc);
                gb.BackColor = System.Drawing.Color.Aquamarine;

                Label q_text = new Label(); // текст питання
                q_text.Name = "label" + (i + 1);
                q_text.Text = "Питання" + (i + 1);
                q_text.Font = new Font("Aria", 10, FontStyle.Bold);
                q_text.Location = new Point(10, 10);
                gb.Controls.Add(q_text);
                int iter = q_text.Location.Y + 30;
                if (i <= 5)
                {
                    foreach (string key in questions[i].answers.Keys)
                    {
                        RadioButton rb = new RadioButton();
                        rb.Text = key;
                        rb.Size = new Size(120, 25);
                        rb.Location = new Point(q_text.Location.X + 10, iter);
                        iter += 30;
                        gb.Controls.Add(rb);
                    }
                }else
                    if (i > 5)
                    {
                        foreach (string key in questions[i].answers.Keys)
                        {
                            CheckBox rb = new CheckBox();
                            rb.Text = key;
                            rb.Size = new Size(120, 25);
                            rb.Location = new Point(q_text.Location.X + 10, iter);
                            iter += 30;
                            gb.Controls.Add(rb);
                        }

                    }


                Button b = new Button();
                b.Name = "button" + (i + 1);
                b.Text = "Answer";
                b.Location = new Point(gb.Size.Width - 120, gb.Size.Height - 30);
                gb.Controls.Add(b);
                this.Controls.Add(gb);
                loc += 200;
            }

您可以为按钮创建常规点击事件:

Button b = new Button();
b.Name = "button" + (i + 1).ToString();
b.Click += b_Click;

在该方法中,检查发件人以查看单击了哪个按钮:

void b_Click(object sender, EventArgs e) {
  Button b = sender as Button;
  if (b != null) {
    GroupBox gp = b.Parent as GroupBox;
    int bIndex = Convert.ToInt32(b.Name.Substring(6)) - 1;
    MessageBox.Show(string.Format("I'm clicking {0}, question index #{1}",
                    gp.Name, bIndex));        
  }
}

在那之后,您将不得不检查questions[bIndex].answers变量中允许的答案以及GroupBox子控件的检查值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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