代码之家  ›  专栏  ›  技术社区  ›  QuestionsEverywhere

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

  •  -5
  • QuestionsEverywhere  · 技术社区  · 9 年前

    你好,我有一个创建按钮数组的方法,我想让它包装在方法中,因为我会使用它几次:

            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
        }
    
    1 回复  |  直到 9 年前
        1
  •  5
  •   Camilo Terevinto Chase R Lewis    9 年前

    你至少试过这个吗?

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