按回车移动到指定控件上,如何实现呢

来源:百度知道 编辑:UC知道 时间:2024/06/05 13:27:59
asp.net 在webform页上有四个控件TextBox1,TextBox2,TextBox3,Button
我想按回车把TextBox1,设为焦点,在按回车把TextBox2,设为焦点,在按回车把Button 设为焦点,在按回车执行Button(onclick),中间跳过TextBox3,
和手机大厅交费功能差不多,如何实现?
KeyPress这个事件在哪设置

Private Sub Form_KeyPress (KeyAscii As Integer)
Dim NextTabIndex As Integer, i As Integer
If KeyAscii = 13 Then
If Screen.ActiveControl.TabIndex = _
Count - 1 Then
NextTabIndex = 0
Else
NextTabIndex = Screen.ActiveControl._
TabIndex + 1
End If
For i = 0 To Count - 1
If Me.Controls(i).TabIndex = _
NextTabIndex Then
Me.Controls(i).SetFocus
Exit For
End If
Next i
KeyAscii = 0
End If
End Sub

1.在webform的keyPress事件中加代码
if(e.KeyChar = (Char)Keys.Enter)//如果按键是Enter
{TextBox1.Focus();}//则TextBox1获得焦点
2.同理在TextBox1的KeyPress事件中加类似代码 TextBox2.Focus()
3.TextBox2........Button1.Foucs();
4.Button1.............调用Button1_Click的方法的代码

其实这样做不太符合规范,一般应该这样,把TextBox1的TabIndex顺序置为0,TextBox2置为1,整个窗体的AcceptButton属性(表示当按下Enter键时相当于单击该按钮)设置为你的Button

1.把画面的KeyPreview 属性设成True
2.在画面的KeyDown事件中写:
If (e.KeyCode() = Keys.Enter)