Link to home
Start Free TrialLog in
Avatar of Azmodan
Azmodan

asked on

Dynamic Control Creation

I would like to dynamically load a control, a control that I don;t know of at desgin-time.
In VB6 there was a Controls.Add were u suplied a ClsID and the control was loaded.
There must be something like this in C#, a way to load at run-time a dll with a UserControl in it and be able to put it on screen.
ASKER CERTIFIED SOLUTION
Avatar of kpkp
kpkp
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Azmodan
Azmodan

ASKER

yes.this seems to be the way to go, but i couldn;t make it work

hdlSample=Activator.CreateInstance("C:\\Simplex.dll","Simplex.UserControl1");

raises an exception:  File or assembly name C:\Simplex.dll, or one of its dependencies, was not found.

what am i doing wrong?
Are you including a refernce to simplex.dll in your project (or including it in the csc command if you're not using vs.net)?  You need to do this - or register simplex.dll in the GAC.
Avatar of Azmodan

ASKER

No,thats the whole point... to load a control at runtime!!

So, just with code, not by adding it to the references at design-time.

Can this be done without the GAC? By giving it the Asembly, or adding it's reference at run time...

If your assembly that you want to use dynamically isn't in the GAC, then it must be in the same directory as the calling application.  Also - don't include the path or .dll extension in the CreateInstance() call.
Avatar of Azmodan

ASKER

yeah..that seams to work. If i don't put the .dll in its name, it takes it from the current directory.

But... the CreateInstance returns an object or ObjectHandle. If I want to put the loaded control on the form, I need to use Controls.Add... which takes a Windows.Forms.Control. And the system won;t cast object to control. Is there a way?
Avatar of Azmodan

ASKER

no..i got it
ObjectHandle.Unwrap.. returns a castable pointer. And it can be casted to Forms.Control
Avatar of Azmodan

ASKER

10x

i'll figure out the details