C#如何从函数返回Button数组
本文关键字:返回 Button 数组 函数 | 更新日期: 2023-09-27 18:28:15
你好,我有一个创建按钮数组的方法,我想让它包装在方法中,因为我会使用它几次:
void CreatingButtons(int n, List<string> names)
{
Button[] Buttons = new Button[n];
int horizontal = 180; int vertical = 5;
int Height = 33; int Width = 350 / Buttons.Length;
for (int i = 0; i < Buttons.Length; i++)
{
Buttons[i] = new Button();
Buttons[i].Height = Height;
Buttons[i].Width = Width;
this.Controls.Add(Buttons[i]);
Buttons[i].Text = names[i];
Buttons[i].TextAlign = ContentAlignment.MiddleCenter;
Buttons[i].Location = new Point(horizontal, vertical);
horizontal += Buttons[i].Width;
Buttons[i].Click += (o, k) => { };
}
}
我想在中返回新创建的按钮阵列
private void bHow_Click(object sender, EventArgs e)
{
List<string> buttonNames = new List<string> { "Dealing the Cards", "Betting Blind", "How to Properly Bet" };
CreatingButtons(3, buttonNames);
//Button [] getTheButtonArrayHere = CreatingButtons() or something like this
}
你至少试过这个吗?
Button[] CreatingButtons(int n, List<string> names)
{
//your code...
return Buttons; // return the Button array
}
// blablabla
Button[] getTheButtonArrayHere = CreatingButtons(someIntVariable, someListOfStringVariable);