定義一個 全局變量intNum,用來儲存控件的tabindex
然后你你要輸入的文本控件的click事件里面加入如下語句
intNum = Me.ActiveControl.TabIndex
比如 :
Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
intNum = Me.ActiveControl.TabIndex
End Sub
每個文本控件的 click事件都必須有此語句
然后在button按鈕的click事件里面書寫控制代碼
比如:
Select Case intNum
Case 0
Me.TextBox1.Text = "焦點在此位置 "
Me.TextBox1.SelectionStart = Me.TextBox1.TextLength
Me.TextBox1.SelectionLength = 0
Me.TextBox1.Focus()
Case 1
Me.TextBox2.Text = "焦點在此位置 "
Me.TextBox2.SelectionStart = Me.TextBox2.TextLength
Me.TextBox2.SelectionLength = 0
Me.TextBox2.Focus()
Case 2
Me.TextBox3.Text = "焦點在此位置 "
Me.TextBox3.SelectionStart = Me.TextBox3.TextLength
Me.TextBox3.SelectionLength = 0
Me.TextBox3.Focus()
Case 3
Me.TextBox4.Text = "焦點在此位置 "
Me.TextBox4.SelectionStart = Me.TextBox4.TextLength
Me.TextBox4.SelectionLength = 0'使光標位置在文本最后
Me.TextBox4.Focus()'因為點擊按鈕會使焦點轉移,必須把焦點重新賦予當然文本框!
End Select