Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
Hello, does anybody have a good example how to call one of many UserControls by a string:
I have many classes like:

C#
public partial class UserControl1 : UserControl
public partial class UserControl2 : UserControl
public partial class UserControl3 : UserControl

and main Form, and a string:
C#
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
Updated 14-Mar-11 5:10am
v3
Comments
Manfred Rudolf Bihy 14-Mar-11 10:43am    
Added pre tags, minor spelling and grammar adjustments.
Sergey Alexandrovich Kryukov 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.

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

C#
//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:
C#
UserControl InstanciateMyUserControl(string className)
{
    return types[className].InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, null) as UserControl;
}
 
Share this answer
 
Comments
prromap 14-Mar-11 11:45am    
Finally an Excellent solution!
Olivier Levrey 14-Mar-11 11:52am    
Thank you.
Sergey Alexandrovich Kryukov 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...
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,
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900