|
-
Oct 17th, 2008, 06:03 AM
#1
Thread Starter
Fanatic Member
[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 :-(
-
Oct 17th, 2008, 06:10 AM
#2
Re: Load a user control onto form using reflection
Was it something like this?
VB.NET Code:
Dim dllAssembly As Reflection.Assembly = Reflection.Assembly.LoadFile("C:\ATI\uc.dll")
Dim ctrl As Control = CType(dllAssembly.CreateInstance(String.Format("{0}.{1}", dllAssembly.GetName().Name, "UserControl1")), Control)
ctrl.Location = New Point(10, 10)
Me.Controls.Add(ctrl)
-
Oct 17th, 2008, 06:57 AM
#3
Thread Starter
Fanatic Member
Re: Load a user control onto form using reflection
Cool, but ctrl always returns NULL.
-
Oct 17th, 2008, 06:58 AM
#4
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
-
Oct 17th, 2008, 07:35 AM
#5
Thread Starter
Fanatic Member
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.
-
Oct 17th, 2008, 07:41 AM
#6
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:
Dim dllAssembly As Reflection.Assembly = Reflection.Assembly.LoadFile("YourDLL.dll")
Dim fullName As String = String.Format("{0}.{1}", dllAssembly.GetName().Name, "YourControl")
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:
For Each t As Type In dllAssembly.GetExportedTypes()
MessageBox.Show(t.FullName)
Next
Do you find your UserControl among those exported types? What was its fullname?
-
Oct 17th, 2008, 10:19 AM
#7
Thread Starter
Fanatic Member
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.
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
|