Hi all!

As you probably have guessed, I have a problem. And as such I would like to use your collective knowledge so I can continue.

I have a form that has a crapload of buttons on it. so, because I was being lazy, I created myself a ButtonGenerator. Here is the code for that:

public void ButtonGenerator(string ButtonName, string ButtonText, int ButtonHeight, int ButtonWidth, int ButtonTop, int ButtonLeft)
			Button NewButton = new Button();
			NewButton.Name = ButtonName;
			NewButton.Text = ButtonText;
			NewButton.Height = ButtonHeight;
			NewButton.Width = ButtonWidth;
			NewButton.Top = ButtonTop;
			NewButton.Left = ButtonLeft;

so I can use something like this:

ButtonGenerator("ButtonQ", "Q", 20, 20, 60, 20);


Now I require my program to sort of highlight a button. This can be done by changing the backgroundcolor of it, or setting the Selected property to true, or whatever way, I don't really care as long as it is clear which button is about to do it's thing.

But how do I access the buttons? I hoped that by adding a Name property I would be able to acces it somehow. I either I can't figure out how, or that is not how it works.

So... How does it work?

Thanks in advance for any answers provided!

Button B = this.Controls[buttonname];

That should work for what you are looking for.
or you could add each control to a list and access it that way, I can think of a lot of ways, but this is the simplest.

commented: most reliable way to go about it +15
commented: As Einstein said: "Keep it simple, but not simpler" +13

Here is a not so simple way to do it. Ideally using the name as Diamonddrake provided would be the best so you know what button you are referencing. To find all of the buttons you could use this approach:

private void button3_Click(object sender, EventArgs e)
      Button[] btns = (Button[])FindControls(this, typeof(Button));
      foreach (Button btn in btns)
        btn.BackColor = Color.Red;

    private static Array FindControls(Control ctrl, Type t)
      System.Collections.ArrayList result = new System.Collections.ArrayList();
      foreach (Control c in ctrl.Controls)
        if ((c.GetType() == t) || c.GetType().IsSubclassOf(t))
        foreach (Control childCtrl in c.Controls)
          result.AddRange(FindControls(childCtrl, t));
      return result.ToArray(t);
commented: bringing in some finess! +4