1,105,625 Community Members

How create dynamic GroupBox

Member Avatar
vishalonne
Posting Whiz in Training
229 posts since Jun 2009
Reputation Points: 12 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

Just to make sure I understand. How many groupboxes do you want to display at one time? It sounds like anywhere from 2 to 6. 1 for the branch checkboxes and possibly one each(up to 5) for the batches of each branch. If this true, I would suggest adding and placing all the groupboxes, with enough room in each one for the maximum number of checkboxes you might add, in the design window. then according to whether the checkbox corresponding to a particular groupbox is checked, set visible to true or false. Then, whenever a branch checkbox is unchecked, clear the checkboxes for that particular groupbox, and whenever it's checked re-load the checkboxes from the database.

Member Avatar
vishalonne
Posting Whiz in Training
229 posts since Jun 2009
Reputation Points: 12 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanx Tins
For guidance. Yes you are right, but my problem is that I dont know how many branch checkbox user will select, he/she might select 1 or all or 3 out of five. In future if branch increases then check box will for branches will also increase. So, it is not feasiable to add groupboxes for each branches at design time. It should be at run time.
Means groupbox(s) should be generated at runtime as per selection by user for branches.

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

How many possible checkboxes will the groupboxes need to hold? And will the groupboxes be holding any other controls?

Member Avatar
vishalonne
Posting Whiz in Training
229 posts since Jun 2009
Reputation Points: 12 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

there can be 8 to 10 batches for each branch, means 8 to 10 checkboxs for each groupboxes. And yes there will be 3 combobox and 1 more checkbox inside each group box.
combobox1 for-> Teachers name coming from table
combobox2 for-> Duration of session coming from table (1 hour, 1.3 hours, 2 hours)
combobox3 for-> Time slot (8:00Am, 8:30 AM, 9:00 AM, 9:30 AM, 10:00 AM, 10:30 AM.. till 8:00 PM)
checkbox for-> Days (S,M,T,W,Th,F,S)

Member Avatar
vishalonne
Posting Whiz in Training
229 posts since Jun 2009
Reputation Points: 12 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

My problem is solved, I just think about when checkbox can be created dynamically why not groupbox can be created dynamicall as I have done for checkboxes and I modified my code and DONE.

        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();
            }
        }
Question Answered as of 11 Months Ago by tinstaafl
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article