Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > how to add array of buttons in a form
 

how to add array of buttons in a form

I have to display buttons in NxN array with N=4,5 and 6, and know each button. N is selected from New in File menu.
How to I am supposed to proceed to do this
ttonhunt  Tuesday, September 02, 2008 2:10 AM
You can do something like this:


private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
this.CreateButtons(4);
}


private void CreateButtons(int n)
{
int count = n * n;
Button[] btns = new Button[count];
for (int j = 0; j < count; j++)
{
btns[j] = new Button();
int row = j / n;
int col = j % n;
btns[j].Name = string.Format("button{0}{1}", row, col);
btns[j].Text = btns[j].Name;
btns[j].Location = new Point(10 + 80 * col, 80 + 30 * row);
this.Controls.Add(btns[j]);
}
}
Zhi-Xin Ye  Tuesday, September 02, 2008 11:21 AM
You can do something like this:


private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
this.CreateButtons(4);
}


private void CreateButtons(int n)
{
int count = n * n;
Button[] btns = new Button[count];
for (int j = 0; j < count; j++)
{
btns[j] = new Button();
int row = j / n;
int col = j % n;
btns[j].Name = string.Format("button{0}{1}", row, col);
btns[j].Text = btns[j].Name;
btns[j].Location = new Point(10 + 80 * col, 80 + 30 * row);
this.Controls.Add(btns[j]);
}
}
Zhi-Xin Ye  Tuesday, September 02, 2008 11:21 AM

OK, I see and have tried this example and to a point it works well. But there are some things which are missing that were available in VB6 control arrays.

In my instance I am actually using textboxes, but you can not change some of the properties at runtime (font properties). When using your example font properties are taken from the default values. I have not found a way to change them.

In VB6 you actually made the control that you wanted to array. Each new control thus had the properties to the base control, not the default properties.

Is there a way to have the new added control to have different font properties than the default?

JHartwell  Friday, May 15, 2009 4:18 AM

You can use google to search for other answers

Custom Search

More Threads

• How to play Local
• linking
• SoapException
• IssueVisionWebCS service for Access Database
• help in datagridview
• Terrarium 1.2 not showing any text on buttons?
• Help
• Terrarium Servers Back Up!
• TaskVision server source code?
• Scheduler