Events for Dynamically Added Textbox

R

Randy

I'm don't even know where to start on this one. I have a form where
the user is allowed to add one row at a time to a table via
textboxes. Imagine an order with an undefined number of items to be
selected at will by the user. The form has an empty textbox built
onto it where the user can select the first item. Once they do so (in
the textbox_validating event), a button appears giving them the option
to add another item to the order. If the user clicks the button, the
button_click event creates a new textbox and places it just below the
first textbox. This new textbox is where they will enter the data for
item #2.

What I need to do is to add some programming to the newly-created
textbox so that it is ready for a potential third item, and so on
until the user is done entering items. Is there a way to add code to
the form as the user is interacting with the form?

Thanks,
Randy
 
R

Randy

One question on this code. How can I modify it so that it works on
the validating event of a textbox rather than the click event of a
button?

Dim newTB As New TextBox
With newTB
.Name = "tb" + intItemCnt + 1
.Size = New System.Drawing.Size(320, 20)
.Location = New Point(205, 147 + intItemCnt * 20)
End With
AddHandler newTB.Validating, AddressOf ValNewTB
Controls.Add(newTB)
intItemCnt += 1
End Sub

Private Sub ValNewTB(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Clicked was " & DirectCast(sender,
Button).Tag.ToString)
End Sub

Thanks,
Randy
 
R

rowe_newsgroups

One question on this code. How can I modify it so that it works on
the validating event of a textbox rather than the click event of a
button?

Dim newTB As New TextBox
With newTB
.Name = "tb" + intItemCnt + 1
.Size = New System.Drawing.Size(320, 20)
.Location = New Point(205, 147 + intItemCnt * 20)
End With
AddHandler newTB.Validating, AddressOf ValNewTB
Controls.Add(newTB)
intItemCnt += 1
End Sub

Private Sub ValNewTB(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Clicked was " & DirectCast(sender,
Button).Tag.ToString)
End Sub

Thanks,
Randy

' Typed in message
Private Sub Textboxes_Validating(...) Handles TextBox1.Validating
If MessageBox.Show("Do you want to add another textbox?", "Add
new Item?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes
Then
Dim tb as New TextBox
tb.Name = String.Format("tb{0}", intItemCount + 1)
tb.Size = New Size(320, 20)
tb.Location = New Point(205, 145 + intItemCount * 20)
AddHandler tb.Validating, AddressOf Textboxes_Validating
Controls.Add(tb)
intItemCnt += 1
End If
End Sub

Thanks,

Seth Rowe
 
R

Randy

Sorry to be slow, but this is the first time that I have used event
handlers. The error message that I can't resolve is:

Method 'Private Sub ValNewTB(sender As Object, e As System.EventArgs)'
does not have the same signature as delegate 'Delegate Sub
CancelEventHandler(sender As Object, e As
System.ComponentModel.CancelEventArgs)'.

Can anybody help me resolve this?

Thanks.
 
L

Lloyd Sheen

Randy said:
Sorry to be slow, but this is the first time that I have used event
handlers. The error message that I can't resolve is:

Method 'Private Sub ValNewTB(sender As Object, e As System.EventArgs)'
does not have the same signature as delegate 'Delegate Sub
CancelEventHandler(sender As Object, e As
System.ComponentModel.CancelEventArgs)'.

Can anybody help me resolve this?

Thanks.

change System.EventArgs to System.ComponentModel.CancelEventArgs as
indicated in the message. The name of the routine is not inportant only
that the routine has the same parameters (signature) as required.

Lloyd Sheen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top