-1

I want to reference a GroupBox name using a string.

My code currently works. I want to change this line of code:

Line1.buttonName.BackgroundImage = CircleColours[i];

Line1 is the GrouBox name. I'd like to be able to change Line1 to a string. Like this:

string groupBoxName = "Line1";
groupBoxName.buttonName.BackgroundImage = CircleColours[i];

The code doesn't work when I do this though. What do I need to change?

Imran Ali Khan
  • 8,469
  • 16
  • 52
  • 77
robrob
  • 1

1 Answers1

1

What you're currently trying to do won't work because groupBoxName is just a string, and those properties don't exist on a string.

You need to search the collection of controls on the Form.

string groupBoxName = "Line1";
GroupBox groupBox = (GroupBox)Controls[groupBoxName];
groupBox.buttonName.BackgroundImage = CircleColours[i];

If it's buried within child controls, you'll want to use the Find() method instead:

string groupBoxName = "Line1";
GroupBox groupBox = (GroupBox)Controls.Find("groupBoxName", true)[0];
groupBox.buttonName.BackgroundImage = CircleColours[i];    
Grant Winney
  • 65,241
  • 13
  • 115
  • 165