【问题标题】:Button Array using String and Integer C#使用字符串和整数 C# 的按钮数组
【发布时间】:2016-04-19 09:55:27
【问题描述】:

我想制作包含stringinteger 的按钮数组。我有30 buttons 并命名为B1, B2, ...., B30,我想根据计数器值更改它们的颜色。我怎样才能做到这一点?这些是我所做的,我卡住了

for(Cnt = 0; cnt < 30; cnt++)
{
   Button[] Tombol = new Button[]{"B"+(cnt+1)};  
   Tombol[cnt].BackColor = Color.Red
}

【问题讨论】:

  • 按钮数组在for循环中初始化,因此只存在于那里。在循环之前创建数组并且只创建单个按钮。但是您是否会在某个时候将这些按钮添加到表单中?
  • 是的。我同时将这些按钮添加到表单中。我仍然对只创建单个按钮感到困惑。你能给我解释一下吗? @弗兰奇

标签: c# arrays


【解决方案1】:

Form(在您的情况下,ControlButton)中的Control(自定义)初始化需要更多而不是简单的声明。除了给出名字(你做的),另外两件重要的事情是:

  1. 将其添加到Control 父级
  2. 很好地定位它

因此,将这些注意事项添加到您要创建的 Button 中,您可以执行类似的操作

int noOfBtns = 30;
Button[] Tombols = new Button[30]; //actually, you may not need this at all
for (int cnt = 0; cnt < numOfBtns; cnt++)
{
    Button tombol = new Button();
    tombol.BackColor = Color.Red;
    tombol.Location = new Point(5, cnt * 25); //Read (2) please formulate this more properly in your case
    tombol.Name = "B" + (cnt + 1).ToString(); 
    // others like size (maybe important depending on your need), color, etc
    this.Controls.Add(tombol); //Read (1) this refers to the `Form` if the parent control you want to put your button to is `Form`. But change the `this` as you see fit
    Tombols[cnt] = tombol; //again, actually you may not need this at all
}

注意您如何制定按钮的位置,非常重要。我上面给出的例子是非常简单的公式,如果你的按钮数量变大,它可能不适合。但这让您大致了解了设置Button位置是多么重要

您可能根本需要Button 的数组,除非出于某种原因您想列出它。但即使你想列出它,你也应该使用ListList.Add 而不是Array

【讨论】:

  • 一个错字:New Point 而不是new Point;您可能也想分配Sizetombol.Size = new Size(50, 20);;更安全的是不要在 for 循环中使用幻数和常量noOfBtnsButton[] Tombols = new Button[noOfBtns]; 然后for(int cnt = 0; cnt &lt; Tombols.Length; ++cnt)
  • 我已经尝试过了,但它不起作用:(那是按钮的颜色仍然是白色,不会变成红色。Button tombol = new Button(); tombol.Location = new Point(999, 415); tombol.Size = new Size(25, 40); tombol.Name = "B11".ToString(); tombol.BackColor = Color.Red; this.Controls.Add(tombol);
  • 是的。我更新了那个代码。首先,我想尝试在特定坐标上更改按钮的颜色。按钮的坐标是 999, 415,它的名字是 B11。它不起作用..你能帮帮我吗? :(
  • 我想更改背景颜色。我在 (999, 415) 的坐标上做了一个按钮,它的大小是 (25, 40)。它的名字是B11。我想先尝试改变它的颜色,它不起作用。
【解决方案2】:

我建议使用 Linq生成数组:

Button[] Tombol = Enumerable
  .Range(0, 30)
  .Select(i => new Button() {
    Text = String.Format("B{0}", i + 1),
    BackColor = Color.Red, 
    //TODO: compute desired color as function of "i" like
    // BackColor = Color.FromArgb(i * 8, 255 - i * 8, 128),
    //TODO: assign Parent, Location, Size etc. like this:
    // Parent = this,
    // Location = new Point(10 + 40 * i, 10),
    // Size = new Size(35, 20),
  })
  .ToArray();

【讨论】:

    【解决方案3】:

    目前,您正在代码的每个循环中重新创建数组,而不是在循环之外创建数组,然后在循环中向其添加按钮。

    此外,您的所有按钮都是在同一位置创建的,因此它们彼此重叠,这意味着您只能看到其中一个。

    试试这样,based on a modified version of the code from this previous answer 创建按钮,将它们添加到您可以搜索的列表中,并设置按钮的位置:

            int top = 50;
            int left = 100;
    
            var buttons = new Dictionary<string, Button>();
    
            for (int i = 0; i < 30; i++)
            {
                Button button = new Button();
                buttons.Add("B"+i, button);
                button.Left = left;
                button.Top = top;
                this.Controls.Add(button);
                top += button.Height + 2;
                button.BackColor = Color.Red;
            }
    

    然后您可以稍后使用代码引用单个按钮:

    buttons["B3"].BackColor= Color.Green;
    

    【讨论】:

      【解决方案4】:

      你必须先初始化按钮数组

      int numOfBtns = 30;
      Button[] Tombol = new Button[numOfBtns];
      

      然后你可以填写所需的项目

      for (int cnt = 0; cnt < numOfBtns; cnt++)
      {
          // Name, Content, Foreground .. whatever
          Tombol[cnt].Name = "B" + (cnt + 1);
      }
      

      【讨论】:

        【解决方案5】:

        我的意思是这样的:

        Button[] Tombol = new Button[30];
        for(cnt = 0; cnt < 30; cnt++)
        {
            Tombol[cnt] = new Button
                {
                    Text = "B" + (cnt+1),
                    BackColor = Color.Red
                };
        }
        

        首先创建数组,然后在 for 循环中逐个实例化实际的按钮。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-21
          • 1970-01-01
          • 2010-11-07
          • 1970-01-01
          • 2018-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多