Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically add controls to a form in VB.NET

Tags:

vb.net

I am working on an inventory in Visual Basic 2010 Express Edition. I don't know the number of fields that will be necessary for the inventory. My hope was that I could add textboxes/checkboxes/buttons using for loops in the program. Is there a way to add controls to a form without using the toolbox?

Can I add controls by instantiating them in the program?

like image 238
Mark Mikhaeil Avatar asked Jul 03 '12 13:07

Mark Mikhaeil


People also ask

How do you adding controls to forms in VB net explain?

Add the control by drawing Select the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.

How do you add a control onto a form?

Adding controls to the form is an easy task, just drag a control from the toolbox and drop it onto the form or draw it on the form. You can drag the control around the form and also resize it.

How many ways in which we can add controls on the form in VB?

There are many types of controls that we can add to the form. Among the controls, the most common ones are the button, label, textbox, list box, combo box, picture box, checkbox, and radio button. The controls' properties can be set to visible or invisible at runtime.


2 Answers

Yes.

Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim MyTextbox as New Textbox
    With MyTextbox
       .Size = New Size(100,20)
       .Location = New Point(20,20)
    End With
    AddHandler MyTextbox.TextChanged, AddressOf MyTextbox_Changed
    Me.Controls.Add(MyTextbox)

'Without a help environment for an intelli sense substitution
'the address name and the methods name
'cannot be wrote in exchange for each other.
'Until an equality operation is prior for an exchange i have to work
'on an as is base substituted.

End Sub

Friend Sub MyTextbox_Changed(sender as Object, e as EventArgs)
   'Write code here.
End Sub
like image 199
Holger Brandt Avatar answered Sep 18 '22 09:09

Holger Brandt


Dim numberOfButtons As Integer
Dim buttons() as Button

Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Redim buttons(numberOfbuttons)
    for counter as integer = 0 to numberOfbuttons
        With buttons(counter)
           .Size = (10, 10)
           .Visible = False
           .Location = (55, 33 + counter*13)
           .Text = "Button "+(counter+1).ToString ' or some name from an array you pass from main
           'any other property
        End With
        '
    next
End Sub

If you want to check which of the textboxes have information, or which radio button was clicked, you can iterate through a loop in an OK button.

If you want to be able to click individual array items and have them respond to events, add in the Form_load loop the following:

AddHandler buttons(counter).Clicked AddressOf All_Buttons_Clicked 

then create

Private Sub All_Buttons_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
     'some code here, can check to see which checkbox was changed, which button was clicked, by number or text
End Sub

when you call: objectYouCall.numberOfButtons = initial_value_from_main_program

response_yes_or_no_or_other = objectYouCall.ShowDialog()

For radio buttons, textboxes, same story, different ending.

like image 34
Thalia Avatar answered Sep 21 '22 09:09

Thalia