473,503 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use a variable name as a control

10 New Member
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
4 7414
QVeen72
1,445 Recognized Expert Top Contributor
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
Jeff G
10 New Member
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
alan4cast
7 New Member
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
Jeff G
10 New Member
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

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

Similar topics

8
2445
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the...
4
3224
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
11
29788
by: Pete Mahoney | last post by:
I am currently working on an ASP page where I create a lot of different check boxes. I have some checkboxes that are Windows platforms and some that are solaris platforms. I want a control...
7
1967
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
2
1460
by: Ian Hubling | last post by:
Here's what I'm trying to do: I have a large number of pages that call a pop-up survey page. It is important to know what page the survey is referring to - so I am passing the name of the...
8
1682
by: Ankit Aneja | last post by:
I'm trying to use a for() loop to go through a set of labels and set their visibility to false. I had to do something like Code: for(int i=1;i<=10;i++) { labeli.visible = false;
2
2140
by: GY2 | last post by:
How do I refer to controls by a string variable name instead of by integer item index? A form contains 7 rich textbox controls labeled rtb1-rtb7. In VB6 I could use the following code to loop...
2
2097
by: paladin.rithe | last post by:
I have no clue if this can be done, but I thought I'd ask anyway. I have a modular system where I store module information in a database. One of the types of modules that I have is one that,...
3
970
by: mosscliffe | last post by:
I have several buttons on a page btn1, btn2, btn3, btn4 ... btnn I wish to control their visibilty for x = 1 to btnCount btn.visible = false next x where the x is used to convert to a...
6
2610
by: encoad | last post by:
Hi everyone, I've run into another road block that google.com isn't able to solve since I don't even know where to start when searching. In my webapp, a certain number of table rows containing...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.