Quote:
for (int i = 0; i < Int32.Parse(value); i++)
{
btnM = new btnMini();
btnM.Name = "btn_" + counter;
btnM.ButtonClick += (sender, e) => btnTestClick(this, e);
counter++;
control.Add(btnM);
foreach (var b in control)
{
this.GenerateFGridPanel.Children.Add(b);
}
}
Step through your code and see what it is doing in the debugger. Your logic is not correct.
On the first run through the loop, you create a button, add it to a
List<Control>
, and then add it as a child of the
GenerateFGridPanel
.
On the second run through the loop, you create another button and add it to the
List<Control>
. You then try to add
both buttons as children of the
GenerateFGridPanel
control.
Since the first button has already been added as a child, you cannot add it again.
Either drop the list and add the controls as you create them, or move the
foreach
loop out of the
for
loop.
for (int i = 0; i < Int32.Parse(value); i++)
{
btnM = new btnMini();
btnM.Name = "btn_" + counter;
btnM.ButtonClick += (sender, e) => btnTestClick(this, e);
counter++;
this.GenerateFGridPanel.Children.Add(btnM);
}
Or:
for (int i = 0; i < Int32.Parse(value); i++)
{
btnM = new btnMini();
btnM.Name = "btn_" + counter;
btnM.ButtonClick += (sender, e) => btnTestClick(this, e);
counter++;
control.Add(btnM);
}
foreach (var b in control)
{
this.GenerateFGridPanel.Children.Add(b);
}