簡體   English   中英

如何在C#中創建動態GroupBox

[英]How create dynamic GroupBox in C#

我正在嘗試動態創建組框,而這些GB將在選中復選框時創建。

意味着如果我選擇第一個CB,則現在有5個復選框,那么將創建1 GB的其他動態復選框,如果我選擇3rd復選框,則將創建另一個GB,並帶有其他幾個復選框。 為此,我正在嘗試使用此代碼,我能夠為在設計時已經創建的固定groupbox創建動態復選框。

我的情況是-5個分支有多個批次。 現在,用戶將從動態復選框中選擇分支,並在此基礎上在每個分支的組框中顯示分支。 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();
        }
    } 

這段代碼非常好,但是我不知道有多少個分支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();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM