在Windows窗体中,如何根据特定条件动态创建复选框和标签。
答案 0 :(得分:1)
创建要添加的控件实例后,不要忘记将它们添加到表单控件属性中,试试这个:
private void Form1_Load(object sender, EventArgs e)
{
bool condition = true;
if (condition)
{
Label l_newOne = new Label()
{
Text = "Label: ",
Location = new Point(10, 30)
};
CheckBox chckb_newOne = new CheckBox()
{
Text = "Correct",
Location = new Point(50, 25)
};
this.Controls.AddRange(new Control[] { chckb_newOne, l_newOne });
}
}
答案 1 :(得分:0)
if (CertainCondition)
{
Children.Add(new CheckBox());
}