如何在C#中创建添加控件的功能 [英] How can I make a function to Add Control in C#
问题描述
如何使一个函数有一个参数接收控件的类型如(Button,GroupBox,Panel,TextBox,Label ...等)
private void CreateControl()
{
Button myButton = new Button();
this.Controls.Add(myButton);
}
我需要设置一个参数来控制控件的类型并不总是一个按钮。
请帮助
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
推荐答案
嗯,你可以这样做:
Well, you could do this:
private void CreateControl(Type controlType)
{
Control control = Activator.CreateInstance(controlType) as Control;
if (control != null) Controls.Add(control);
}
这有点复杂,因为它在很大程度上取决于您要传递的参数类型:您需要一个参数告诉你要创建什么类型的控件,这意味着传递一个字符串作为类名,或者一个Type指示控件应该是什么类型 - 这将使你的外部代码调用它复杂 - 但是我有多复杂无法分辨。
一般来说,这种方法并不是一个好主意 - 这是可能的,但这并不意味着你应该这样做 - 这通常意味着你的设计过于复杂或经过深思熟虑。但也有例外。
一种方法是通过泛型:
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:
private void CreateControl<T>() where T : Control, new()
{
T t = new T();
Controls.Add(t);
}
然后你创建这样的实例:
You then create the instance like this:
CreateControl<Button>();
否则,它的参数类型和反射使用 Activator.CreateInstance [ ^ ]
您好,
以下是示例代码。只需采取组合框/下拉和放大器列出要动态创建的控件&分配一些值。
点击按钮执行代码。
例如文本框为值1,标记为值2
和调用函数CreateControl()&申请swith case。
这是小代码;
取一个整数作为全局变量
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;
变量用于定位目的。
然后修改你的函数CreateControl()
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:
}
}
该代码适用于LAbel& Textbox.your将它扩展为您想要的任意数量的控件。您可以将它用于textbox,combobox,Groupbox,panel&等等。
请小心试试,抓住你的代码来发现任何错误。
让我知道这是否对你有所帮助。?
快乐编码: - )
}
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 :-)
这篇关于如何在C#中创建添加控件的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!