|
-
Sep 21st, 2007, 02:19 AM
#1
Thread Starter
New Member
[2005] 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.
-
Sep 21st, 2007, 02:35 AM
#2
Re: [2005] Use a variable name as a control
vb.net Code:
Dim ctl As Control = Me.Controls(controlName)
-
Sep 21st, 2007, 07:30 AM
#3
Re: [2005] Use a variable name as a control
Is tv a TreeView control?
-
Sep 21st, 2007, 02:56 PM
#4
Thread Starter
New Member
Re: [2005] Use a variable name as a control
Yes, tv is a treeview.
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:
[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 21st, 2007, 07:17 PM
#5
Re: [2005] Use a variable name as a control
You say this:
The ability to do this is key to my application.
but you are not the first person to ask how to do this sort of thing and it's usually a sign of faulty design. Firstly, there's no point doing this:
vb.net Code:
names(1)='treeOrders'
names(2)='treeProducts'
names(3)='treeInvoices'
tv='frmMainMenu.' & names(i)
when you can do this (or some close variation of it):
vb.net Code:
trees(1) = Me.treeOrders
trees(2) = Me.treeProducts
trees(3) = Me.treeInvoices
tv = Me.trees(i)
Apart from that, there are almost always better ways to access your controls than by name like that. The only reason you would genuinely need to access a control by name is if you were getting that name from an external source that had to be stored as a string. If everything is internal to your app then there is NEVER a need to access controls by name like that.
-
Sep 23rd, 2007, 02:43 PM
#6
Thread Starter
New Member
Re: [2005] Use a variable name as a control
Thanks for the reply.
The only reason you would genuinely need to access a control by name is if you were getting that name from an external source that had to be stored as a string.
Actually that is what I am doing. I am reading files (or records in a file) and the name of the file is part of the name of the tree. So the tree name really is a string.
-
Sep 23rd, 2007, 05:50 PM
#7
Re: [2005] Use a variable name as a control
I would have to disagree with that based on what you've posted so far. You've got trees for Orders, Products and Invoices. You already know that. It's not like you have a bunch of trees that could be for anything at all. You know for a fact that they are for those three types of records. That means that you can specifically read the file containing the Order data and load it into the Order tree. From the information you've provided us to date, you should be hard-coding that.
-
Sep 23rd, 2007, 06:41 PM
#8
Thread Starter
New Member
Re: [2005] Use a variable name as a control
I was trying to keep the example simple so I did not explain everything.
There is a Menus file in the database with each record being a menu prompt and associated process. The Main Menu contains the names of the sub menus.
I just finished a working example (using simple text files).
Code:
Dim i As Integer = 0
Dim node_name As String
Dim node_cmd As String
Dim menuLine As String
Dim nodeLine As String
' loop through main menu list
Dim srMainMenu As StreamReader = New StreamReader("c:\VBTest\Menulist.txt")
Do
menuLine = srMainMenu.ReadLine()
If menuLine > "" Then
' each menu item should be a previously defined TreeView
Dim controls() As Control = frmMainMenu.Controls.Find("Tree" & menuLine, True)
Dim treName As TreeView = DirectCast(controls(0), TreeView)
If controls.Length > 0 Then
treName.Width = 350
treName.Nodes.Clear()
Dim sr As StreamReader = New StreamReader("c:\VBTest\" & menuLine & ".txt")
Do
nodeLine = sr.ReadLine()
If nodeLine > "" Then
i = InStr(nodeLine, vbTab)
node_name = Mid(nodeLine, 1, i - 1)
node_cmd = Mid(nodeLine, i + 1)
treName.Nodes.Add(node_cmd, node_name)
End If
Loop Until nodeLine Is Nothing
sr.Close()
End If
End If
Loop Until menuLine Is Nothing
srMainMenu.Close()
What I am attempting to do is duplicate my current application with VB. The ap is currently written in Pick/D3, which is a very powerful environment, but lacks any native GUI tools.
One of the biggest assets of the ap is the ability to add things to menus by updating the Menus file. The next time the user logs in, the new menus are available.
My goal is for most of the application to be parameter driven. For example, every entry screen is built using a parameter file. A template will be used to create entry screens from the parameter file, so that if a parameter is changed, the new screen will have the changes.
From what I have seen so far, I think this is do-able.
I have also seen that this will be a challange for me to get there.
I'm sure I will be posting more questions as I progress.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|