可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to create 10 buttons on my form when I click on button1. No error with this code below but it doesnt work either.
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < buttons.Capacity; i++)
{
this.Controls.Add(buttons[i]);
}
}
回答1:
It doesn't work because the list is empty. Try this:
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < 10; i++)
{
Button newButton = new Button();
buttons.Add(newButton);
this.Controls.Add(newButton);
}
}
回答2:
You aren't creating any buttons, you just have an empty list.
You can forget the list and just create the buttons in the loop.
private void button1_Click(object sender, EventArgs e)
{
int top = 50;
int left = 100;
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
this.Controls.Add(button);
top += button.Height + 2;
}
}
回答3:
You could do something like this:
Point newLoc = new Point(5,5); // Set whatever you want for initial location
for(int i=0; i < 10; ++i)
{
Button b = new Button();
b.Size = new Size(10, 50);
b.Location = newLoc;
newLoc.Offset(0, b.Height + 5);
Controls.Add(b);
}
If you want them to layout in any sort of reasonable fashion it would be better to add them to one of the layout panels (i.e. FlowLayoutPanel) or to align them yourself.
回答4:
Two problems- List is empty. You need to add some buttons to the list first. Second problem: You can't add buttons to "this". "This" is not referencing what you think, I think. Change this to reference a Panel for instance.
//Assume you have on your .aspx page:
<asp:Panel ID="Panel_Controls" runat="server"></asp:Panel>
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < buttons.Capacity; i++)
{
Panel_Controls.Controls.Add(buttons[i]);
}
}
回答5:
I came with the same doubt, with the current contribution on the issue I could came up with:
int altura = this.Size.Height;
int largura = this.Size.Width;
int alturaOffset = 10;
int larguraOffset = 10;
int larguraBotao = 100; //button widht
int alturaBotao = 40; //button height
for (int i = 0; i < 50; ++i)
{
if ((larguraOffset+larguraBotao) >= largura)
{
larguraOffset = 10;
alturaOffset = alturaOffset + alturaBotao;
var button = new Button();
button.Size = new Size(larguraBotao, alturaBotao);
button.Name = "" + i + "";
button.Text = "" + i + "";
//button.Click += button_Click;//function
button.Location = new Point(larguraOffset, alturaOffset);
Controls.Add(button);
larguraOffset = larguraOffset + (larguraBotao);
}
else
{
var button = new Button();
button.Size = new Size(larguraBotao, alturaBotao);
button.Name = "" + i + "";
button.Text = "" + i + "";
//button.Click += button_Click;//function
button.Location = new Point(larguraOffset, alturaOffset);
Controls.Add(button);
larguraOffset = larguraOffset+(larguraBotao);
}
}
The expected behaviour is that, this will generate buttons using the current state of your window size, Always breaking a line when the next button would exceed the right margin of your window.
回答6:
First, you aren't actually creating 10 buttons. Second, you need to set the location of each button, or they will appear on top of each other. This will do the trick:
for (int i = 0; i < 10; ++i)
{
var button = new Button();
button.Location = new Point(button.Width * i + 4, 0);
Controls.Add(button);
}
回答7:
You can't add a Button to an empty list without creating a new instance of that Button.
You are missing the
Button newButton = new Button();
in your code plus get rid of the .Capacity
回答8:
use button array like this.it will create 3 dynamic buttons bcoz h variable has value of 3
private void button1_Click(object sender, EventArgs e)
{
int h =3;
Button[] buttonArray = new Button[8];
for (int i = 0; i <= h-1; i++)
{
buttonArray[i] = new Button();
buttonArray[i].Size = new Size(20, 43);
buttonArray[i].Name= ""+i+"";
buttonArray[i].Click += button_Click;//function
buttonArray[i].Location = new Point(40, 20 + (i * 20));
panel1.Controls.Add(buttonArray[i]);
} }