2

我正在尝试将三个组合框合并到一个由文本框、numericupdown 控件和现在的组合框组成的功能 GUI 中。
在这些表单上,我可以使用键盘(me.selectnextcontrol)进行导航,按键向上和向下。
发生什么了?
当我第一次进入这些 GUI 时,一切正常,我可以按预期使用键盘向上/向下移动。

当我编辑一个位于我的 gui 中间并且设置如下的组合框时,问题就出现了:

    mycombo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    mycombo.AutoCompleteSource = AutoCompleteSource.CustomSource
    mycombo.AutoCompleteCustomSource = myAutoCompleteStringCollection

问题是当我回到那些组合框时,我的导航循环不再获得按键(向上或向下键),因为组合框将它们用于自身目的(更改索引)。

我在 combobox_Leave 之后尝试,mycombo.AutoCompleteSource = AutoCompleteSource.CustomSource但这会关闭不需要的自动完成功能。

问题是:
这里是否可以在模式使用后设置描述的组合框,就像它在程序开始时一样,当它没有被编辑时,所以我可以在初始方式通过这些组合框使用键盘导航,但如果我需要,自动建议选项保持启用再次编辑它。

已编辑:这是一个显示问题的简单示例:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim tb As New TextBox
    Dim cbb As New ComboBox
    Dim tbb As New TextBox
    Dim b1 As New Button
    Dim b2 As New Button

    With Me
        .KeyPreview = True
        .Size = New Size(350, 200)
        With .Controls
            .Add(tb)
            With tb
                .TabIndex = 0
                .Location = New Point(95, 20)
                .Text = "This is"
            End With
            .Add(cbb)
            With cbb
                .TabIndex = 1
                .Items.AddRange(New String() {"alabama", "africa", "australia", "grenland"})
                .Location = New Point(95, 50)
                .Text = "an Example"
                .DropDownStyle = ComboBoxStyle.DropDown
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.ListItems
            End With
            .Add(tbb)
            With tbb
                .TabIndex = 2
                .Location = New Point(95, 80)
                .Text = "textbox"
            End With

            .Add(b1)
            With b1
                .TabStop = False
                .Location = New Point(90, 130)
                .Text = "Nothing"
            End With
            .Add(b2)
            With b2
                .TabStop = False
                .Location = New Point(170, 130)
                .Text = "Exit"
                AddHandler b2.Click, AddressOf b2_Click
            End With
        End With
    End With
End Sub

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Up Then
        e.Handled = True
        Me.SelectNextControl(Me.ActiveControl, False, True, True, True)
    End If

    If e.KeyCode = Keys.Down Then
        e.Handled = True
        Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
    End If
End Sub

Private Sub b2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Me.Close()
End Sub
End Class

当您启动此程序时,请使用向下箭头键多次通过所有控件。然后在组合框上停止并键入字母“a”,然后尝试使用向下箭头再次导航。

4

1 回答 1

1
Public Class Form1

    Private tb As New TextBox
    Private cbb As New ComboBox
    Private tbb As New TextBox
    Private b1 As New Button
    Private b2 As New Button

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        AddHandler cbb.PreviewKeyDown, AddressOf cbb_PreviewKeyDown
        AddHandler tb.PreviewKeyDown, AddressOf tb_PreviewKeyDown
        AddHandler tbb.PreviewKeyDown, AddressOf tbb_PreviewKeyDown

        Me.Size = New Size(350, 200)

        With tb
            .Parent = Me
            .TabIndex = 0
            .Location = New Point(95, 20)
            .Text = "This is"
        End With

        With cbb
            .Parent = Me
            .TabIndex = 1
            .Items.AddRange(New String() {"alabama", "africa", "australia", "grenland"})
            .Location = New Point(95, 50)
            .Text = "an Example"
            .DropDownStyle = ComboBoxStyle.DropDown
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            .AutoCompleteSource = AutoCompleteSource.ListItems
        End With

        With tbb
            .Parent = Me
            .TabIndex = 2
            .Location = New Point(95, 80)
            .Text = "textbox"
        End With

        With b1
            .Parent = Me
            .TabStop = False
            .Location = New Point(90, 130)
            .Text = "Nothing"
        End With

        With b2
            .Parent = Me
            .TabStop = False
            .Location = New Point(170, 130)
            .Text = "Exit"
            AddHandler b2.Click, AddressOf b2_Click
        End With

    End Sub

    Private Sub tb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
        If e.KeyCode = Keys.Up Then tbb.Focus()
        If e.KeyCode = Keys.Down Then cbb.Focus()
    End Sub

    Private Sub cbb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
        If e.KeyCode = Keys.Up Then
            cbb.AutoCompleteMode = AutoCompleteMode.None
            tb.Focus()
            cbb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
            If Not cbb.Items.Contains(cbb.Text) Then cbb.Text = ""
        End If
        If e.KeyCode = Keys.Down Then
            cbb.AutoCompleteMode = AutoCompleteMode.None
            tbb.Focus()
            cbb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
            If Not cbb.Items.Contains(cbb.Text) Then cbb.Text = ""
        End If
    End Sub

    Private Sub tbb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
        If e.KeyCode = Keys.Up Then cbb.Focus()
        If e.KeyCode = Keys.Down Then tb.Focus()
    End Sub

    Private Sub b2_Click()
        Me.Close()
    End Sub
End Class

我修改了你的代码。现在,它正在工作。:)

于 2013-02-08T18:20:18.683 回答