Load UserControl from existing DLL file
-
Hello all, there is a problem that I want to ask: I have made 2 UserControls using Visual Basic 2005. Let's say the names are UserControl1 and UserControl2 . Both were made under the same project that I name it: WindowsControlLibrary1. On both of them, I put different components, in UserControl1 I use a DataGridView component, and in the other one, I put a TextBox and a Button or something. After scripting on both of them, I built the project and I got 1 dll file: WindowsControlLibrary1.dll. Now, I want to make new project, I create 1 MDI form. And I want to make that MDI form to be the parent of 2 MDI child form: UserControl1 and UserControl2 that I can get from WindowsControlLibrary1.dll. How can I do that, what should I do to get the controls inside the dll file. I want to use it as part of the new project. So that if i call function like: UserControl1.Show(), the UserControl will be appeared under the main form of the new project, and have all function as child form. Can someone help me? If I could do that, next I want to add the path of the dll inside SQL Server database. When I run the main form, it will read the path, and dinamically run them as the MDI child on runtime. Thanks Best Regard, Indra Permana Rusli
- No Signature Available -
-
Hello all, there is a problem that I want to ask: I have made 2 UserControls using Visual Basic 2005. Let's say the names are UserControl1 and UserControl2 . Both were made under the same project that I name it: WindowsControlLibrary1. On both of them, I put different components, in UserControl1 I use a DataGridView component, and in the other one, I put a TextBox and a Button or something. After scripting on both of them, I built the project and I got 1 dll file: WindowsControlLibrary1.dll. Now, I want to make new project, I create 1 MDI form. And I want to make that MDI form to be the parent of 2 MDI child form: UserControl1 and UserControl2 that I can get from WindowsControlLibrary1.dll. How can I do that, what should I do to get the controls inside the dll file. I want to use it as part of the new project. So that if i call function like: UserControl1.Show(), the UserControl will be appeared under the main form of the new project, and have all function as child form. Can someone help me? If I could do that, next I want to add the path of the dll inside SQL Server database. When I run the main form, it will read the path, and dinamically run them as the MDI child on runtime. Thanks Best Regard, Indra Permana Rusli
- No Signature Available -
Indra_PR wrote:
Now, I want to make new project, I create 1 MDI form. And I want to make that MDI form to be the parent of 2 MDI child form: UserControl1 and UserControl2 that I can get from WindowsControlLibrary1.dll.
If your controls are of the type usercontrol you can't do this. You can only add forms as mdi children. To be able to use the controls you created you have to add a reference to the dll. Project --> right mouse click --> add reference --> browse to your dll
-
Indra_PR wrote:
Now, I want to make new project, I create 1 MDI form. And I want to make that MDI form to be the parent of 2 MDI child form: UserControl1 and UserControl2 that I can get from WindowsControlLibrary1.dll.
If your controls are of the type usercontrol you can't do this. You can only add forms as mdi children. To be able to use the controls you created you have to add a reference to the dll. Project --> right mouse click --> add reference --> browse to your dll
But I don't want to link it when I code the new project, I want to link the project and the dll (add as reference) on runtime. Yesterday, I got a reference, telling to use Assembly.Load() Method. I've tried to use it but it only succeed when I made my first project as a WindowsApplication and create Forms inside (not UserControl). Then when I deploy the project, I get an exe file and I can use it well with my project. But when I try use the WindowsControlLibrary with a dll output, I cannot do that, the compiler showed me an error. I don't have my code right now, maybe I'll post it here later. If possible, can someone give me a reference to use the dll and get the controls inside, or maybe give me an example related to this topic. Please ;) Thanks Best Regard, Indra Permana Rusli
- No Signature Available -
-
Hello all, there is a problem that I want to ask: I have made 2 UserControls using Visual Basic 2005. Let's say the names are UserControl1 and UserControl2 . Both were made under the same project that I name it: WindowsControlLibrary1. On both of them, I put different components, in UserControl1 I use a DataGridView component, and in the other one, I put a TextBox and a Button or something. After scripting on both of them, I built the project and I got 1 dll file: WindowsControlLibrary1.dll. Now, I want to make new project, I create 1 MDI form. And I want to make that MDI form to be the parent of 2 MDI child form: UserControl1 and UserControl2 that I can get from WindowsControlLibrary1.dll. How can I do that, what should I do to get the controls inside the dll file. I want to use it as part of the new project. So that if i call function like: UserControl1.Show(), the UserControl will be appeared under the main form of the new project, and have all function as child form. Can someone help me? If I could do that, next I want to add the path of the dll inside SQL Server database. When I run the main form, it will read the path, and dinamically run them as the MDI child on runtime. Thanks Best Regard, Indra Permana Rusli
- No Signature Available -
You have several statements/questions here, some of which are confusing.
Indra_PR wrote:
After scripting on both of them, I built the project and I got 1 dll file: WindowsControlLibrary1.dll.
What scripting are you doing or did you mean compile?
Indra_PR wrote:
How can I do that, what should I do to get the controls inside the dll file. I want to use it as part of the new project. So that if i call function like: UserControl1.Show(), the UserControl will be appeared under the main form of the new project, and have all function as child form.
Normally, you would add a reference and then use the controls you created. If you intend on re-using the same form stle with the controls you created, you might want to make another .dll, create a template form with the custom controls you created and some public properties. You could then create the form (dynamically if really needed) at runtime (and the datagridview would be populated correctly depending on the value of the public properties) regardless where it's called from. Code for creating assembly dynamically:
Try ao = \[Assembly\].LoadFrom(sFullPathIncludingFileName) o = ao.CreateInstance(sAssembly & "." & sClass, True) Catch ex As Exception Throw ex End Try
Indra_PR wrote:
Can someone help me? If I could do that, next I want to add the path of the dll inside SQL Server database. When I run the main form, it will read the path, and dinamically run them as the MDI child on runtime.
I don't think you would want to do this from a SQL database......doesn't make sense to do so. Generally, when calling a .dll natively from SQL, you want the .dll to be performing a complex operation that would perform better outside of SQL or gain 'easier' access to resources (folders, directories, ftp, email, etc); typically, you wouldn't want to display a GUI app. Tackle one part at a time and try to remove some of the complexity you got going on.
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
You have several statements/questions here, some of which are confusing.
Indra_PR wrote:
After scripting on both of them, I built the project and I got 1 dll file: WindowsControlLibrary1.dll.
What scripting are you doing or did you mean compile?
Indra_PR wrote:
How can I do that, what should I do to get the controls inside the dll file. I want to use it as part of the new project. So that if i call function like: UserControl1.Show(), the UserControl will be appeared under the main form of the new project, and have all function as child form.
Normally, you would add a reference and then use the controls you created. If you intend on re-using the same form stle with the controls you created, you might want to make another .dll, create a template form with the custom controls you created and some public properties. You could then create the form (dynamically if really needed) at runtime (and the datagridview would be populated correctly depending on the value of the public properties) regardless where it's called from. Code for creating assembly dynamically:
Try ao = \[Assembly\].LoadFrom(sFullPathIncludingFileName) o = ao.CreateInstance(sAssembly & "." & sClass, True) Catch ex As Exception Throw ex End Try
Indra_PR wrote:
Can someone help me? If I could do that, next I want to add the path of the dll inside SQL Server database. When I run the main form, it will read the path, and dinamically run them as the MDI child on runtime.
I don't think you would want to do this from a SQL database......doesn't make sense to do so. Generally, when calling a .dll natively from SQL, you want the .dll to be performing a complex operation that would perform better outside of SQL or gain 'easier' access to resources (folders, directories, ftp, email, etc); typically, you wouldn't want to display a GUI app. Tackle one part at a time and try to remove some of the complexity you got going on.
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
I made a function like this:
Public Shared Function LoadAssembly(ByVal AssemblyPath As String, ByVal ClassName As String) As Object
Dim objAssembly As Reflection.AssemblyobjAssembly = Reflection.Assembly.LoadFrom(AssemblyPath) Try LoadAssembly = objAssembly.CreateInstance(ClassName, True) Catch ex As Exception MsgBox(ex.Message) Return Nothing End Try If LoadAssembly Is Nothing Then MsgBox("Assembly : " & AssemblyPath & vbNewLine & "Class : " & ClassName) Return Nothing End If
End Function
And I call the function in the form load of the new project:
Dim frm As Form
Try
frm = LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.Form1")
Me.IsMdiContainer = True
frm.MdiParent = Me
frm.Dock = DockStyle.Fill
frm.Show()
Catch ex As Exception
MsgBox(ex.Message)
End TryIn order to make it works, I create 1 form inside the WindowsControlLibrary1.dll that I name it Form1. But when I want to add the UserControl1 inside the form, I don't know what to do next.
Dim usc As Form
Try
usc = DirectCast(LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.UserControl1"), Form)
Me.IsMdiContainer = True
usc.MdiParent = Me
usc.Dock = DockStyle.Fill
usc.Show()
Catch ex As Exception
MsgBox(ex.Message)
End TryThis script returned an error:
Unable to cast object of type 'WindowsControlLibrary1.UserControl1' to type 'System.Windows.Forms.Form'.
I need to add the UserControl1 on my project form. Is there any UserControl Container or something that I should add on my form? :doh:
-
I made a function like this:
Public Shared Function LoadAssembly(ByVal AssemblyPath As String, ByVal ClassName As String) As Object
Dim objAssembly As Reflection.AssemblyobjAssembly = Reflection.Assembly.LoadFrom(AssemblyPath) Try LoadAssembly = objAssembly.CreateInstance(ClassName, True) Catch ex As Exception MsgBox(ex.Message) Return Nothing End Try If LoadAssembly Is Nothing Then MsgBox("Assembly : " & AssemblyPath & vbNewLine & "Class : " & ClassName) Return Nothing End If
End Function
And I call the function in the form load of the new project:
Dim frm As Form
Try
frm = LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.Form1")
Me.IsMdiContainer = True
frm.MdiParent = Me
frm.Dock = DockStyle.Fill
frm.Show()
Catch ex As Exception
MsgBox(ex.Message)
End TryIn order to make it works, I create 1 form inside the WindowsControlLibrary1.dll that I name it Form1. But when I want to add the UserControl1 inside the form, I don't know what to do next.
Dim usc As Form
Try
usc = DirectCast(LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.UserControl1"), Form)
Me.IsMdiContainer = True
usc.MdiParent = Me
usc.Dock = DockStyle.Fill
usc.Show()
Catch ex As Exception
MsgBox(ex.Message)
End TryThis script returned an error:
Unable to cast object of type 'WindowsControlLibrary1.UserControl1' to type 'System.Windows.Forms.Form'.
I need to add the UserControl1 on my project form. Is there any UserControl Container or something that I should add on my form? :doh:
The form IS the container. You're trying to treat a UserControl as a Form, which won't work at all. You really need to rethink your design because what you're trying to do now just isn't going to work. The only option you have, with what you've got now, is to put that usercontrol on a form.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
The form IS the container. You're trying to treat a UserControl as a Form, which won't work at all. You really need to rethink your design because what you're trying to do now just isn't going to work. The only option you have, with what you've got now, is to put that usercontrol on a form.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Wuaaaa thanks, I've modified my code.
Dim usc As UserControl
Try
Me.WindowState = FormWindowState.Maximized
usc = LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.UserControl1")
usc.Dock = DockStyle.Fill
Me.Controls.Add(usc)
Catch ex As Exception
MsgBox(ex.Message)
End TryIt works :laugh: Then I add uiBase.vb (UserControl) inside WindowsControlLibrary1, and made the UserControl1 inherits the uiBase. But when I was trying to call with "WindowsControlLibrary1.UserControl1" it failed to load. I've tried to call with parameter "WindowsControlLibrary1.uiBase.UserControl1" too, but it still returned me an error. Ooooh, I need help here :doh: Thanks Best Regard, Indra Permana Rusli
- No Signature Available -
-
Wuaaaa thanks, I've modified my code.
Dim usc As UserControl
Try
Me.WindowState = FormWindowState.Maximized
usc = LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.UserControl1")
usc.Dock = DockStyle.Fill
Me.Controls.Add(usc)
Catch ex As Exception
MsgBox(ex.Message)
End TryIt works :laugh: Then I add uiBase.vb (UserControl) inside WindowsControlLibrary1, and made the UserControl1 inherits the uiBase. But when I was trying to call with "WindowsControlLibrary1.UserControl1" it failed to load. I've tried to call with parameter "WindowsControlLibrary1.uiBase.UserControl1" too, but it still returned me an error. Ooooh, I need help here :doh: Thanks Best Regard, Indra Permana Rusli
- No Signature Available -
Refer back to the replies from Dave and I.
Indra_PR wrote:
Dim usc As UserControl Try Me.WindowState = FormWindowState.Maximized usc = LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.UserControl1") usc.Dock = DockStyle.Fill Me.Controls.Add(usc) Catch ex As Exception MsgBox(ex.Message) End Try
Where is this code snippet located. What is "me" in this instance? Is me referring to a form?
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Refer back to the replies from Dave and I.
Indra_PR wrote:
Dim usc As UserControl Try Me.WindowState = FormWindowState.Maximized usc = LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.UserControl1") usc.Dock = DockStyle.Fill Me.Controls.Add(usc) Catch ex As Exception MsgBox(ex.Message) End Try
Where is this code snippet located. What is "me" in this instance? Is me referring to a form?
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Yep, me refers to a form. It has no problem, the problem is in here:
usc = LoadAssembly("C:\WindowsControlLibrary1.dll", "WindowsControlLibrary1.UserControl1")
- No Signature Available -