如何创建动态GroupBox [英] How create dynamic GroupBox

查看:80
本文介绍了如何创建动态GroupBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有



我正在尝试动态创建组合框,这些GB将在选中复选框时创建。



意味着我现在有5个复选框,如果我选择第一个CB然后1 GB,其他一些动态复选框将被创建,如果我选择第三个检查,那么另一个GB将创建其他几个复选框。

为此,我尝试使用此代码,我可以为已在设计时创建的固定组合框创建动态复选框。



我的方案是 - 5个分支有多个批次。现在用户将从动态复选框中选择分支,并在此基础上,btaches将显示在每个分支的groupbox中。

Branch1 Branch2 Branch3 Branch4 Branch5如果用户选择第3和第5分支,则GB1将显示Branch3'的批次和GB2将显示Branch5的批次

这是代码 -



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



这段代码非常好,但我不知道有多少分支CB将使用select,那么我怎样才能把GB对于每个选定的分支在设计时,对于他我需要在运行时选择分支复选框生成GB。


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.

推荐答案

尝试

< a href =http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=143> http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=143 [ ^ ]

http://www.dotnetspider.com/forum/181808-Radio-button-creating-dynamically.aspx [ ^ ]

http://p2p.wrox.com/c/40016-dynamic-checkbox-radiobutton-lists.html [ ^ ]
Try
http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=143[^]
http://www.dotnetspider.com/forum/181808-Radio-button-creating-dynamically.aspx[^]
http://p2p.wrox.com/c/40016-dynamic-checkbox-radiobutton-lists.html[^]


这是修改后的代码,它给了我想要的结果

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


解决方案2是我为在组框内生成动态组框和动态复选框而编写的代码。

我从获得帮助
Solution 2 is the code which I wrote for generating dynamic groupbox and dynamic checkbox inside groupbox.
I got help from


这篇关于如何创建动态GroupBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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