【发布时间】:2016-05-18 17:41:37
【问题描述】:
我想在 trackbar_scroll 上动态创建文本框。如果 trackbar 的值为 5,它可能有 5 个文本框。当它减少到 2 时,它必须有 2 个文本框。这是我减少 trackbar_scroll 值时的问题:
private void trackBar1_Scroll(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls) // to remove all textboxes before creating new
{
if (ctrl is TextBox)
{
this.Controls.Remove(ctrl);
ctrl.Dispose();
}
}
int x = 45; // location for textbox
for (int i = 0; i < trackBar1.Value; i++)
{
listBox1.Items.Add(i);
TextBox _text = new TextBox();
_text.Name = "txt"+i;
_text.Height = 20;
_text.Width = 100;
_text.Text = _text.Name;
_text.Location = new Point(x, 85);
this.Controls.Add(_text);
x = x + 120;
}
}
【问题讨论】:
标签: c# dynamic textbox trackbar