By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Use a variable name as a control

P: 10
Hi,
How can I use a variable as a control name?
I'll try to make sample code very simple.

Expand|Select|Wrap|Line Numbers
  1.  names(1)='treeOrders'
  2. names(2)='treeProducts'
  3. names(3)='treeInvoices'
  4.  
  5. for i = 1 to 3 
  6.     tv='frmMainMenu' & names(i)
  7.     tv.node.add='abc'
  8.     tv.node.add='123'
  9. next i
  10.  
My problem is that I do not know how to make "tv" a valid control.
Sep 21 '07 #1
Share this Question
Share on Google+
4 Replies


QVeen72
Expert 100+
P: 1,445
Hi,
How can I use a variable as a control name?
I'll try to make sample code very simple.

Expand|Select|Wrap|Line Numbers
  1.  names(1)='treeOrders'
  2. names(2)='treeProducts'
  3. names(3)='treeInvoices'
  4.  
  5. for i = 1 to 3 
  6.     tv='frmMainMenu' & names(i)
  7.     tv.node.add='abc'
  8.     tv.node.add='123'
  9. next i
  10.  
My problem is that I do not know how to make "tv" a valid control.
Hi,

Check this :

Expand|Select|Wrap|Line Numbers
  1.   Dim tv
  2.   Dim ctl
  3.   Dim i As Integer
  4.   For i = 1 To 3
  5.     For Each ctl In Me.Controls
  6.         If ctl.Name = names(i) Then
  7.             Set tv = Nothing
  8.             Set tv = ctl
  9.              tv.node.add='abc'
  10.              tv.node.add='123'
  11.             Exit For
  12.         End If
  13.     Next
  14.   Next i
  15.  

Regards
Veena
Sep 21 '07 #2

P: 10
I think my example might have been too simple and I did not explain enough.

I have a tabControl with a treeView on each tab.
Each tree will have a name like treeXXXXX (see names() in code sample). There is a data file for each tree with the same name as the tree.
I will read a text file for each tree to get the data to populate the trees.
The code is more like this: (I am new to VB. I realize my read & loop code is not correct. I am only interested in how to assign the string variable to the control name.)
Expand|Select|Wrap|Line Numbers
  1.  names(1)='treeOrders'
  2. names(2)='treeProducts'
  3. names(3)='treeInvoices'
  4. for i = 1 to 3 
  5.      read stuff from files, names(i) 
  6.      tv='frmMainMenu.' & names(i) '<-- this needs to be the name of the tree
  7.      for j = 1 to lines.in.stuff
  8.         tv.node.add=stuff(j) '<-- this is probably my big problem
  9.      next j
  10. next i
So...
line 6 creates a string with the name of the control.
something prior to line 8 has to convert the string "tv" to a control.name.

The ability to do this is key to my application. I assume I can do things like:
nm="TxtName"
nm.text='Jim'
This will obviously not work either because nm is a string, not a control. I think the solution to the code sample will give me the answer to this and other similar needs.

Thanks. (Sorry to be so verbose!)
Sep 21 '07 #3

P: 7
The ability to do this is key to my application. I assume I can do things like:
nm="TxtName"
nm.text='Jim'
Actually, you can't do that directly.

You need to define a Control variable and set that variable to be the one that you're trying to match. Or create a new one (if that's what you want).

Instead of the above, all of the controls in the form are in a collection called "Controls", thus Veena's description of "Me.Controls" listed in that code.

So, to go further, you need to parse through the controls (or better yet, make your own collection of the treeviews that you're looking for, with the name as the index), find the one that you're looking for, then assign the values that you want.

pseudocode example
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim myTreeViews as Collection
  3.  
  4. Public Sub Form_Load()
  5.   Set myTreeViews as New Collection
  6.   myTreeviews.Add me.TreeView1, Me.Treeview1.Name
  7.   myTreeviews.Add me.TreeView2, Me.Treeview2.Name
  8.   myTreeviews.Add me.TreeView3, Me.Treeview3.Name
  9. End Sub
  10.  
  11. Public Sub LoadFileName(TreeViewName as String)
  12.   Dim tv as TreeView
  13.   Set tv = myTreeViews(TreeViewName)
  14.   tv.Text = "Whatever"
  15. End Sub
  16.  
Sep 21 '07 #4

P: 10
Thanks,

This is starting to make some sense to me.

You idea should let me accomplish what I'm trying to do.
Sep 23 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.