组合框自动完成问题

时间:2013-02-08 11:24:38

标签: vb.net

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

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

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

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

我在使用了combobox_Leave之后尝试使用mycombo.AutoCompleteSource = AutoCompleteSource.CustomSource,但是关闭了自动完成功能。

问题是:
这里有可能在模式使用后设置描述的组合框,就像它在程序开始时一样,当它没有被编辑时,所以我可以在初始方式通过这样的组合框导航键盘,但是如果我需要编辑它,那么autosuggest选项仍保持启用状态试。

EDITED: 这是一个显示问题的简单示例:

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”,然后尝试使用键向下箭头再次导航。

1 个答案:

答案 0 :(得分: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

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