简体   繁体   English

在运行时在VB.NET中添加一个控件(或控件)

[英]Adding a Control (or Controls) in VB.NET at runtime

This question is about VB.NET. 这个问题是关于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. 无论如何,我一直在创建一个简单的Windows地址簿表单应用程序。 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. 所以,我的应用程序还必须能够编辑这些地址中的每一个(顺便说一下,我的应用程序使用Access数据库,这真的很糟糕,但这是我的任务的一部分),我已经想过使用一个控制数组(只是就像在PHP中,但它显然从未在.NET中工作过)来编辑它们。 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. 我已经浏览了网上的每个论坛,但似乎无法找到足够的答案。

Thanks! 谢谢!

Vb.net, as part of the .NET framework have a control called FlowLayoutPanel that does what you want. 作为.NET框架的一部分,Vb.net有一个名为FlowLayoutPanel的控件,可以执行您想要的操作。 This control is used to mantain an order in the controls added to a form without you having to position each one manually. 此控件用于保存添加到表单的控件中的订单,而无需手动定位每个控件。

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. 但是,如果可以,最好的方法是使用连接到数据集的网格控件。

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 TextBox class. 例如, TextBox类。 You do that by declaring a variable of type TextBox and calling the constructor: 您可以通过声明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 Controls collection . 第三 ,您需要将该控件添加到窗体的Controls集合中 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 Panel or a GroupBox .) For example: (另请注意,您不限于将控件添加到Form 。您可以将其添加到任何容器式控件,例如PanelGroupBox 。)例如:

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. 但是,由于您正在创建一个与Access绑定的基于数据的应用程序,因此您应该考虑使用数据绑定控件 ,这会使您的生活更加简单。 They can automatically sync up their contents with the information saved in your database. 他们可以自动将其内容与数据库中保存的信息同步。

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)

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类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM