help with array of buttons
-
hi i need to place on my form buttons as N number and i need that the buttons will be an array. for example: N=5 i need to see on screen: [button1] [button2] [button3] [button4] [button5] how to do it on C# WinForm FW3.5 thanks in advance
Assuming that you have detailed your requirements correctly, and that you can't use a list (for example), you could do it like this (in pseudocode):
declare array of size n
for count = 0, count less than n
create a new instance of button
set the text of the button
add it to the parent control collection (where parent could be a form, a panel, or the like)
end forForgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Assuming that you have detailed your requirements correctly, and that you can't use a list (for example), you could do it like this (in pseudocode):
declare array of size n
for count = 0, count less than n
create a new instance of button
set the text of the button
add it to the parent control collection (where parent could be a form, a panel, or the like)
end forForgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Don't forget to set the Location of the Button. I also tend to put have a Panel or GroupBox to hold such dynamic Controls to separate them from the non-dynamic items.
-
hi i need to place on my form buttons as N number and i need that the buttons will be an array. for example: N=5 i need to see on screen: [button1] [button2] [button3] [button4] [button5] how to do it on C# WinForm FW3.5 thanks in advance
The solution would be:
public Form1()
{
InitializeComponent();var buttons = new Button\[5\]; for (int i = 1; i < buttons.Length; i++) { buttons\[i\] = new Button(); buttons\[i\].Text = "\[button" + i + "\]"; buttons\[i\].Location = new Point(0, 25 \* i); } Controls.AddRange(buttons); }
obviously you would have to adjust on your formatting. but thats the general idea. hope this helped.
-
The solution would be:
public Form1()
{
InitializeComponent();var buttons = new Button\[5\]; for (int i = 1; i < buttons.Length; i++) { buttons\[i\] = new Button(); buttons\[i\].Text = "\[button" + i + "\]"; buttons\[i\].Location = new Point(0, 25 \* i); } Controls.AddRange(buttons); }
obviously you would have to adjust on your formatting. but thats the general idea. hope this helped.
Please note the above code would produce #4 buttons, not #5. The array element at position #0 will be undefined. If the button's width, which you don't specify, is exactly #25, then they will be equally spaced, and not overlap; but that makes an assumption about a button's default width, which can be affected by whether 'AutoSize is 'true, and the width of the 'Text property content. And, no, I did not down-vote your post. best, Bill
"Science is facts; just as houses are made of stones: so, is science made of facts. But, a pile of stones is not a house, and a collection of facts is not, necessarily, science." Henri Poincare
-
Please note the above code would produce #4 buttons, not #5. The array element at position #0 will be undefined. If the button's width, which you don't specify, is exactly #25, then they will be equally spaced, and not overlap; but that makes an assumption about a button's default width, which can be affected by whether 'AutoSize is 'true, and the width of the 'Text property content. And, no, I did not down-vote your post. best, Bill
"Science is facts; just as houses are made of stones: so, is science made of facts. But, a pile of stones is not a house, and a collection of facts is not, necessarily, science." Henri Poincare
I agree with your statement. This was a rough general idea so he could get started. Not a final solution. Please bare that in mind. Thanks though.