Obaja Alfonsus Dhani
Obaja Alfonsus Dhani

Reputation: 175

Button Array using String and Integer C#

I want to make button array that include string and integer. I have 30 buttons and named B1, B2, ...., B30 and I want to change their color depend on the counter value. How can I do that? These are what I have done and I stuck

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

Upvotes: 2

Views: 857

Answers (5)

Ian
Ian

Reputation: 30813

The Control (custom) initialization in the Form (in your case, the Control being a Button) requires more than a simple declaration. Apart from giving name (which you do), two other important things to do are:

  1. To add it to the Control parent
  2. To locate it nicely

Thus, adding those considerations to the Button you are to create, you could do something like this

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
}

Take care of how you formulate the location of your button, very important. The example I gave above is really simple formulation, which might not fit if your number of buttons grow large. But that gives you the basic idea on how important it is to set the location of the Button right.

You may need the array of Button at all, unless for some reason you want to list it. But even you want to list it, you should use List and List.Add instead of Array.

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

I suggest using Linq to generate the array:

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();

Upvotes: 1

tomRedox
tomRedox

Reputation: 30443

At the moment you are recreating your array on each loop of your code rather than creating the array outside of the loop and then adding buttons to it in the loop.

In addition all your buttons are being created in the same position, so they are on top of each other meaning you can only see one of them.

Try something like this, based on a modified version of the code from this previous answer which creates the buttons, adds them to a list you can search and the sets the positions of the buttons too:

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

You can then refer to an individual button later with the code:

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

Upvotes: 0

honzakuzel1989
honzakuzel1989

Reputation: 2520

You have to initialize the button array first

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

and after you can fill required items

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

Upvotes: 0

Franky
Franky

Reputation: 651

I mean like this:

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

First you create the array and in the for loop you instantiate the actual buttons one by one.

Upvotes: 0

Related Questions