C# 如何重绘(重新生成)窗口上动态生成的控件

2024-12-03 03:41:55
推荐回答(2个)
回答1:

你试试下面的方法吧,测试过没有问题,怎么添加timer我就不用说了吧
private void timer1_Tick(object sender, EventArgs e)
{
if (newPanel.Controls.Count == 0)
{
for (int i = 0; i < 4; i++)
{
RadioButton radio = new RadioButton();
radio.Text = "radio" + i.ToString();
radio.Location = new Point(50, i * 30);
newPanel.Controls.Add(radio);
}
}
else
{
int count = newPanel.Controls.Count;
for (int i = 0; i < count; i++)
{
newPanel.Controls[0].Dispose();
}
}
}

回答2:

private void button3_Click(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton radio = new RadioButton();
radio.Visible = true;
this.panel1.Controls.Add(radio);
radio.Location = new Point(100, 100);
radio.Text = "radio1";

System.Windows.Forms.RadioButton radio1 = new RadioButton();
radio1.Visible = true;
this.panel1.Controls.Add(radio1);
radio1.Location = new Point(150, 150);
radio1.Text = "radio2";

}
//清除所有控件
private void button4_Click(object sender, EventArgs e)
{
this.panel1.Controls.Clear();
}