VB.NET Combobox - 数值的自动完成行为

发布于 2025-01-07 23:50:10 字数 1639 浏览 1 评论 0原文

我对 VB.NET(使用 .NET Framework 2.0)中组合框的自动完成行为有疑问。

我使用组合框输入数字值,并使用其下拉列表来建议可能的数字值。此列表按升序排序,例如 {"10","92", "9000", "9001"}。

组合框属性设置如下:

  • AutoCompleteMode: SuggestAppend
  • AutoCompleteSource: ListItems
  • DropDownStyle: DropDown
  • Sorted: False

DropDown 列表简单地填充如下:

  • myCombobox.Items.Add("10")
  • myCombobox.Items.Add("92")
  • myCombobox .Items.Add("9000")
  • myCombobox.Items.Add("9001")

当我没有输入任何内容,下拉列表的值顺序是正确的,按原始/升序排列。但是,当我开始输入内容时,下拉列表中的建议值将进行排序(按字母数字顺序):如果我输入“9”,建议列表将变为 {“9000”、“9001”、“92”}。

我想防止这种行为以原始/升序获取列表的值。我不知道如何...

一个可能的解决方法是用零填充列表中的值,例如 {"0010", "0092", "9000", "9001"} 但我想避免这种情况。

编辑:

根据bendataclear的建议,可以使用列表框来显示建议。 这适用于小型列表,但不能很好地扩展到大型列表。它可能对某些应用程序有用。根据 Bendataclear 给出的代码,我使它的工作方式如下:

Private Sub ComboBox1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp

    Dim cursorPos As Integer = ComboBox1.SelectionStart

    ListBox1.Items.Clear()

    For Each s In ComboBox1.Items
        If s.StartsWith(ComboBox1.Text) Then
            ListBox1.Items.Add(s)
        End If
    Next

    If ListBox1.Items.Count > 0 And ComboBox1.Text.Length > 0 Then
        ComboBox1.Text = ListBox1.Items(0)
        ComboBox1.SelectionStart = cursorPos
        ComboBox1.SelectionLength = 0
    End If

End Sub

代码尚未经过彻底测试,可以改进,但主要想法就在那里。

编辑2:

使用DataGridView可以带来更好的性能;这对我来说已经足够了。谢谢本数据清除。

只是出于好奇,欢迎任何其他答案:)

I have a problem with the auto-complete behaviour of comboboxes in VB.NET (with the .NET framework 2.0).

I am using a combobox to type in numeric values, and its DropDown list to suggest possible numeric values. This list is sorted in ascending order, for example {"10","92", "9000", "9001"}.

The combobox properties are set as follow:

  • AutoCompleteMode: SuggestAppend
  • AutoCompleteSource: ListItems
  • DropDownStyle: DropDown
  • Sorted: False

The DropDown list is simply filled like this:

  • myCombobox.Items.Add("10")
  • myCombobox.Items.Add("92")
  • myCombobox.Items.Add("9000")
  • myCombobox.Items.Add("9001")

When I don't type anything, the order of values of the DropDown list is correct, in original/ascending order. However, when I start typing something, the suggested values in the DropDown list get sorted (alphanumerically): if I type "9", the list of suggestions becomes {"9000", "9001", "92"}.

I would like to prevent this behaviour to get the values of the list in the original/ascending order. I can't figure out how...

A possible work-around would be to pad with zeroes the values in the list, e.g. {"0010", "0092", "9000", "9001"} but I would like to avoid this.

Edit:

As suggested by bendataclear, one can use a list box to display the suggestions.
This will work for small lists but doesn't scale well to large lists. It may be useful for some applications. Based on the code given by bendataclear, I made it work this way:


Private Sub ComboBox1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp

    Dim cursorPos As Integer = ComboBox1.SelectionStart

    ListBox1.Items.Clear()

    For Each s In ComboBox1.Items
        If s.StartsWith(ComboBox1.Text) Then
            ListBox1.Items.Add(s)
        End If
    Next

    If ListBox1.Items.Count > 0 And ComboBox1.Text.Length > 0 Then
        ComboBox1.Text = ListBox1.Items(0)
        ComboBox1.SelectionStart = cursorPos
        ComboBox1.SelectionLength = 0
    End If

