Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Auto generating buttons
 

Auto generating buttons

Hey All
For a little project of mine, I need to creat buttons from either an ArrayList containing the names of what the buttons should be called or from or from a database table. Depending what was is easiest to understand/do.

They all have to be in the same windows form, so that if the ArrayList contains 10 names then there should be generated 10 buttons on load, and if one of the names is removed from the ArrayList then the button should also be removed.

Hope anyone will be able to help me.

Thanks
Anders Lykke
Anders Lykke  Tuesday, November 14, 2006 5:23 PM

System.Collections.IList buttonNames = new System.Collections.ArrayList(new String[] { "one", "two" });

private void Form1_Load ( object sender, EventArgs e )

{

Size defaultButtonSize = new Size(75, 23);

const int top = 10;

const int verticalPadding = 4;

int i = 0;

foreach (string name in buttonNames)

{

Button button = new Button();

this.Controls.Add(button);

button.Size = defaultButtonSize;

button.Name = name;

button.Text = name;

button.Location = new Point(10, top + i * (button.Height + verticalPadding));

++i;

}

}

Peter Ritchie  Tuesday, November 14, 2006 6:08 PM

System.Collections.IList buttonNames = new System.Collections.ArrayList(new String[] { "one", "two" });

private void Form1_Load ( object sender, EventArgs e )

{

Size defaultButtonSize = new Size(75, 23);

const int top = 10;

const int verticalPadding = 4;

int i = 0;

foreach (string name in buttonNames)

{

Button button = new Button();

this.Controls.Add(button);

button.Size = defaultButtonSize;

button.Name = name;

button.Text = name;

button.Location = new Point(10, top + i * (button.Height + verticalPadding));

++i;

}

}

Peter Ritchie  Tuesday, November 14, 2006 6:08 PM
Thanks alot Peter for the fast and very good help. I assume this is more or less a general solution to adding controls to a Windows form.
Anders Lykke  Tuesday, November 14, 2006 7:30 PM

You can use google to search for other answers

Custom Search

More Threads

• jagged arrays don't serialise correctly as properties in the visual studio designer
• How do I flag a property as not serializable
• How to set color of the splitter in SplitContainer control?
• webbrower control
• Designers and Abstract Classes
• Help with UI design of General Journal Voucher and other vouchers
• Control sizes shink after opened from saved xml for .net 2003
• How to get data from database and save as xml file with autonumber as file name?
• Forms-only components
• How To Refresh Property Grid while desigining,.