Solved

Dynamic Control Creation

Posted on 2002-07-02
8
227 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
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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