Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reflect on dynamic control creation?

Status
Not open for further replies.

woyler

Programmer
Jun 20, 2001
678
0
0
US
Hi all
I want to get some ideas on this before I chase down a dead end;

I am creating controls dynamically based on user input before the form is loaded. ie;
dim lbl as new Label
lbl.name = "lblONE"
me.controls.add(lbl)

So using the above, say I create 5 labels. As they use the program I need to reference these labels to change the taext property. How do I reference the instance of the controls by name?

Thanks in advance,
Bill
 
If you don't have hundreds of controls, the easiest way would be to:

Code:
Function FindControl(ControlName As String) As Control
  For Each c As Control In Me.Controls
    If c.Name = ControlName Then
       Return c
     End If
  Next
  Return Nothing
End Function
 
Riverguy,
Thanks for the response. That is exactly what I am doing no. Problem is that I will have quite a few controls (50-60) and they will be referenced very often. So in short, that approach is working currently, but I am looking for a more efficient way.

regards,
Bill
 
I find the tag property helpful here.

For Each cntrl In Me.Controls
If CType(cntrl.Tag, String) = "userInput" Then
'do something
End If

Hope this helps.
 
Heart of the problem is I am looking for a way to avoid iterating through each control of the parent. After I create the control and it is loaded and displayed there has to be a way to refer to it rather then looking at all the loaded controls.
 
Is the performance hit really that bad on 50-60 controls? they're already in memory, and using a for each control in controls loop uses a reference, so it's not like you're even moving memory.

-Rick

----------------------
 
I guess its not so much the performance hit as it would be doing it in a cleaner fashion. It seemed overkill to me to loop through 50 controls of different types to change one label. But, thats why I posted. If the way I am doing it is the the least intrusive when it is all said and done, thats the way I will go. I just thought that there may be a faster/cleaner way.

regards,
Bill
 
I think the code is clean as it only takes a few lines. But what makes the label change? If the user clicks on it, or interacts with it directly, you could use use it's event handler to change the text.
 
If you want a non-looping solution you could probrably use a hash table to track which label is associated with some key, then pull back the item based on the key.

But there is nothing "unclean" about iterating through a collection.

-Rick

----------------------
 
In a nutshell , I am creating a treeview,label,and checkbox for each "manufacturing container" As data is entered elsewhere in the world, this software detects the data change and need to update the proper container accordingly. So there is no user interaction at that point. The only thing I have is a dataset with the container number from the database that corresponds with the name of the control.
 
just to make sure there isn't any confussion from my post, by "Key" I mean a unique identifier, not a keyboard-key pressed by the user.

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top