Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
Dear All
 
I am trying to create groupbox dynamically and these GB will create on selection on checkbox.
 
Means I have 5 check boxes now if I select 1st CB then 1 GB with some other dynamic checkbox shall be created, if I select 3rd check then another GB shall be created with few more other checkboxes.
For this I am trying with this code whee I am able to create dynamic checkboxes for fixed groupbox which is already created at design time.
 
My scenario is - 5 Branches have multiple batches. Now user will choose branch from dynamically checkboxes and on that basis btaches will be displayed in groupbox for each branch.
Branch1 Branch2 Branch3 Branch4 Branch5 if user select 3rd and 5th branch then GB1 will show Branch3's Batches and GB2 shall show Branch5's Batches
Here is the code -
 
//Check Box for Branches                
        private void RO_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox1.Controls.Clear();
            String m = RO.SelectedItem.ToString();
            Console.WriteLine(m);
            aCommand2 = new OleDbCommand("select * from branch_tbl,region_tbl where branch_tbl.region_id=region_tbl.region_id and region_tbl.region_name LIKE '"+m +"'", main_connection);
            aAdapter2 = new OleDbDataAdapter(aCommand2);
            ds2 = new DataSet();
            aAdapter2.Fill(ds2, "app_info");
            ds2.Tables[0].Constraints.Add("pk_bno", ds2.Tables[0].Columns[0], true);
            int bran_count = ds2.Tables[0].Rows.Count;
            Console.WriteLine(bran_count);
            checkBox = new System.Windows.Forms.CheckBox[bran_count];
            for (int i = 0; i < bran_count; ++i)
            {
                checkBox[i] = new CheckBox();
                checkBox[i].Name = "radio" + Convert.ToString(i);
                checkBox[i].Text = ds2.Tables[0].Rows[i][2].ToString();
                checkBox[i].Location = new System.Drawing.Point(125 * i, 15);
                groupBox1.Controls.Add(checkBox[i]);
                checkBox[i].CheckStateChanged += new System.EventHandler(CheckBoxCheckedChanged);
            }
        }
        int count = 1;
        int position = 1;
        //Code for handling event when branch check box is selected or unselected
        private void CheckBoxCheckedChanged(object sender, EventArgs e)
        {
            CheckBox c = (CheckBox)sender;
            //Label myLabel;
            String str = null;
            if (c.Checked == true)
            {
                str = c.Text;               
                aCommand3 = new OleDbCommand("select * from batch_tbl where batch_branch LIKE '" + str + "'", main_connection);
                aAdapter3 = new OleDbDataAdapter(aCommand3);
                ds3 = new DataSet();
                aAdapter3.Fill(ds3, "app_info");
                ds3.Tables[0].Constraints.Add("pk_bno", ds3.Tables[0].Columns[0], true);
                int batch_count = ds3.Tables[0].Rows.Count;
                //filling the groupbox with batch code by generating dynamic checkboxes
                for (int i = 0; i < batch_count; ++i)
                {
                    checkBox[i] = new CheckBox();
                    checkBox[i].Name = "check" + Convert.ToString(i);
                    checkBox[i].Text = ds3.Tables[0].Rows[i][1].ToString();
                    Console.WriteLine(checkBox[i].Text);
                    checkBox[i].Location = new System.Drawing.Point(104*position, 30);
                    groupBox2.Text = c.Text; 
                    groupBox2.Controls.Add(checkBox[i]);
                    position++;
                    count++;
                }
            }
            else
            {
                count--;
                this.Controls.RemoveByKey("lbl" + c.Name);
                this.Update();
            }
        }   
This code very fine but I don't know how many Branch CB will use select, so how can I put GB for each selected branch at desig time, for his I need to genrated GB at runtime on selection of Branch CheckBoxes.
Posted 12-May-13 7:46am
Comments
Maciej Los at 12-May-13 14:57pm
   
I don't know where do you stuck and what kind of problem do you have...
You can add groupbox(es) in the same way as you've added checkboxes.
Just think about it and try!
;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
vishal deb at 12-May-13 13:05pm
   
