Jim Porter
Jim Porter

Reputation: 63

Update Dynamically created text box by name withought looping through all controls (VB)

I have a project that creates several buttons dynamically along with a slider (two splitters in a panel).

For the purpose of this lets call the slider "slider9001" and the label "label9001". Each set of control's last 4 digits are different.

Currently I'm looping through all of the controls and looking for a label with the last 4 digits of the slider. This seems incredibly inefficient, especially considering there will be over 100 labels and 40-50 sliders on the form.

Does anyone know of a simple and more efficient way of accessing a dynamically created label

I should mention the slider will be used to update the label

Upvotes: 0

Views: 82

Answers (3)

Trevor Tubbs
Trevor Tubbs

Reputation: 2117

As the labels are generated, store a reference to each one in a dictionary. Use the digits as the key. When you need a label, simply retrieve it from the dictionary. The same approach will work for the sliders as well.

dim labels as new Dictionary(of string, Label)
' add generated labels to dictionary

dim lbl = labels("9001")

Upvotes: 2

Jim Porter
Jim Porter

Reputation: 63

Outstanding! Since I already know the name

Dim lbl as label = ctype(me. Controls("label9001"),label)

Worked perfectly. Thanks Much!!!

Upvotes: 0

user2669338
user2669338

Reputation: 175

If you know the name of the control you can get a reference to it Dim lbl as label = ctype(me. Controls("label9001"),label)

Upvotes: 2

Related Questions