简体   繁体   中英

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

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.

Thanks!

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.

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.

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.

Assuming containerControl is declared as a 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. You do that by declaring a variable of type TextBox and calling the constructor:

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 . 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:

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.

Take a look at this. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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