Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I make a function has 1 parameter recieves the type of the Control like(Button, GroupBox,Panel,TextBox,Label ...etc)

private void CreateControl()
{
Button myButton=new Button();
this.Controls.Add(myButton);
}

I need to put a paramter to spicify the type of the control not always a button.

Please help
Posted

Well, you could do this:
C#
private void CreateControl(Type controlType)
{
    Control control = Activator.CreateInstance(controlType) as Control;
    if (control != null) Controls.Add(control);
}
 
Share this answer
 
That's a little complex, because it depends to a large extent of what kind of parameter you want to pass: You need a parameter which tells you what type of control to create, which means passing it a string which is the class name, or a Type which indicates what type the control should be - and that's going to make your external code to call it complex - but how complex I can't tell.
Generally, this kind of aproach is not a good idea - it's possible, but that doesn't mean you should do it - it normally means your design is overcomplicated or badly thought out. There are exceptions though.

One way to do it is via generics:
C#
private void CreateControl<T>() where T : Control, new()
    {
    T t = new T();
    Controls.Add(t);
    }
You then create the instance like this:
C#
CreateControl<Button>();

Otherwise, it's parameter types and Reflection using Activator.CreateInstance[^]
 
Share this answer
 
Hello ,

Here is the sample code. Just take the combobox/Dropdown & list the control which you want to Create Dynamically & assign some value.
Take the Button to execute the code on Click.
For Example textbox as value 1,label as value 2
and Call Function CreateControl() & apply swith case.

Here is the small Code;
Take one integer as global variable
int cleft = 1; 

the variable is for the positioning purpose.

Then Modify your function CreateControl()


<pre lang="c#">private void CreateControl()
{
switch (urDropdown.selectedvaue)
                    {
                        case 1://TextBox
                            Button myButton = new Button();
                            this.Controls.Add(myButton);
                            myButton.Top = cleft * 25;
                            myButton.Left = 100;
                            myButton.Text = "TextBox " + this.cleft.ToString();
                            cleft++;
                            break;

                        case 2://LAbel
                            Label label1 = new Label();
                            this.Controls.Add(label1);
                            label1.Top = cleft * 25;
                            label1.Left = 100;
                            label1.Text = "label1 " + this.cleft.ToString();
                            cleft++;
                            break;
case 3://Button 
break;
                        default:
                    }

}


The code is for LAbel & Textbox.your expand it to any number of controls you want.You can take it for textbox,combobox,Groupbox,panel & so on .
Kindly take try, catch in your code to catch any error when it occurs.
Let me know whether this helped you or not.?
Happy Coding :-)
 
Share this answer
 
v3

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