[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 :D .
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?
Thanks for any input
voidpixels
Re: [2008] Treeview & dynamic user controls
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.
1 Attachment(s)
Re: [2008] Treeview & dynamic user controls
Atheist, thanks for your reply.
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
Re: [2008] Treeview & dynamic user controls
I am sorry for being hasty, but is anyone there who could throw some light in this?
Re: [2008] Treeview & dynamic user controls
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:
vb Code:
Select Case tvSettings.SelectedNode.Name
Case "Database"
Dim uc As New ucDatabase
uc.Location = New Point(50, 50)
Me.Controls.Add(uc)
Case "Advanced"
Dim uc As New ucAdvanced
uc.Location = New Point(50, 50)
Me.Controls.Add(uc)
End Case