【发布时间】:2015-08-30 10:31:52
【问题描述】:
我有一个通用消息框,我们有几个面板,我们在运行时在其中添加控件。
在我的表单中,我已经在 pnlBottom 上添加了 pnlButtons 以及其他一些控件。
现在在运行时,我正在向pnlButtons 添加一个OK 按钮,该按钮位于pnlBottom 上。我没有为Designer.vb 文件中的任何控件设置TabIndex。
我正在尝试使用下面的代码将注意力集中在OK 按钮上,但它不起作用。
For Each control As Control In Me.Controls
If TypeOf (control) Is Panel Then
Dim pnlBottons As Panel = CType(control, Panel)
If pnlBottons.Name = "pnlBottom" Then
For Each ctrl As Control In control.Controls
Dim pnlButtons As Panel = CType(ctrl, Panel)
If pnlButtons.Name = "pnlButtons" Then
For Each ctrlbtn As Control In ctrl.Controls
If TypeOf (ctrlbtn) Is Button Then
Dim textBox As Button = CType(ctrlbtn, Button)
textBox.Parent.Parent.TabIndex = 0
textBox.Parent.TabIndex = 0
textBox.TabIndex = 0
End If
Next
End If
Next
End If
End If
Next
在这里,我将 pnlBottom、pnlButtons 和 OK 按钮的 TabIndex 设置为 0。
请建议如何聚焦OK 按钮。
【问题讨论】:
-
If textBox.Text = "OK" Then textBox.Focus()? (没有那些 Parent 和 TabIndex) -
这不是 TabIndex 应该如何工作的。 TabIndex 确定通过 tab 键访问控件的顺序(从最低到最高索引,没有特定的第一个/最后一个索引)。要在运行时将焦点设置在特定控件上,您可以依赖 Focus() 方法,如上一条评论中所建议的那样。
-
我为 StackOverflow 制作了一个小工具,可以轻松将代码缩进增加或减少 4 个空格。 Here's the link 使用它。
-
textBox.Focus() 没有在运行时将焦点设置在我的按钮控件上。所以我认为我可以使用标签索引 o(zero) 来显示 OK 按钮上的焦点,但它不起作用。
-
哪个控件有焦点?