确定助记符/加速键键值是否适用于控件的方法?

发布于 2024-12-15 05:22:47 字数 2296 浏览 2 评论 0原文

我有一个应用程序(.net Framework 4,vb.net),最初是一个 VB6 应用程序。 为了模仿旧选项卡控件行为的部分内容,我正在实现允许您切换到另一个选项卡的加速键。 例子 - TabControl 有 5 个选项卡。 - 选项卡 2 有一个带有文本框的标签 &Data(alt-d 加速器) - 用户选择了选项卡 1,然后按 alt-d,这会导致选项卡控件选择选项卡 2 并将焦点设置到相应的文本框。

我编写了一些代码来查找包含控件的选项卡(我通过重写 ProcessMnemonic 来做到这一点),然后简单地浏览选项卡(从选定的选项卡开始),如果找到匹配项,我选择该选项卡,然后允许系统通过调用“MyBase.ProcessMnemonic(charCode)”来处理助记词。

但我的问题是 Control.IsMnemonic 调用。由于您只传递控件的“文本”,因此任何包含 & 的控件都将被传递。它的文本属性可以导致它匹配。

例如,myTextbox.Text =“here &friend”将导致 Alt-F 将焦点设置在该框上。

我可以明确检查控件类型是否是标签...但我还需要组框和...还有什么?按钮我还应该允许助记符...

这是一些代码(注意我没有包含选项卡迭代,因为它似乎不相关);

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 或者 Typeof ctrl 是 groupbox (等等...)...

但是确定这一点的属性(或方法)会很棒。有什么想法吗?

谢谢! 克里斯·伍德拉夫

I have an application (.net framework 4, vb.net) that originally was a VB6 application.
To mimic portions of the old tab control behavior, I am implementing accelerator keys that allow you to switch to another tab.
Example
- TabControl with 5 tabs.
- Tab 2 has a label &Data (alt-d accelerator) with a textbox
- User has Tab 1 selected, and hits alt-d which causes the tab control to select tab 2 and set focus to the corresponding textbox.

I have written some code which looks for the tab which contains the control (I do this by overriding ProcessMnemonic) and simply look through the tabs (starting with the selected one) and if I find a match, I select the tab then allow the system to process the mnemonic by calling "MyBase.ProcessMnemonic(charCode)".

But my issue is the Control.IsMnemonic call. Since you only pass the control's "Text" any control which contains an & in it's text property can cause it to be a match.

For instance, myTextbox.Text = "here &friend" would cause Alt-F to set focus on that box.

I can explicitly check if the control type is a label... but then I also need groupboxes and... whatelse? Buttons I should also allow the mnemonic...

Here's some code (note I didn't include the tab iteration as it didn't seem pertinent);

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

I suppose I could do something like...

If Typeof ctrl is Label orelse Typeof ctrl is groupbox (etc...)...

But a property (or method) to determine this would be great. Any ideas?

Thanks!
Chris Woodruff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文