Solved

Dynamic Control Creation

Posted on 2002-07-02
8
223 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
  • 5
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
kpkp earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Azmodan
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Expert Comment

by:kpkp
Comment Utility
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
Comment Utility
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
Comment Utility
no..i got it
ObjectHandle.Unwrap.. returns a castable pointer. And it can be casted to Forms.Control
0
 

Author Comment

by:Azmodan
Comment Utility
10x

i'll figure out the details
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now