Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am made a project (windows form, vb.net)
My mean is how to add dynamic a user control to panel by name of user control.
I had made with form by below:
VB
Dim a As Assembly = Assembly.GetEntryAssembly()
           Dim t As System.Type = a.[GetType](formName)
           Dim o As Object = System.Activator.CreateInstance(t, True)
           Dim frm As Form = DirectCast(o, Form)

frm.show()

but I can't with user control
VB
Dim a As Assembly = Assembly.GetEntryAssembly()
Dim t As System.Type = a.[GetType](controlName)                        ' -->nothing
          Dim o As Object = System.Activator.CreateInstance(t, True)
          Dim ctlAs System.Windows.Forms.UserControl= DirectCast(o, System.Windows.Forms.UserControl)</pre>
pnHolder.control.add(ctl)          

Can You help me, Plz?
Posted

1 solution

Not sure if this is the problem, assuming you are using VB.Net (and not C# as the Tag says).

VB
pnHolder.control.add(ctl)


It should read:

pnHolder.controls.add(ctl)

You should also declare your user control before you can use it in your code. For example, I have a UserControl Class called _clUserControl. In the form where you wish to add the UserControl to the Panel control, you have to declare the UserControl first, e.g.:

VB
Private clUserControl As New _clUserCOntrol 'Declare the User Control


In your code where you wish to add the control to the Panel Control, simply add the the control:

VB
myPanel.Controls.Add(clUserControl)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900