我有这个程序动态地添加了指向数据库中总裁数量的picturebox。我如何把它们放在groupbox里面并且把groupbox里的picturebox放进去?如果图片盒很多,组盒应该伸展。在c#winforms控件中水平和垂直对齐动态添加控件
我现在有这样的代码:
private void Form1_Load(object sender, EventArgs e)
{
conn.Open();
try
{
cmd = new SqlCommand("SELECT COUNT(Position) FROM TableVote WHERE Position='" + "President" + "'", conn);
Int32 PresCount = (Int32)cmd.ExecuteScalar();
TxtPresCount.Text = PresCount.ToString();
for (int i = 0; i < PresCount; ++i)
{
GroupBox PresGB = new GroupBox();
{
PresGB.Size = new Size(491, 152);
PresGB.Location = new Point(12, 12);
PresGB.Text = "President";
this.Controls.Add(PresGB);
PresGB.SendToBack();
PictureBox PresPB = new PictureBox();
PresPB.Location = new Point(80 + (150 * i) + 20, 50);
PresPB.Size = new Size(75, 75);
PresPB.BorderStyle = BorderStyle.Fixed3D;
PresPB.ImageLocation = "imgPath";
this.Controls.Add(PresPB);
PresPB.BringToFront();
};
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
我想的pictureboxes是组框内部,里面对齐。
检查码头和锚性质 – 2013-10-05 00:58:41
但我编程方式添加的控件不在设计。那么我该怎么做呢? –
然后以编程方式设置属性 – 2013-10-05 01:30:02