Click here to Skip to main content
12,300,607 members (61,042 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# User-controls
Hello, does anybody have a good example how to call one of many UserControls by a string:
I have many classes like:

public partial class UserControl1 : UserControl
public partial class UserControl2 : UserControl
public partial class UserControl3 : UserControl
and main Form, and a string:
public partial class MainForm : Form
string[] callUserControl = {UserControl1,UserControl2,UserControl3};

In specific event, I would like to call a UserControlX from a string[X], and pass that control to a panel.
Those are names of user control classes, and i want to pass the type of a control to the panel but by selecting it from a string.
Something like a Reflection

Thanks in advance!
Posted 14-Mar-11 4:37am
prromap326
Edited 14-Mar-11 5:10am
v3
Comments
Manfred R. Bihy 14-Mar-11 10:43am
   
Added pre tags, minor spelling and grammar adjustments.
SAKryukov 14-Mar-11 16:20pm
   
Don't say what you don't understand! This is nothing like Reflection.
This is very dirty programming relying on hard-coded (immediate) constants.
The whole design is wrong, should be re-considered.
Also, "Call UserControl makes no sense". The Question demonstrate confusion in basic concepts.
--SA
Toli Cuturicu 14-Mar-11 18:35pm
   
Non-sense. Have my One.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

For example, you can use a Dictionary that gives the type you want from the string, and then instanciate the type:

//associates a type to a string
Dictionary<string, Type> types = new Dictionary<string, Type>();
//add your types
types["UserControl1"] = typeof(UserControl1);
types["UserControl2"] = typeof(UserControl2);
types["UserControl3"] = typeof(UserControl3);

To instanciate the type from the string:
UserControl InstanciateMyUserControl(string className)
{
    return types[className].InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, null) as UserControl;
}
  Permalink  
Comments
prromap 14-Mar-11 11:45am
   
Finally an Excellent solution!
Olivier Levrey 14-Mar-11 11:52am
   
Thank you.
SAKryukov 14-Mar-11 16:17pm
   
It looks like an excellent solution by OP who welcomes unacceptable practice of selecting an object by name (or any other hard-coded string), which is dirty and not supportable.
There is a little value in answer so I cannot give a good vote.
I cannot down-vote your answer because this is not your fault. I won't vote this time.
--SA
Olivier Levrey 15-Mar-11 5:08am
   
Actually you are right. I first wanted to add some comments about considering another solution but I was lazy this time...
prromap 15-Mar-11 11:35am
   
I don't agree with you, i get the solution i wanted, i fits the purpose, "dirty and not supportable", it's your opinion but in my and certain cases it's the ultimate...
Espen Harlinn 17-Mar-11 9:58am
   
Good effort, my 5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Do you mean you want to pass the type of a control to the panel or are you talking about an instance of one of your user controls? I'm asking this because your the names in the array intializer look like the names of your user control classes.

Regards,
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160525.2 | Last Updated 14 Mar 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100