End Sub

The code has not been thoroughly tested and can be improved, but the main idea is there.

Edit 2:

Using DataGridView leads to better performance; it was sufficient for me. Thanks bendataclear.

Just out of curiosity, any other answer is welcomed :)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2025-01-14 23:50:10

当组合框显示数据时似乎是一个问题,因为即使您设置了自定义源,它也会按字母顺序重新排序:

ComboBox1.Items.Add("10")
ComboBox1.Items.Add("92")
ComboBox1.Items.Add("9000")
ComboBox1.Items.Add("9001")

ComboBox1.AutoCompleteCustomSource.Add("10")
ComboBox1.AutoCompleteCustomSource.Add("92")
ComboBox1.AutoCompleteCustomSource.Add("9000")
ComboBox1.AutoCompleteCustomSource.Add("9001")

ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

我认为我能想到的唯一方法是创建您自己的自动完成功能,例如(未经测试):

Dim cbotxt As String = ComboBox1.Text
Dim key As String

key = ChrW(e.KeyCode)


ListBox1.Items.Clear()

For Each i In ComboBox1.Items

    Dim s As String = i.ToString()

    If s.StartsWith(ComboBox1.Text & key) Then

        ListBox1.Items.Add(s)


    End If

Next

If ListBox1.Items.Count > 0 Then
    ListBox1.Visible = True
    ComboBox1.Text = ListBox1.Items(0)

End If

编辑:

对于许多项目来说都是一个好方法(我在应用程序中使用了 10000 多个项目):

首先从列表框更改为 datagridview。
然后声明一个字符串列表并填充要自动完成的值

 Dim Numberlist as List<Of String>

' Fill List using Numberlist.Add("String")

然后在文本中更改属性:

Filter = NumberList.FindAll(AddressOf checkNum)

DataGridView1.DataSource = Filter

并添加用于检查字符串的函数。

Function checkNum(ByVal b As String) As Boolean

    If b.StartsWith(ComboBox1.Text) Then
        Return True
    Else
        Return False
    End If

End Function

此方法在我的机器上运行 10k 项的速度比我打字的速度还要快。

Seems to be an issue when the combo box displays the data, as even if you set a custom source it re-orders alphabetically:

ComboBox1.Items.Add("10")
ComboBox1.Items.Add("92")
ComboBox1.Items.Add("9000")
ComboBox1.Items.Add("9001")

ComboBox1.AutoCompleteCustomSource.Add("10")
ComboBox1.AutoCompleteCustomSource.Add("92")
ComboBox1.AutoCompleteCustomSource.Add("9000")
ComboBox1.AutoCompleteCustomSource.Add("9001")

ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

I think the only way I can think of is to create your own autocomplete something like (untested):

Dim cbotxt As String = ComboBox1.Text
Dim key As String

key = ChrW(e.KeyCode)


ListBox1.Items.Clear()

For Each i In ComboBox1.Items

    Dim s As String = i.ToString()

    If s.StartsWith(ComboBox1.Text & key) Then

        ListBox1.Items.Add(s)


    End If

Next

If ListBox1.Items.Count > 0 Then
    ListBox1.Visible = True
    ComboBox1.Text = ListBox1.Items(0)

End If

Edit:

A good approach for many items (I'm using for 10000+ in an application):

First change from a list box to a datagridview.
Then declare a list of strings and fill with values you want to autocomplete

 Dim Numberlist as List<Of String>

' Fill List using Numberlist.Add("String")

Then in the text change property:

Filter = NumberList.FindAll(AddressOf checkNum)

DataGridView1.DataSource = Filter

And add the function to check the strings.

Function checkNum(ByVal b As String) As Boolean

    If b.StartsWith(ComboBox1.Text) Then
        Return True
    Else
        Return False
    End If

End Function

This method runs on my machine with 10k items faster than I can type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文