Phuc Coi
Phuc Coi

Reputation: 101

How to use Click event in dynamic groupbox in C#

enter image description here

I have a form like this. You can see my dynamic groupbox (30 groupbox) in Flowlayout on the right, and a picturebox to show questions on the left. I'm worndering that How to use Click event for each groupbox to show the question equivalent in Picturebox?

Here's my code of 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;
    }

And here's the code of 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();

    }

Upvotes: 0

Views: 3367

Answers (2)

Prateek Shrivastava
Prateek Shrivastava

Reputation: 1937

This is an example code:

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

Upvotes: 2

Kevin
Kevin

Reputation: 2631

I would extend the GroupBox control and add a property for the Question ID (Add new Item > Custom Control) and change the class to look like this:

    public partial class QuestionGroupBox : GroupBox
    {
        public string QuestionID;

        public QuestionGroupBox()
        {
            InitializeComponent();
        }

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

Then populate your flowlayout and set the QuestionID and Click EventHandler, similar to this:

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

Finally create your event handler to get and display the question

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

Upvotes: 1

Related Questions