2016-01-14 36 views
2

我想要使按鈕數組包括stringinteger。我有30 buttons並命名爲B1, B2, ...., B30,我想根據計數器值更改它們的顏色。我怎樣才能做到這一點?這是我做了什麼,我堅持使用字符串和整數C的按鈕數組#

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

按鈕數組是在for循環中初始化的,因此只存在於那裏。在循環之前創建數組,並只創建單個按鈕。 但是你是否在某些時候將這些按鈕添加到表單中? – Franky

+0

是的。我將這些按鈕添加到同一點的表單。我仍然困惑於只創建單個按鈕。你能解釋給我嗎? @Franky –

+0

Weeee四個答案 –

回答

1

FormControl(自定義)初始化(在你的情況, ControlButton)要求更多比簡單的聲明。除了給予名稱(你做的),另外兩個重要的事情要做有:

  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的數組,除非出於某種原因您想要列出它。但即使你想列出它,你應該使用ListList.Add而不是Array

+0

錯字:'新點'而不是'新點';你可能也想分配'Size':'tombol.Size = new Size(50,20);'; safes不是在* for循環中使用魔術數字和常量'noOfBtns' *:'Button [] Tombols = new Button [noOfBtns];'then'for(int cnt = 0; cnt

+0

我試過這個,它不起作用:(這是按鈕的顏色仍然是白色,並且不會變爲紅色 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); –

+0

Yes。I首先我想在特定的座標上嘗試改變按鈕的顏色,按鈕的座標是999,415,它的名字是B11,它不起作用..你能幫我嗎?:( –

0

我的意思是這樣的:

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

首先創建數組,並在for循環實例化實際的按鈕一個接一個。

0

此刻,您正在代碼的每個循環上重新創建數組,而不是在循環外部創建數組,然後在循環中向其添加按鈕。

此外,所有的按鈕都是在同一個位置創建的,所以它們彼此重疊,意味着您只能看到其中的一個。

嘗試這樣的事情,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; 
0

你有按鈕陣列首先初始化

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); 
} 
1

我建議使用的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();