Use a variable name as a control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff G
    New Member
    • Sep 2007
    • 10

    Use a variable name as a control

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

    Code:
     names(1)='treeOrders'
    names(2)='treeProducts'
    names(3)='treeInvoices'
     
    for i = 1 to 3 
    	tv='frmMainMenu' & names(i)
    	tv.node.add='abc'
    	tv.node.add='123'
    next i
    My problem is that I do not know how to make "tv" a valid control.
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Originally posted by Jeff G
    Hi,
    How can I use a variable as a control name?
    I'll try to make sample code very simple.

    Code:
     names(1)='treeOrders'
    names(2)='treeProducts'
    names(3)='treeInvoices'
     
    for i = 1 to 3 
    	tv='frmMainMenu' & names(i)
    	tv.node.add='abc'
    	tv.node.add='123'
    next i
    My problem is that I do not know how to make "tv" a valid control.
    Hi,

    Check this :

    [code=vb]
    Dim tv
    Dim ctl
    Dim i As Integer
    For i = 1 To 3
    For Each ctl In Me.Controls
    If ctl.Name = names(i) Then
    Set tv = Nothing
    Set tv = ctl
    tv.node.add='ab c'
    tv.node.add='12 3'
    Exit For
    End If
    Next
    Next i
    [/code]


    Regards
    Veena

    Comment

    • Jeff G
      New Member
      • Sep 2007
      • 10

      #3
      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.)
      Code:
       names(1)='treeOrders'
      names(2)='treeProducts'
      names(3)='treeInvoices'
      for i = 1 to 3 
      	 read stuff from files, names(i) 
      	 tv='frmMainMenu.' & names(i) '<-- this needs to be the name of the tree
      	 for j = 1 to lines.in.stuff
      		tv.node.add=stuff(j) '<-- this is probably my big problem
      	 next j
      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!)

      Comment

      • alan4cast
        New Member
        • Sep 2007
        • 7

        #4
        Originally posted by Jeff G
        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.Control s" 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
        Code:
        Dim myTreeViews as Collection
        
        Public Sub Form_Load()
          Set myTreeViews as New Collection
          myTreeviews.Add me.TreeView1, Me.Treeview1.Name
          myTreeviews.Add me.TreeView2, Me.Treeview2.Name
          myTreeviews.Add me.TreeView3, Me.Treeview3.Name
        End Sub
        
        Public Sub LoadFileName(TreeViewName as String)
          Dim tv as TreeView
          Set tv = myTreeViews(TreeViewName)
          tv.Text = "Whatever"
        End Sub

        Comment

        • Jeff G
          New Member
          • Sep 2007
          • 10

          #5
          Thanks,

          This is starting to make some sense to me.

          You idea should let me accomplish what I'm trying to do.

          Comment

          Working...