C#如何在面板形式加载中显示随机生成器数组按钮 [英] C# how to display a random generator array button in panel form load
本文介绍了C#如何在面板形式加载中显示随机生成器数组按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个随机生成器数组按钮。我希望在表单加载时在面板中显示它...?
我尝试过:
I have a randomly generator array button.i want to display it inside the panel when form load...?
What I have tried:
int maxRows = 10;
int maxCols = 10;
Button[,] b_Array = new Button[maxRows, maxCols];
for (int i = 0; i < maxRows; i++)
{
for (int j = 0; j < maxCols; j++)
{
b_Array[i, j] = new Button();
b_Array[i, j].Size = new Size(40, 55);
b_Array[i, j].Location = new Point(55 + j * 35, 55 + i * 55);
this.Controls.Add(b_Array[i, j]);
}
}
推荐答案
您展示的代码应该有效:但是它将按钮添加到这个
引用,这是包含类的当前实例 - 通常它将是您当前的表单。
要添加只需更改:
The code you show should work: but it adds the buttons to thethis
reference, which is the current instance of the containing class - normally it will be your current form.
To add them to your panel instead of teh form, just change:
this.Controls.Add(b_Array[i, j]);
收件人:
To:
myPanel.Controls.Add(b_Array[i, j]);
但我这样做的方式略有不同:
But the way I would do it is slightly different:
Button[] buttons = new Button[maxRows * maxCols];
int index = 0;
for (int i = 0; i < maxRows; i++)
{
for (int j = 0; j < maxCols; j++)
{
Button b = new Button();
b.Size = new Size(40, 55);
b.Location = new Point(55 + j * 35, 55 + i * 55);
buttons[index++] = b;
}
}
myPanel.Controls.AddRange(buttons);
这篇关于C#如何在面板形式加载中显示随机生成器数组按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文