【问题标题】:Auto complete in ComboBox windows forms在 ComboBox 窗体中自动完成
【发布时间】:2012-05-28 21:48:42
【问题描述】:

我在组合框中有自动完成功能,它可以正常工作但几乎没有问题,我让建议列表出现在文本长度超过 3 个字符的情况下,但列表出现在 4 个字符的长度之后(当用户输入文本的第五个字符),这意味着当列表第一次执行的代码没有出现在这里时,是Text_Changed事件中的代码

 Private Sub TxtItem_TextChanged(sender As System.Object, e As System.EventArgs) Handles TxtItem.TextChanged


    If Trim(TxtItem.Text) <> "" And Trim(TxtItem.Text).Length > 3 Then
        'Autocomplete
        Dim Bl As New ItemBL
        Dim suggestions = Bl.DisplayLikeNameList(Trim(TxtItem.Text))
        Dim MySource As New AutoCompleteStringCollection()
        MySource.AddRange(suggestions.ToArray)

        With TxtItem
            .AutoCompleteMode = AutoCompleteMode.Suggest
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            .AutoCompleteCustomSource = MySource
        End With
        TxtItem.Select(TxtItem.Text.Length, 0)
        Exit Sub
    Else
        TxtItem.AutoCompleteMode = AutoCompleteMode.None
    End If

End Sub

注意:此问题会导致应用程序在 Windows XP 上退出(有时),但不会影响 Windows 7

【问题讨论】:

  • 导致应用程序退出? 如何?
  • 有时当用户输入文本多3个字符时应用程序退出,不显示异常消息或其他东西,直接退出
  • 不要在事件处理程序中更改 AutoCompleteMode,这有很多副作用。如果您不想显示任何内容,请确保源为空。

标签: vb.net winforms autocomplete combobox


【解决方案1】:

在这篇文章中找到了答案:Dynamically changing Textbox's AutoComplete List causes AccessViolationException, any advice?

所以我必须制作全局建议列表并在表单load 事件中初始化AutoComplete 属性,并且效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2018-11-25
    • 2010-10-15
    相关资源
    最近更新 更多