Results 1 to 5 of 5

Thread: [RESOLVED] [2008] Treeview & dynamic user controls

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Location
    Switzerland
    Posts
    4

    Resolved [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?

    Thanks for any input
    voidpixels

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Last edited by Atheist; Feb 12th, 2008 at 06:19 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Location
    Switzerland
    Posts
    4

    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
    Attached Files Attached Files
    Last edited by voidpixels; Feb 13th, 2008 at 10:31 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Location
    Switzerland
    Posts
    4

    Re: [2008] Treeview & dynamic user controls

    I am sorry for being hasty, but is anyone there who could throw some light in this?

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Select Case tvSettings.SelectedNode.Name
    2.     Case "Database"
    3.         Dim uc As New ucDatabase
    4.         uc.Location = New Point(50, 50)
    5.         Me.Controls.Add(uc)
    6.     Case "Advanced"
    7.         Dim uc As New ucAdvanced
    8.         uc.Location = New Point(50, 50)
    9.         Me.Controls.Add(uc)
    10. End Case
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width