在两个面板之间设置制表位

时间:2016-07-05 04:02:40

标签: vb.net

我有两个面板,每个面板都有几个文本框。我想要的是看似非常简单的东西:用户在此面板中输入文本框,然后按Tab键跳转到其他面板中的“链接”文本框。

但是,vb拒绝跳转到其他面板,除非它完成了一个面板内的所有文本框,无论它是什么TapStop。

我尝试从文本框中捕获Tab键,并将焦点发送到链接的键,但没有成功:按Tab键甚至不会触发KeyDown或KeyPress事件。

我首先尝试将TabStop设置为面板,但这也失败了。

所以,问题仍然存在..如何设置tabstop ..或任何类似的方法,到两个面板之间的文本框,以便当用户在一个面板中按Tab键时,它会切换到另一个面板?

我需要Tab键,而不是任何其他键。

1 个答案:

答案 0 :(得分:2)

您必须将表单上每个控件的False属性设置为Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox6.PreviewKeyDown, TextBox5.PreviewKeyDown, TextBox4.PreviewKeyDown, TextBox3.PreviewKeyDown, TextBox2.PreviewKeyDown, TextBox1.PreviewKeyDown If e.KeyCode = Keys.Tab Then Dim controls As Control() = {TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2} Dim currentControlIndex = Array.IndexOf(controls, ActiveControl) Dim nextControl = controls(currentControlIndex + 1) nextControl.Select() End If End Sub ,然后自行处理标签,您可以这样做:

Handles

您希望能够使用Tab键的每个控件都必须位于Select子句中,并且您希望能够Tab键到或来自的每个控件都必须位于数组中并且按您想要的顺序选项卡你应该再次重复数组末尾的第一个控件,以便从最后回到开头。

另请注意,默认情况下不会选择任何控件,如果它们都没有停止,在这种情况下,您必须手动Shown您希望在窗体中Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown 'Manually focus the first control after the form is displayed. Button1.Select() End Sub 'Include all the controls that you want to behave as Tab stops in the Handles clause. 'The order is unimportant but ordering them you will Tab to them is not a bad idea. Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown, TextBox1.PreviewKeyDown, TextBox4.PreviewKeyDown, TextBox2.PreviewKeyDown, TextBox5.PreviewKeyDown, TextBox3.PreviewKeyDown, TextBox6.PreviewKeyDown, Button2.PreviewKeyDown If e.KeyCode = Keys.Tab Then 'This array must contain all controls to behave as Tab stops in order and the first must be repeated at the end. Dim controls As Control() = {Button1, TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2, Button1} 'Find the currently active control in the array. Dim currentControlIndex = Array.IndexOf(controls, ActiveControl) 'Get the next control in the manual tab order. Dim nextControl = controls(currentControlIndex + 1) 'Focus that next control. nextControl.Select() End If End Sub 拥有焦点的控件{{1} 1}}事件处理程序。

编辑:这是一个更完整的例子:

@

该代码适用于以下形式,其中TextBox1,TextBox2和TextBox3在Panel1中,TextBox4,TextBox5和TextBox6在Panel2中:

Manual Tab Form

豫ICP备18024241号-1