illumi
illumi

Reputation: 458

Creating a user control object in runtime

please help me on creating a user control object in runtime using createobject function or whatever better function in vb.net.

here is my code:

Dim b As New Security.Sec_Role
b.Name = "Sec_Role" 
b.visible = true

but i want to use this code:

dim b as object
b = createobject("Security.Sec_Role")

but it always return an error:

Cannot create ActiveX component.

EDIT: i figure it out..thanks a lot..i use this codes:

Dim asm As System.Reflection.Assembly = Assembly.Load("Security")
Dim b As Object = Activator.CreateInstance(asm.GetType("Security.Sec_Role"))

Upvotes: 0

Views: 2164

Answers (3)

Mark Hall
Mark Hall

Reputation: 54562

If it is an .Net UserControl you will not be able to use CreateObject unless it has been exposed as a Com object according to the MSDN page for CreateObject. Using New would be the proper way to create a .Net UserControl.

From above link:

Creates and returns a reference to a COM object. CreateObject cannot be used to create instances of classes in Visual Basic unless those classes are explicitly exposed as COM components.

From this MSDN Forum try something like this using System.Activator.CreateInstance:

Dim oType As System.Type = Type.GetType("MyNamespace.ClassName")
Dim obj = System.Activator.CreateInstance(oType)

Upvotes: 2

competent_tech
competent_tech

Reputation: 44971

You will want to use Activator.CreateInstance:

Dim b as object
b = Activator.CreateInstance(Nothing, "Sec_Role")

It will be easiest if this method is in the assembly which has the controls. Otherwise, you will need to provide the assembly name in the first parameter.

Upvotes: 1

SLaks
SLaks

Reputation: 888205

.Net classes are not ActiveX controls.
You can't do that.

You may be looking for Reflection or a dictionary.

Upvotes: 1

Related Questions