[RESOLVED] [2008] Treeview & dynamic user controls
Hello Geeks,
I am new (on the board and in programming) so please don't hit me too hard .
Unfortunately I have some problems with a TreeView which is used as a menu in my application. Based on my selection, i want to load the necessary UserControl into my form. The names of my menu items are like "Database", "Advanced", etc.
I created one UserControl for each menu item, which are named "ucDatabase", "ucAdvanced", etc. When i try something like this:
Code:
Me.pcRight.Controls.Add(GetControlByName(tvSettings.SelectedNode.Name))
Function GetControlByName(ByVal Name As String) As Control
' Set uc prefix
Dim ucName As String = Name.Insert(0, "uc")
Return ucName
End Function
I get the following error message:
Value of type 'String' cannot be converted to System.Windows.Forms.Control'
So how can i convert my string to an user control object which can be loaded on my form?
Me.Controls(ucName) will give you the control, if it exists in the forms container.
Thats what youd need to return. Right now youre just returning a string containing the name of the control.
edit: Also, you will be adding the control to pcRights control collection every time a selection has been made, i dont think thats what you really want.
Last edited by Atheist; Feb 12th, 2008 at 06:19 PM.
Unfortunately i don't fully understand what you are talking about
I understand that at the moment i only return a name, but based on this name i want to create a new object from the type of the appropriate user control. So if the name is ucDatabase the code for generating my user control should be something like:
Code:
Dim myNewControl As ucDatabase
Me.pcRight.Controls.Add(myNewControl)
But i am unsure how to solve my problem when i don't know the type of object if have to create (Dim myNewControl As ......) becaused it's based on the name of the NodeName.
I added my project for better understanding. Maybe you or someone else can help me out.
Thanks for any advice
voidpixels
Last edited by voidpixels; Feb 13th, 2008 at 10:31 AM.
Ah, so you want to create a new instance of a specific usercontrol based on the selected treeview node?
It can be done using Activator.CreateInstance, but I wouldnt recommend it. I would do something like this: