问题

添加都是在groupbox的text的位置,每添加一个就不前面加的覆盖了

1
2
3
4
5
6
7
8
9
10
11
foreach(DataRow dr in dt.Rows)

{

CheckBox chkbox = new CheckBox();

chkbox.Text = dr["模块名称"].ToString();

gpboxBrow.Controls.Add(chkbox);

}

添加位置坐标来解决问题

  • 方法一:

    在你的代码中添加这样的代码:

1
2
chkbox.Location = new System.Drawing.Point(349, 210);
chkbox.Size = new System.Drawing.Size(75, 23);

例如:动态添加两个Button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private void button2_Click(object sender, EventArgs e)
{
// button1
//
Button btn = new Button();
btn.Location = new System.Drawing.Point(349, 210);
btn.Name = "button1";
btn.Size = new System.Drawing.Size(75, 23);
btn.TabIndex = 2;
btn.Text = "button1";
btn.UseVisualStyleBackColor = true;
this.Controls.Add(btn);

//
// button2
//
Button btn2 = new Button();
btn2.Location = new System.Drawing.Point(349, 240);
btn2.Name = "button2";
btn2.Size = new System.Drawing.Size(75, 23);
btn2.TabIndex = 3;
btn2.Text = "button2";
this.Controls.Add(btn2);
}

或者设置chkbox.Location属性

1
2
3
4
5
6
7
8
9
int y = 0;
foreach(DataRow dr in dt.Rows)
{
y += 30;
CheckBox chkbox = new CheckBox();
chkbox.Location = new Point(10,y);
chkbox.Text = dr["模块名称"].ToString();
gpboxBrow.Controls.Add(chkbox);
}

相关链接(侵删)

  1. groupbox 里动态添加checkbox的问题

=================我是分割线=================

欢迎到公众号来唠嗑: