繁体   English   中英

如何在C#中的动态分组框中使用Click事件

[英]How to use Click event in dynamic groupbox in C#

在此处输入图片说明

我有这样的形式。 您可以在右侧的Flowlayout中看到我的动态分组框(30个分组框),在左侧显示图片框以显示问题。 我不知道如何为每个组框使用Click事件以在Picturebox中显示等效问题?

这是我的groupbox代码

        public GroupBox gbx(String name, int s, string i, string msch)
    {
        this.Name = int.Parse(i);
        this.sda = s;
        this.MsCauHoi = msch;

        // gb
        // 
        if (s == 2)
        {
            gb.Controls.Add(cb1);
            gb.Controls.Add(cb2);

        }
        if (s == 3)
        {
            gb.Controls.Add(cb1);
            gb.Controls.Add(cb2);
            gb.Controls.Add(cb3);
        }
        if (s == 4)
        {
            gb.Controls.Add(cb1);
            gb.Controls.Add(cb2);
            gb.Controls.Add(cb3);
            gb.Controls.Add(cb4);
        }

        gb.Location = new System.Drawing.Point(219, 44);
        gb.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1);
        gb.Name = name;
        gb.Padding = new System.Windows.Forms.Padding(1, 1, 1, 1);
        gb.Size = new System.Drawing.Size(120, 45);
        gb.TabIndex = 7;
        gb.TabStop = false;
        gb.Text = i;
        gb.BackColor = Color.Silver;


        // 
        // cb1
        // 
        cb1.AutoSize = true;
        cb1.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
        cb1.Location = new System.Drawing.Point(15, 13);
        cb1.Margin = new System.Windows.Forms.Padding(0);
        cb1.Name = "cb1";
        cb1.Size = new System.Drawing.Size(17, 31);
        cb1.TabIndex = 0;
        cb1.Text = "1";
        cb1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        cb1.UseVisualStyleBackColor = true;
        cb1.CheckedChanged += delegate (object sender, EventArgs e)
        {

            CheckBox b = new CheckBox();
            b = (CheckBox)sender;
            if (b.Checked == true)
            {
                gb.BackColor = Color.Turquoise;

            }
        };
        // 
        // cb2
        // 
        cb2.AutoSize = true;
        cb2.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
        cb2.Location = new System.Drawing.Point(35, 13);
        cb2.Name = "cb2";
        cb2.Size = new System.Drawing.Size(17, 31);
        cb2.TabIndex = 1;
        cb2.Text = "2";
        cb2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        cb2.UseVisualStyleBackColor = true;
        cb2.CheckedChanged += delegate (object sender, EventArgs e)
        {

            CheckBox b = new CheckBox();
            b = (CheckBox)sender;
            if (b.Checked == true)
            {
                gb.BackColor = Color.Turquoise;

            }
        };
        // 
        // cb3
        // 
        cb3.AutoSize = true;
        cb3.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
        cb3.Location = new System.Drawing.Point(58, 13);
        cb3.Name = "cb3";
        cb3.Size = new System.Drawing.Size(17, 31);
        cb3.TabIndex = 2;
        cb3.Text = "3";
        cb3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        cb3.UseVisualStyleBackColor = true;
        cb3.CheckedChanged += delegate (object sender, EventArgs e)
        {

            CheckBox b = new CheckBox();
            b = (CheckBox)sender;
            if (b.Checked == true)
            {
                gb.BackColor = Color.Turquoise;

            }

        };
        // 
        // cb4
        // 
        cb4.AutoSize = true;
        cb4.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
        cb4.Location = new System.Drawing.Point(81, 13);
        cb4.Name = "cb4";
        cb4.Size = new System.Drawing.Size(17, 31);
        cb4.TabIndex = 3;
        cb4.Text = "4";
        cb4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        cb4.UseVisualStyleBackColor = true;
        cb4.CheckedChanged += delegate (object sender, EventArgs e)
        {

            CheckBox b = new CheckBox();
            b = (CheckBox)sender;
            if (b.Checked == true)
            {
                gb.BackColor = Color.Turquoise;

            }
        };
        return gb;
    }

这是FormLoad的代码

       private void FrmThi_Load(object sender, EventArgs e)
    {
        droptable();
        this.CauDaLam = 0;
        if (dethi == null) setdethi();
        Screen scr = Screen.PrimaryScreen; //đi lấy màn hình chính
        this.Left = (scr.WorkingArea.Width - this.Width) / 2;
        this.Top = (scr.WorkingArea.Height - this.Height) / 2;
        int i = 0;
        foreach (DataRow row in this.dethi.Rows)
        {
            String myValue = row["SoDA"].ToString();
            String msch = row["MaCH"].ToString();
            ptl = new FrmPhieuTraLoi();
            pn_DeThi.Controls.Add(ptl.gbx("cau" + (i + 1).ToString(), int.Parse(myValue), (i + 1).ToString(), msch));
            listptl.Add(ptl);
            i++;
        }
        loadcauhoi(this.CauDangLam);
        listptl[CauDangLam].setBackColorCDL();

        Random r = new Random();
        lbmade1.Text = r.Next(1, 4).ToString();
        txt = lbSatHachBangLai.Text;
        len = txt.Length;
        lbSatHachBangLai.Text = "";
        timer1.Start();
        this.timer2.Start();

    }

这是一个示例代码:

private void Form1_Load(object sender, EventArgs e)
{
    for(int idx = 0; idx < 5; idx++)
    {
        var gBox = new GroupBox();
        gBox.Height = 50;
        gBox.Width = 50;
        gBox.Text = "Box: " + idx;
        gBox.Tag = idx;

        gBox.MouseClick += GBox_MouseClick;

        this.Controls.Add(gBox);
    }
}

private void GBox_MouseClick(object sender, MouseEventArgs e)
{
    //var ctrl = sender as Control; -- Not required
    int questionIdx = (int)(sender as Control).Tag;            
}

我将扩展GroupBox控件并为问题ID添加属性(添加新项>自定义控件),然后将类更改为如下所示:

    public partial class QuestionGroupBox : GroupBox
    {
        public string QuestionID;

        public QuestionGroupBox()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }

然后填充您的流程布局并设置QuestionID并单击EventHandler,类似于此:

 var gb = new QuestionGroupBox{  QuestionID = "44"};
 gb.Click += new System.EventHandler(this.questionGroupBox_Click);
 //add add to your flow layout

最后创建事件处理程序以获取并显示问题

private void questionGroupBox_Click(object sender, EventArgs e)
{
   var questionID = ((QuestionGroupBox)sender).QuestionID;
   //display your question
}

暂无
暂无

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

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