Suppose there are a number of elements on a form: label1
, label2
, etc. The user enters the number of the field that he wants to change, and his value. How to access a field by a specific number without using a switch
?
- oneWhat graphic framework do you use? - VladD
- @VladD using CLR - Elmir pm
- And what is a graphical framework? (By CLR, you mean probably C ++ / CLI.) - VladD 4:16 pm
- @VladD Windows Forms - Elmir 4:21 pm
- Yeah, Windows Forms. - VladD 4:21 pm
2 answers
The container (form, panel, etc.) containing labels (label) has a Controls property that returns a collection of child controls. Make a foreach
on this set and you can access each tag in turn.
If you need to get a specific label by number, it would be more correct to create an array (or even an int
-> Label
dictionary), fill it once with the specified labels, and then access the index.
There is another option: use ControlCollection::Find
on the list, which will return the Controls
property mentioned above and directly use the name label1
, label2
, etc. In general, there are quite a few options.
I figured it out myself. The main problem was that you cannot add labels to a regular array of type Label^
, and to the vector as well. But you can do this:
array <Label^>^ labels={label1,label2,...};
After that, you can refer to the labels as follows:
labels[i]->Visible=0;
And, accordingly, run on them in a cycle.