2

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
}
2
  • 2
    The button array is initialized within the for loop and therefore only exists there. Create the array before the loop and only create single buttons. But are you adding those buttons to a form at some point? Commented Jan 14, 2016 at 7:01
  • Yes. I add those buttons to a form at the same point. I still confused with only create single buttons. Can you explain it to me ? @Franky Commented Jan 14, 2016 at 7:04

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

A typo: New Point instead of new Point; you, probably, want assign Size as well: tombol.Size = new Size(50, 20);; safier is not to use magic numbers and constants noOfBtns in the for loop: Button[] Tombols = new Button[noOfBtns]; then for(int cnt = 0; cnt < Tombols.Length; ++cnt)
I have tried this and it doesn't work :( That's button's color still white and doesn't change to red. 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);
Yes. I update that code. First I want to try change button's color on the specific coordinate. The button's coordinate is 999, 415 and its name is B11. It doesn't work.. Can you please help me ? :(
I want to change Back Color. I have made a button on the coordinate of (999, 415) and its size is (25, 40). Its name is B11. I want to try change its color first and it doesn't work.
1

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

Comments

0

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.

Comments

0

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

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.