在 C# 中动态创建控件的方法
在 C# 中可以通过哪些方式动态创建控件?
起初这是对象,但更准确地说是控制。我的术语被搞乱了。谢谢乔尔。
编辑{ 在运行时创建的控件。并且能够被程序访问和编辑。 这有帮助吗? 我
喜欢动态创建的想法,并且想知道有什么方法可以做到这一点。
每个答案请只提供一个,我想看看人们如何对它们进行排名。
例如,
private Label _lblCLastName = new Label();
private static List<ChildrenPanel> _ListCP = new List<ChildrenPanel>();
public void CreatePanel(Panel Container)
{
// Created Controls
#region Controls
_pnlStudent.Controls.Add(_lblCLastName);
//
// lblCLastName
//
_lblCLastName.AutoSize = true;
_lblCLastName.Location = new System.Drawing.Point(6, 32);
_lblCLastName.Name = "lblCLastName";
_lblCLastName.Size = new System.Drawing.Size(58, 13);
_lblCLastName.TabIndex = 10;
_lblCLastName.Text = "Last Name";
// Adds controls to selected forms panel
Container.Controls.Add(_pnlStudent);
// Creates a list of created panels inside the class
// So I can access user input
ListCP.Add(this);
}
这是与我正在谈论的内容接近的代码片段。我又发了一篇文章,但没有完全正确地发布问题。我将删除它,但 atm 仍然可以查看。
如果仍然存在问题,请保持建设性,我不介意负面的意见,只要它有帮助。
编辑: 我能够得到一些我一直在寻找的答案。感谢所有回复的人。当我有能力时我也会关闭它。如果其他人可以关闭它,我们将不胜感激。
What ways can you dynamically create controls in C#?
This was objects at first but it would have been more precise to say controls. My terminology was messed up. Thanks Joel.
Edit{
Controls that are created during runtime. And are able to be accessed and edited by the program.
Does this help?
}
I like the idea of Dynamic creation and was wondering what ways there were to do this.
Please only one per answer, I would like to see how people rank them.
eg
private Label _lblCLastName = new Label();
private static List<ChildrenPanel> _ListCP = new List<ChildrenPanel>();
public void CreatePanel(Panel Container)
{
// Created Controls
#region Controls
_pnlStudent.Controls.Add(_lblCLastName);
//
// lblCLastName
//
_lblCLastName.AutoSize = true;
_lblCLastName.Location = new System.Drawing.Point(6, 32);
_lblCLastName.Name = "lblCLastName";
_lblCLastName.Size = new System.Drawing.Size(58, 13);
_lblCLastName.TabIndex = 10;
_lblCLastName.Text = "Last Name";
// Adds controls to selected forms panel
Container.Controls.Add(_pnlStudent);
// Creates a list of created panels inside the class
// So I can access user input
ListCP.Add(this);
}
This is a code snippet from something that is close to what I'm talking about. I made another post but didn't quite post the question right. I will be deleting it but atm it is still viewable.
If there are still problems please be constructive I don't mind negitive input as long as it's helpful.
Edit:
I was able to get some answers I was looking for. Thank you to everyone who replied. I will close this when I am able too. If someone else can close it that would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我使用 new 关键字动态创建对象。
I use the new keyword to dynamically create objects.
动态创建 GUI 对象非常有用,但对于维护来说也可能是一场噩梦。
一个好的经验法则是限制动态创建的 GUI 对象的数量。
您可能实际上想要使用动态创建的 GUI 对象的一种情况是当您不知道所需对象的数量或数量时。例如,结果集中的每一行都有一个标签(即使如此,您也可以考虑使用 DataGrid 或 GridView 类型对象)。
这适用于 WinForms 和 ASP.NET。只要确保正确记录您的代码即可。
我的建议是坚持使用视觉设计器来获得更简单的表单,并且仅在绝对必要时动态创建和添加对象。
(FWIW,您发布的代码片段可能会被简化和/或重构,因为它似乎朝着错误的方向发展。)
Creating GUI objects dynamically can be extremely useful, however, it can also be a nightmare for maintenance.
A good rule of thumb is to limit the amount of GUI object you dynamically create.
One situation where you may actually want to use a dynamically created GUI object is when you don't know the amount or count of objects you need. For example, one label for each row in a result set (even then you may consider a DataGrid or GridView type object).
This works for both WinForms and ASP.NET. Just be sure to document your code correctly.
My advice would be to stick with the Visual Designer for simpler forms and only create and add objects dynamically when it's absolutely necessary.
(FWIW, the code snippet you posted could probably be simplified and/or refactored as it seems to be going in the wrong direction.)
匿名类型,C# 3.x
这是相当动态的,因为您不必编写类模板来获取自定义对象。
编译器将从您提供的初始化值推断属性的类型。
该类型无法从源代码中访问,但可以在 IL 中看到。但是,如果您创建多个具有相同属性的匿名对象,C# 编译器将为所有这些对象使用相同的类型。
规定还有更多,但这些都很重要。
Anonymous Types, C# 3.x
This is fairly dynamic-esque because you don't have to code a class template to get custom objects.
The compiler will infer the Types of the properties from the initialization values you provide.
The type is not accessible from your source code, but can be seen in the IL. However if you create multiple anonymous objects with the same properties the C# compiler will use the same type for all of them.
Stipulations There are more, but these are important.
激活(也适用于远程对象)
使用 System.Activator 类'重载 Activator.CreateInstance< /a> 方法。这涉及到本地或远程创建对象的领域。
(有关远程对象的 MSDN 文档。)
Activation (for remote objects too)
Use the System.Activator class' overloaded Activator.CreateInstance methods. This gets into the realm of creating objects locally or remotely.
(MSDN Documentation about Remote Objects.)
假设您正在谈论动态对象的创建:
您显然需要一个库来支持这一点,除非您想自己进入
Reflection.Emit
- LinFu 在版本 1 中支持这一点:http://code.google.com/p/linfu/
但是,该功能已被删除我好像记得版本2之前的。
Assuming you are talking about the creation of dynamic objects:
You'll obviously need a library to support this, unless you want to get into
Reflection.Emit
yourself - LinFu supported this in version 1:http://code.google.com/p/linfu/
However, it's a feature that was dropped before version 2 I seem to remember.