0

First off, I am using a C# WindowsFormApplication. I want to do something such as: int ChecklistItemVariable = 0;

And then when a button is pressed: ChecklistItem{ChecklistItemVariable}.ForeColor = Color.LimeGreen

This way I don’t have to write an individual line of code for when the user is on each line of the checklist. The checklist items will be named “ChecklistItem1, ChecklistItem2” etc.

  • Do you want to [iterate controls](https://stackoverflow.com/q/1499049/1997232) or what is the original problem you are trying to solve? – Sinatr Aug 05 '20 at 10:26
  • 1
    You can store the controls in an array and then access items in the array by index. – Damien_The_Unbeliever Aug 05 '20 at 10:35
  • All variable names are fixed at compile time. So, no you can't do that. - You can change the `Name` property if you like and then either `Find` them in a `Controls`collection or put them in a `Dictionary>string, Control>`. The whole idea however is not a good one imo. Look up XY-problem. You can also store references to the Controls in a class which you add to the CheckList (instead of adding just strings)..- What you also should know the all Controls have a multi-purpose property `Tag` which could be used. – TaW Aug 05 '20 at 10:45
  • Note that ListBoxes by default hold strings or objects, which do not have a`Tag`. So adding a class of your own (which needs a ToString method) is by far the best option. – TaW Aug 05 '20 at 10:48

0 Answers0