Reputation: 2015
Here i have dynamically created textboxes based on button click
So when i have my cursor in first textbox and when i key press enter button it should jump to next textboxes. How can I do that
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer
Dim textbox As New TextBox()
count = Panel1.Controls.OfType(Of TextBox)().ToList().Count
textbox.Location = New System.Drawing.Point(60, 25 * count)
textbox.Size = New System.Drawing.Size(80, 20)
textbox.Name = "textbox_" & (count + 1)
Panel1.Controls.Add(textbox)
End Sub
Upvotes: 1
Views: 1524
Reputation: 125197
You can handle KeyDown
event for your TextBox
controls and check if the is Enter, select next control:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 1 To 10
Dim txt = New TextBox()
txt.Name = String.Format("textbox_{0}", i)
AddHandler txt.KeyDown, AddressOf txt_KeyDown
Me.FlowLayoutPanel1.Controls.Add(txt)
Next
End Sub
Private Sub txt_KeyDown(sender As Object, e As KeyEventArgs)
If (e.KeyData = Keys.Enter) Then
e.Handled = True
SendKeys.Send("{Tab}")
End If
End Sub
Note:
It's better to use TableLayoutPanel
or FlowLayoutPanel
to add dynamic controls.
Add handler can be done also this way:
AddHandler txt.KeyDown, Sub(s, ea)
If (ea.KeyData = Keys.Enter) Then
ea.Handled = True
SendKeys.Send("{Tab}")
End If
End Sub
Upvotes: 1