2016-06-17 108 views
1

這裏我有動態創建基於鍵文本框中單擊如何跳到光標移到下一個文本框上輸入按鍵

所以當我有我的第一個文本框光標,當我按鍵輸入按鈕,它應該跳轉到下一個文本框。我怎樣才能做到這一點

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 

enter image description here

回答

1

您可以處理KeyDown事件爲您TextBox控制和檢查,如果是輸入,選擇下一控制:

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 

注:

  • 最好使用TableLayoutPanelFlowLayoutPanel添加動態控件。

  • 添加處理程序也可以做到這樣:

    AddHandler txt.KeyDown, Sub(s, ea) 
              If (ea.KeyData = Keys.Enter) Then 
               ea.Handled = True 
               SendKeys.Send("{Tab}") 
              End If 
             End Sub 
    
相關問題