【问题标题】:C# How to return Button array from a functionC#如何从函数返回按钮数组
【发布时间】:2016-04-01 20:26:56
【问题描述】:

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

        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
    }

【问题讨论】:

  • void 意味着它不返回任何东西时,你将如何在这里返回任何东西private void bHow_Click(object sender, EventArgs e)...更改方法签名以返回按钮[]
  • @MethodMan 我很确定他的意思是“在这里获取按钮数组”而不是“返回”
  • @cFrozenDeath 我很确定他没有\
  • @MethodMan 那么这可能是个问题。如果没有手动更改命名,则不能将 Button 的 Click Event 返回类型设置为 void 以外的任何内容。如果名称已更改或未附加到按钮的单击事件,我将予以纠正。
  • @cFrozenDeath 不用担心 .. 只是快速观察一下

标签: c# arrays button return controls


【解决方案1】:

你至少试过这个吗?

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

// blablabla
Button[] getTheButtonArrayHere = CreatingButtons(someIntVariable, someListOfStringVariable);

【讨论】:

    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2021-01-30
    • 2014-12-19
    相关资源
    最近更新 更多