VB.NET组合框-数值的自动完成行为 [英] VB.NET Combobox - Auto-complete behaviour for numeric values

查看:144
本文介绍了VB.NET组合框-数值的自动完成行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在使用组合框键入数字值,并使用其DropDown列表来建议可能的数字值.此列表以升序排序,例如{"10","92","9000","9001"}.

组合框属性设置如下:

  • AutoCompleteMode:SuggestAppend
  • AutoCompleteSource:ListItems
  • DropDownStyle:DropDown
  • 排序:错误

DropDown列表只是这样填充:

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

当我什么都不输入时,DropDown列表的值顺序是正确的,以原始/升序排列.但是,当我开始键入内容时,DropDown列表中的建议值将按字母顺序排序:如果我键入"9",则建议列表将变为{"9000","9001","92"}}.

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

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

bendataclear建议,可以使用列表框显示建议. 这将适用于小型列表,但不适用于大型列表.对于某些应用程序可能很有用.根据bentataclear给出的代码,我以这种方式使其工作:

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

该代码尚未经过全面测试,可以进行改进,但是主要思想在那里.

使用DataGridView可获得更好的性能;对我来说足够了.谢谢bentataclear.

出于好奇,我们欢迎您提出其他答案:)

解决方案

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

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.

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 :)

解决方案

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.

这篇关于VB.NET组合框-数值的自动完成行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