I already done with these articles and also if you gone through my issue then you can see I am able to created dynamic check box done many operation with them. I want to generate dynamic groupbox based on checkbox selection
Maciej Los at 12-May-13 15:01pm
   
Please, read my comment to your question.
Abhinav S at 13-May-13 0:26am
   
Fair comment.
vishal deb at 13-May-13 0:43am
   
Thank you Maciej Los
I gave a deep thought as you suggested and found the way
Maciej Los at 13-May-13 1:47am
   
You're welcome ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here is code after modification which gave me my desired result
private void RO_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox1.Controls.Clear();
            String m = RO.SelectedItem.ToString();
            Console.WriteLine(m);
            aCommand2 = new OleDbCommand("select * from branch_tbl,region_tbl where branch_tbl.region_id=region_tbl.region_id and region_tbl.region_name LIKE '" + m + "'", main_connection);
            aAdapter2 = new OleDbDataAdapter(aCommand2);
            ds2 = new DataSet();
            aAdapter2.Fill(ds2, "app_info");
            ds2.Tables[0].Constraints.Add("pk_bno", ds2.Tables[0].Columns[0], true);
            int bran_count = ds2.Tables[0].Rows.Count;
            Console.WriteLine(bran_count);
            checkBox = new CheckBox[bran_count];
            
            for (int i = 0; i < bran_count; ++i)
            {
                checkBox[i] = new CheckBox();
                checkBox[i].Name = "radio" + Convert.ToString(i);
                checkBox[i].Text = ds2.Tables[0].Rows[i][2].ToString();
                checkBox[i].Location = new System.Drawing.Point(125 * i, 15);
                groupBox1.Controls.Add(checkBox[i]);
                checkBox[i].CheckStateChanged += new System.EventHandler(CheckBoxCheckedChanged);
            }
            gpBox=new GroupBox[bran_count];
        }
       String str = null;
       int count = 1;
       int gpcount = 1;
       int position = 1;
       int gpposition = 110;
        //Code for handling event when branch check box is selected or unselected

        private void CheckBoxCheckedChanged(object sender, EventArgs e)
        {
            CheckBox c = (CheckBox)sender;
            //Label myLabel;
            String str = null;
            if (c.Checked == true)
            {
                str = c.Text;
                gpBox[gpcount] = new GroupBox();
                gpBox[gpcount].Name = "gpBox" + Convert.ToString(count);
                gpBox[gpcount].Text = str;
                gpBox[gpcount].Location = new Point(5, gpposition);
                gpBox[gpcount].AutoSize = true;
                this.Controls.Add(gpBox[gpcount]);
                
                aCommand3 = new OleDbCommand("select * from batch_tbl where batch_branch LIKE '" + str + "'", main_connection);
                aAdapter3 = new OleDbDataAdapter(aCommand3);
                ds3 = new DataSet();
                aAdapter3.Fill(ds3, "app_info");
                ds3.Tables[0].Constraints.Add("pk_bno", ds3.Tables[0].Columns[0], true);
                int batch_count = ds3.Tables[0].Rows.Count;
                //filling the groupbox with batch code by generating dynamic checkboxes
                for (int i = 0; i < batch_count; ++i)
                {
                    checkBox[i] = new CheckBox();
                    checkBox[i].Name = "check" + Convert.ToString(i);
                    checkBox[i].Text = ds3.Tables[0].Rows[i][1].ToString();
                    Console.WriteLine(checkBox[i].Text);
                    checkBox[i].Location = new System.Drawing.Point(104 * position, 30);
                    gpBox[gpcount].Controls.Add(checkBox[i]);
                    position++;
                    count++;
                }
                position = 1;
                gpposition += 100;
            }
            else
            {
                count--;
                this.Controls.RemoveByKey("lbl" + c.Name);
                this.Update();
            }
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Solution 2 is the code which I wrote for generating dynamic groupbox and dynamic checkbox inside groupbox.
I got help from
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 13 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100