Link to home
Start Free TrialLog in
Avatar of higginsonline
higginsonline

asked on

Programmatically Add Text Boxes to a WinForm in Visual Basic 2005

How could I programmatically add multiple text boxes to a WinForm, in Visual Basic 2005

Basically I want to read a table in a database and depending on how many rows there are I would like to add that many text boxes.  I am having a difficult time figuring out how to go about creating a textbox using just code.

Thanks in Advance

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Do you need any of the events with the text box controls?
Avatar of higginsonline
higginsonline

ASKER

I will need to be able to change the fonts/names etc. So that I can edit/delete the items in the database as well.
Hi higginsonline;

This will basically create the TextBox on the form

        Dim rowCount As Integer = 10
        Dim X As Integer = 10   ' The X location of the TextBox on the form
        Dim Y As Integer = 10   ' The Y location of the TextBox on the form

        For idx As Integer = 0 To rowCount
            Dim tb As New TextBox
            tb.Name = "tb" & idx.ToString()
            tb.Location = New Point(X, Y)
            Y += 25
            Me.Controls.Add(tb)
        Next

Fernando
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You rock...Thanks!
One more thing, is their a way to place this inside my panel instead of behind it?
Yes you can add it to any container by changing this statement

    Me.Controls.Add(tb)

to

    TheContainerYouWant.Controls.Add(tb)

For example to add it to a panel control called Panel1 the statement will become

    Panel1.Controls.Add(tb)
You my friend, are the man!
Glad I was able to help. ;=)
Grr, sorry I keep working on this program and seeing more and more lol.

Can I make a button with a click event? basically I want them to be able to click the button to select the row...

Thanks!
To your statement, "Can I make a button with a click event? basically I want them to be able to click the button to select the row..."

Not sure what you mean, can you elaborate on what you need a little more.
I basically just need a button with a click event that is created with each row to hold the ID of that row, so when I click it, I can do a select * from table where ID = buttonID

OK; a button is created basically the same way as the text box was created just once for each row. So when you create a button you can do it like this.

        Private row As Integer = 1

        Dim btn As New Button()
        btn.Text = "Row " & row.ToString()
        btn.Name = "Row" & row.ToString()
        btn.Location = New Point(X, Y) ' Need different X and Y from TextBox location
        ' I am using the Tag property to identify the row clicked on
        btn.Tag = row
        ' Because you want to be able to click on the button and have an event fired
        ' you need to have written a subroutine to handle that in advance. This wires
        ' the Click event on the button to that subroutine
        AddHandler btn.Click, AddressOf Row_Click
        row += 1
        Panel1.Controls.Add(btn)
        ' No need to add it to the hashtable

    ' Event for dynamically created button.
    Private Sub Row_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs)

        Dim btn As Button = CType(sender, Button)
        ' The row that was clicked on
        Dim rowSelected As Integer = CInt(btn.Tag)

    End Sub

If you never remove the buttons nothing else is needed. If you do need to remove the button before the program is terminated then you will need to remove the event handler for that button with the following statement.

    RemoveHandler btn.Click, AddressOf Row_Click

where btn is the reference to the button being removed.
Once again you are the man... the man of mans the man of all mans.....

One last question though I swear...

Is there a way to clear the contents of a panel?
The panel I create all this in would love to clear it when I click on another button.

Thats it... last one.... I swear

Thanks!
Well how can I say no.

The following code will remove all the controls on the panel so the button you click on to remove all the controls should not be located there.

    Private Sub btnClearContents_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnClearContents.Click

        ' Remove the event handlers from the button controls on the panel
        For Each ctl As Control In Panel1.Controls
            ' Find out if the control is a button
            If ctl.GetType.Name = "Button" Then
                ' The control was a button remove its click event handler
                RemoveHandler ctl.Click, AddressOf Row_Click
            End If
        Next
        ' Remove all controls from the panel
        Panel1.Controls.Clear()
        ' Remove all the TextBox controls in the hashtable
        htTextBox.Clear()
        ' All controls on the panel have gone to garbage collection

    End Sub
T
TH
THA
THAN
THANK
THANK Y
THANK YO
THANK YOU

I am now closing my browser so I will not be tempted to ask more stupid annoying questions.

Really appreciate your help.
I am of the opinion that there are no stupid questions,  Except, possibly, a question not asked. But here at EE they like to keep it to one question per thread. LOL ;=)
And as always, glad I was of some help. ;=)
Woops, I asked like 8 per this thread my bad.