在运行时在 VB.NET 中添加一个控件(或多个控件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5487718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a Control (or Controls) in VB.NET at runtime
提问by Franz Steven Labawan
This question is about VB.NET. I'm quite a novice on this one, so please forgive me if you feel that this question is nothing short of crazy or whatever. Anyway, I've been a creating a simple Windows addressbook Form application. We all know for a fact that a single person can have one or more addresses, of which a one-to-many relationship holds true. So there, my application also has to be able to edit each of these addresses (by the way, my application uses an Access Database, which really sucks but it's part of my task), and I already thought of using a control array (just like in PHP but it obviously never worked in .NET) for me to edit them. How am I supposed to implement this? I've scoured every forum on the web possible but couldn't seem to find an answer to suffice.
这个问题是关于 VB.NET 的。我是这个问题的新手,所以如果你觉得这个问题简直是疯狂或其他什么,请原谅我。不管怎样,我一直在创建一个简单的 Windows 地址簿表单应用程序。我们都知道一个事实,一个人可以拥有一个或多个地址,其中一对多的关系成立。因此,我的应用程序还必须能够编辑这些地址中的每一个(顺便说一下,我的应用程序使用 Access 数据库,这确实很糟糕,但它是我的任务的一部分),而且我已经考虑使用控件数组(只是就像在 PHP 中一样,但它显然从未在 .NET 中工作过)让我编辑它们。我应该如何实现这一点?我已经搜索了网络上的每个论坛,但似乎找不到足够的答案。
Thanks!
谢谢!
回答by Doliveras
Vb.net, as part of the .NET framework have a control called FlowLayoutPanel that does what you want. This control is used to mantain an order in the controls added to a form without you having to position each one manually.
Vb.net 作为 .NET 框架的一部分,有一个名为 FlowLayoutPanel 的控件,可以执行您想要的操作。此控件用于维护添加到表单的控件中的顺序,而无需手动定位每个控件。
The only thing you have to do is create a new instance of the control you need in the form, let's call it EditControl, and add the new instance of the control to the FlowLayoutPanel control.
您唯一需要做的就是在表单中创建您需要的控件的新实例,我们称之为 EditControl,并将该控件的新实例添加到 FlowLayoutPanel 控件中。
Something like this:
像这样的东西:
dim tmpC as new EditControl()
containerControl.Controls.Add(tmpC)
with this the FlowLayoutPanel control will show as many EditControls as you add on the form.
有了这个 FlowLayoutPanel 控件将显示与您在表单上添加的一样多的 EditControl。
Assuming containerControl is declared as a FlowLayoutPanel.
假设 containerControl 被声明为 FlowLayoutPanel。
But if you can, the best way of doing this is using a grid control connected to a Dataset.
但如果可以,最好的方法是使用连接到数据集的网格控件。
回答by Cody Gray
I'm not sure I understand the question. What exactly are you trying to do? What do you have already? What have you already tried that isn't working?
我不确定我是否理解这个问题。你到底想做什么?你已经有什么了?你已经尝试过哪些不起作用?
Judging only by the title, you want to add another control to a form at runtime. That's simple enough.
仅从标题来看,您希望在运行时向窗体添加另一个控件。这很简单。
First, you need to create an instance of a control class. For example, the TextBoxclass. You do that by declaring a variable of type TextBoxand calling the constructor:
首先,您需要创建一个控件类的实例。以TextBox班级为例。你可以通过声明一个类型的变量TextBox并调用构造函数来做到这一点:
Dim txt As New TextBox()
Second, you might want to set some properties of that textbox you just created. These are the same properties you can set in Design View using the Properties Window. For example:
其次,您可能想要设置您刚刚创建的那个文本框的一些属性。这些属性与您可以使用“属性”窗口在“设计视图”中设置的属性相同。例如:
txt.Text = "Default text"
Third, you need to add that control to your form's Controlscollection. This is what makes the control actually display on the form. (Also note that you're not limited to adding the control to a Form. You can add it to any container-style control, such as a Panelor a GroupBox.) For example:
第三,您需要将该控件添加到表单的Controls集合中。这就是使控件实际显示在窗体上的原因。(另请注意,您不仅可以将控件添加到 a Form。您可以将其添加到任何容器样式的控件,例如 aPanel或 a GroupBox。)例如:
myForm.Controls.Add(txt)
However, since you're creating a data-based application tied with Access, you really ought to look into using data-bound controls, which make your life much simpler. They can automatically sync up their contents with the information saved in your database.
但是,由于您正在创建一个与 Access 相关联的基于数据的应用程序,您确实应该考虑使用数据绑定控件,这会让您的生活变得更加简单。他们可以自动将其内容与保存在您数据库中的信息同步。
回答by squillman
You don't need a control array or even to add them programatically, you just need to bind your datasource properties / fields / columns / whatever do the controls on your form and have a mechanism for scrolling or paging through the address records. How you do this will very much depend on what kind of UI you wish to present to the user.
您不需要控件数组,甚至不需要以编程方式添加它们,您只需要绑定您的数据源属性/字段/列/表单上的控件并具有滚动或分页地址记录的机制。您如何做到这一点在很大程度上取决于您希望呈现给用户的 UI 类型。
回答by IntellectualChestnut
Take a look at this.
http://shashwats-softwares.com/2016/01/29/creating-controls-at-runtime-in-vb-net/
看看这个。
http://shashwats-softwares.com/2016/01/29/creating-controls-at-runtime-in-vb-net/
dim btn as new system.windows.forms.buttons
btn.text="Click Me"
Me.controls.add(btn)

