C#如何从函数返回按钮数组
您好,我有这个方法,它创建了一个按钮数组,我想将它包装在方法中,因为我将多次使用它:C#如何从函数返回按钮数组,c#,arrays,button,return,controls,C#,Arrays,Button,Return,Controls,您好,我有这个方法,它创建了一个按钮数组,我想将它包装在方法中,因为我将多次使用它: void CreatingButtons(int n, List<string> names) { Button[] Buttons = new Button[n]; int horizontal = 180; int vertical = 5; int Height = 33; int Width = 350 / Button
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) => { };
}
}
无效创建按钮(int n,列表名称)
{
按钮[]按钮=新按钮[n];
int水平=180;int垂直=5;
内部高度=33;内部宽度=350/按钮。长度;
对于(int i=0;i{};
}
}
我想在这里返回新创建的按钮数组
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
}
private void bHow_Click(对象发送者,事件参数e)
{
列表按钮名称=新列表{“发牌”、“盲注”、“如何正确下注”};
创建按钮(3个按钮名称);
//Button[]getTheButtonArrayHere=CreatingButtons()或类似的内容
}
您至少试过这个吗
Button[] CreatingButtons(int n, List<string> names)
{
//your code...
return Buttons; // return the Button array
}
// blablabla
Button[] getTheButtonArrayHere = CreatingButtons(someIntVariable, someListOfStringVariable);
Button[]创建按钮(int n,列表名称)
{
//你的代码。。。
返回按钮;//返回按钮数组
}
//喋喋不休
Button[]getTheButtonArrayHere=创建按钮(someIntVariable,someListOfStringVariable);
当void
表示它不返回任何内容时,如何在此处返回任何内容private void bHow_Click(对象发送者,事件参数)
。。。将方法签名更改为return Buttons[]@MethodMan我很确定他指的是“在此处获取按钮数组”,而不是“return”@cfrozendath我很确定他没有\@MethodMan,那么这可能是个问题。如果命名未手动更改,则不能将按钮的单击事件返回类型设置为除void以外的任何类型。如果名称已更改,或者此名称未附加到按钮的单击事件,我将得到更正。@cfrozendath不必担心。。只是快速观察一下