1

もともとVB6アプリケーションだったアプリケーション(.net Framework 4、vb.net)があります。古いタブ コントロールの動作の一部を模倣するために、別のタブに切り替えることができるアクセラレータ キーを実装しています。例 - 5 つのタブを持つ TabControl。- タブ 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 orelse の場合 Typeof ctrl が groupbox (etc...)...

しかし、これを決定するためのプロパティ (またはメソッド) は素晴らしいでしょう。何か案は?

ありがとう!クリス・ウッドラフ

4

0 に答える 0