我有一个应用程序(.net framework 4,vb.net),它最初是一个VB6应用程序。 为了模仿旧选项卡控件行为的一部分,我正在实现加速键,允许您切换到另一个选项卡。 例 - 带有5个选项卡的TabControl。 - 标签2有一个带有文本框的标签和数据(alt-d加速器) - 用户选择了选项卡1,并点击alt-d,使选项卡控件选择选项卡2并将焦点设置为相应的文本框。
我编写了一些代码,用于查找包含控件的选项卡(我通过覆盖ProcessMnemonic来完成),只需查看选项卡(从选定的选项开始),如果找到匹配项,则选择选项卡然后允许系统通过调用“MyBase.ProcessMnemonic(charCode)”来处理助记符。
但我的问题是Control.IsMnemonic调用。由于您只传递控件的“文本”任何包含&的控件。在它的文本属性可以使它成为匹配。
例如,myTextbox.Text =“here& friend”会导致Alt-F将焦点设置在该框上。
我可以明确检查控件类型是否是标签......但是我还需要groupboxes和... whatelse?按钮我也应该允许助记符...
这是一些代码(注意我没有包含选项卡迭代,因为它似乎并不相关);
Private Function IsMnemonicInThisContainer(charCode As Char, controlContainer As System.Windows.Forms.Control.ControlCollection) As Boolean
For Each ctrl As Control In controlContainer
If Control.IsMnemonic(charCode, ctrl.Text) Then
If ControlIsAlive(ctrl) Then
Return True
End If
ElseIf ctrl.HasChildren Then
If ControlIsAlive(ctrl) AndAlso IsMnemonicInThisContainer(charCode, ctrl.Controls) Then
Return True
End If
End If
Next
Return False
End Function
Private Function ControlIsAlive(ctrl As Control) As Boolean
' In a TABPAGE that is not selected, the controls all appear to be visible = FALSE,
' because they aren't actually "visible" - HOWEVER... the control itself may be expecting
' to be visible (once it's tab is shown)... so this call to GetStateMethodInfo which I grabbed from
' http://stackoverflow.com/questions/3351371/using-control-visible-returns-false-if-its-on-a-tab-page-that-is-not-selected
' is the solution I needed.
' Instead of walking the tree though I am going to "check containers" as I drop into them... if they are not enabled/visible
' then I'm not going to go any deeper
' Is control enabled and going to be shown? (calling ctrl.visible allows us to bypass the other call if we can!)
Return (ctrl.Enabled AndAlso (ctrl.Visible OrElse CBool(GetStateMethodInfo.Invoke(ctrl, New Object() {2}))))
End Function
我想我可以做点像......
如果Typeof ctrl是Label orelse Typeof ctrl是groupbox(etc ...)...
但确定这一点的属性(或方法)会很棒。有什么想法吗?
谢谢! 克里斯伍德拉夫