473,651 Members | 2,647 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 7428
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.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
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
2456
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 variable name for the case of non array variables. Also I want to show the array name. Is there any way that the variable name that is passed to the function can be displayed. function formattedoutput($object) {
4
3243
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 name="Banner_error_2"> errormessage 2 for banner </xsl:variable>
11
29794
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 checkbox over the Windows ones to turn them all on or all off, and the same with the solaris. One of the control check boxes looks something like this: <input type="checkbox" name="objRS("ProductId") & "_solaris" value=""0""...
7
1978
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(); somename.show() Note that the class name "somename" is reused for the variable name.
2
1466
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 referring page through the URL. The problem I'm having is that when the data on the survey page is submitted through formmail, I can't seem to get the referring page's name sent too - I keep getting the variable name itself, not the contents of the...
8
1696
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
2152
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 through each one but with .NET I'm having trouble doing something just as efficient. Is there not a way to refer to the control by its name or do I have to go find its index number in order to point to it?
2
2109
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, depending on the file, could have a different variable name in each file. I have named the module, and want to name it according to the name of the variable (ie, modulename Joe, var $Joe) Now, this variable needs to be loaded into a function, but...
3
974
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 number making the variable name btn1, btn2 etc
6
2617
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 textboxes is displayed depending on a number chosen by the user. For example, if the user chooses 3, there will be three rows displayed with 4 texts boxes in each row. Using a for loop to create the textboxs, I've appended a number to the ID...
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.