我有一些进度条,它们是在按钮单击的运行时创建的。
private void button1_Click(object sender, EventArgs e)
{
int count=0;
for(var item in items)
{
count++;
ProgressBar pBar = new ProgressBar();
pBar.Name = "progressBar1_"+count;
pBar.Width = 200;
pBar.Height = 15;
pBar.Minimum = 1;
pBar.Maximum = 100;
pBar.Value = 1;
Panel1.Controls.Add(pBar);
如何访问动态创建的进度条以显示进度?
"progressBar1_"+count.PerformStep();// This doesnt work
答案 0 :(得分:0)
每次拨打电话时都在for循环中
ProgressBar pBar = new ProgressBar();
您创建了一个进度条的新实例。
一种选择是在列表或字典中记住这个实例。
类似的东西:
List<string> items = new List<string>() { "item" };
Dictionary<string, ProgressBar> progressBars = new Dictionary<string, ProgressBar>();
private void button1_Click(object sender, EventArgs e)
{
int count=0;
foreach(var item in items)
{
count++;
ProgressBar pBar = new ProgressBar();
pBar.Name = "progressBar_" + count;
pBar.Width = 200;
pBar.Height = 15;
pBar.Minimum = 1;
pBar.Maximum = 100;
pBar.Value = 1;
panel1.Controls.Add(pBar);
progressBars.Add(pBar.Name, pBar);
}
}
private void button2_Click(object sender, EventArgs e)
{
progressBars["progressBar_1"].PerformStep();
}
第二个选项是在面板的控件中搜索此实例。
List<string> items = new List<string>() { "item" };
private void button1_Click(object sender, EventArgs e)
{
int count=0;
foreach(var item in items)
{
count++;
ProgressBar pBar = new ProgressBar();
pBar.Name = "progressBar_" + count;
pBar.Width = 200;
pBar.Height = 15;
pBar.Minimum = 1;
pBar.Maximum = 100;
pBar.Value = 1;
panel1.Controls.Add(pBar);
progressBars.Add(pBar.Name, pBar);
}
}
private void button3_Click(object sender, EventArgs e)
{
(panel1.Controls.Find("progressBar_1", false).Single() as ProgressBar).PerformStep();
}