Click here to Skip to main content
11,811,971 members (58,444 online)
Rate this: bad
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
Edited 14-Mar-11 5:10am
Manfred R. Bihy at 14-Mar-11 10:43am
Added pre tags, minor spelling and grammar adjustments.
SAKryukov at 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.
Toli Cuturicu at 14-Mar-11 18:35pm
Non-sense. Have my One.
Rate this: bad
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;
prromap at 14-Mar-11 11:45am
Finally an Excellent solution!
Olivier Levrey at 14-Mar-11 11:52am
Thank you.
SAKryukov at 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.
Olivier Levrey at 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 at 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 at 17-Mar-11 9:58am
Good effort, my 5
Rate this: bad
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.


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

  Print Answers RSS
0 KrunalRohit 354
1 Palash Mondal_ 185
2 CHill60 180
3 OriginalGriff 178
4 Abhinav S 175
0 OriginalGriff 3,888
1 KrunalRohit 2,392
2 CPallini 2,240
3 Maciej Los 2,155
4 ppolymorphe 1,725

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 14 Mar 2011
Copyright © CodeProject, 1999-2015
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