Solved

Dynamic Control Creation

Posted on 2002-07-02
8
232 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:Azmodan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
kpkp earned 50 total points
ID: 7124230
sounds like you need to use the Activator.CreateInstance method...

check out

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemActivatorClassCreateInstanceTopic.asp
0
 

Author Comment

by:Azmodan
ID: 7124675
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?
0
 
LVL 4

Expert Comment

by:kpkp
ID: 7124705
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Azmodan
ID: 7124739
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...

0
 
LVL 4

Expert Comment

by:kpkp
ID: 7124917
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.
0
 

Author Comment

by:Azmodan
ID: 7126341
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?
0
 

Author Comment

by:Azmodan
ID: 7126351
no..i got it
ObjectHandle.Unwrap.. returns a castable pointer. And it can be casted to Forms.Control
0
 

Author Comment

by:Azmodan
ID: 7126473
10x

i'll figure out the details
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question