c# - 如何在 C# 中创建动态 GroupBox

标签 c# groupbox

我正在尝试动态创建组框,这些 GB 将根据复选框的选择创建。

意味着我现在有 5 个复选框,如果我选择第一个 CB,那么将创建 1 GB 和一些其他动态复选框,如果我选择第三个检查,那么将创建另一个 GB 和几个其他复选框。 为此,我正在尝试使用这段代码,我能够为设计时已经创建的固定组框创建动态复选框。

我的情况是 - 5 个分支有多个批处理。现在用户将从动态复选框中选择分支,在此基础上 btaches 将显示在每个分支的组框中。 Branch1 Branch2 Branch3 Branch4 Branch5 如果用户选择第 3 和第 5 个分支,则 GB1 将显示 Branch3 的批处理,GB2 将显示 Branch5 的批处理 这是代码-

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

这段代码非常好,但我不知道有多少 Branch CB 将使用 select,所以我如何在设计时为每个选定的分支放置 GB,因为我需要在运行时在选择 Branch CheckBoxes 时生成 GB。

最佳答案

经过深思熟虑,我解决了我的问题。我刚刚完成了生成动态复选框的操作。这是代码

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

关于c# - 如何在 C# 中创建动态 GroupBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16514622/

相关文章:

c# - 直接给类变量赋值

c# - 将 Kusto 客户端响应转换为对象列表会返回空对象

vb.net - VB .Net 我可以有效地管理组框内的所有事件吗?

c++ - 向静态组合框控件添加单击/双击事件

vb.net - 删除 GroupBox 内的控件

c# - 更新控件中的 asp.net 面板可见性

c# - 按月将值放入数据表组

.net - GroupBox控件中的圆角

c# - 如何获取 WPF 中组框中的控件列表

c# - 使用 NLog 配置 API 写入 ApplicationData 文件夹