Results 1 to 7 of 7

Thread: [RESOLVED] Load a user control onto form using reflection

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Resolved [RESOLVED] Load a user control onto form using reflection

    Hi All,

    About 2 years ago I wrote an application that loaded a bunch of user controls onto a form using reflection but being the typical dik I can't find the bleeding code.

    I have an assembly "myassembly.dll" which contains a control called "mycontrol".
    I have dropped this into the bin of my application which contains a form and a panel. I jsut wnat to load the control into the panel !!

    Can someone pint me in the right direction ?

    I have to stop throwing my code away :-(

    Parksie

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

    Re: Load a user control onto form using reflection

    Was it something like this?

    VB.NET Code:
    1. Dim dllAssembly As Reflection.Assembly = Reflection.Assembly.LoadFile("C:\ATI\uc.dll")
    2.         Dim ctrl As Control = CType(dllAssembly.CreateInstance(String.Format("{0}.{1}", dllAssembly.GetName().Name, "UserControl1")), Control)
    3.         ctrl.Location = New Point(10, 10)
    4.         Me.Controls.Add(ctrl)
    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
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Load a user control onto form using reflection

    Cool, but ctrl always returns NULL.

    Parksie

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

    Re: Load a user control onto form using reflection

    Ah yes I forgot to mention, you'd need to change "UserControl1" to hold whatever is the name of your usercontrol, incase you havent already done that
    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)

  5. #5

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Load a user control onto form using reflection

    Yeah, I did dio that but still no joy. It must be something I'm doing.
    My head is full of messy thinking today.

    Parksie

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

    Re: Load a user control onto form using reflection

    Well...CreateInstance takes a string argument that needs to be the full name of the type you want to create, if its not returning anything, you might want to check that the correct string is passed to it.

    Run this:
    VB.NET Code:
    1. Dim dllAssembly As Reflection.Assembly = Reflection.Assembly.LoadFile("YourDLL.dll")
    2. Dim fullName As String = String.Format("{0}.{1}", dllAssembly.GetName().Name, "YourControl")
    3. MessageBox.Show(fullName)
    What did it show you?

    Now, have a look at the types that your DLL's assembly exposes by doing:

    VB.NET Code:
    1. For Each t As Type In dllAssembly.GetExportedTypes()
    2.             MessageBox.Show(t.FullName)
    3.         Next

    Do you find your UserControl among those exported types? What was its fullname?
    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)

  7. #7

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Load a user control onto form using reflection

    My mistake.

    I had my control in a sub folder called Controls meaning I should have called my control "Controls.MyControl".

    Thanks.
    My stupidity is yet again to blaim.

    Parksie

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