多个 GroupBox 中每个按钮的 Button_click 事件(代码中) [英] Button_click event(in code) for every button in multiple GroupBox

查看:37
本文介绍了多个 GroupBox 中每个按钮的 Button_click 事件(代码中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have multiple groupbox, which one contains a question and some answers and button ANSWER in each GroupBox. I create GroupBox and its Controls in Form_Load Method, not manually. How to handle button_click events for every button? I think it's not necessary to write this handle method for every button, because there are only two different button_handler: for questions where there can be only one correct answers, and for questions, where multiple answers can be correct. My questions look like:

我的 GroupBox 结构:

My GroupBox structure:

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

推荐答案

你可以为你的按钮创建一个通用的点击事件:

You can create a general click event for your buttons:

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

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

and in the method, examine the sender to see which button was clicked:

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

之后,您必须检查 GroupBox 子控件的选中值,并使用 questions[bIndex].answers 变量中允许的答案.

After that, you would have to examine the checked values of your GroupBox child controls with the allowable answers in the questions[bIndex].answers variable.

这篇关于多个 GroupBox 中每个按钮的 Button_click 事件(代码中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