C#如何从函数返回Button数组

Que*_*ere -5 c# arrays controls return 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) => { };
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想在这里返回新创建的按钮数组

        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
    }
Run Code Online (Sandbox Code Playgroud)

SO *_*ood 5

你至少试过这个吗?

Button[] CreatingButtons(int n, List<string> names)
{
    //your code...
    return Buttons; // return the Button array
}

// blablabla
Button[] getTheButtonArrayHere = CreatingButtons(someIntVariable, someListOfStringVariable);
Run Code Online (Sandbox Code Playgroud)