如何防止在下拉列表中的ComboBox自动选择除完全匹配? [英] How can I prevent auto-select in ComboBox on drop-down except for exact matches?

查看:166
本文介绍了如何防止在下拉列表中的ComboBox自动选择除完全匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ComboBox是与包含数据库中的行的数据网格相关的详细显示的一部分。没有绑定到ComboBox存在,我手动这样做。

I have a ComboBox that is part of a detail display related to a data grid containing rows from a database. No binding to the ComboBox exists, I am doing this manually. The ComboBox allows manual entry, as if it were a text field, while still providing a drop-down of choices.

我的问题是,如果我手动输入了文本,该字段,并且下拉单击,ComboBox显然想要寻找一个匹配。此外,搜索很简单,因此 KG 匹配 KG / Day 。我必须避免这种情况,并强制完全匹配。

My issue is that if I have manually entered text in the field, and the drop-down is clicked, the ComboBox apparently wants to seek out a match. Also, it appears that the search is simple, so KG matches KG/Day. I must avoid this and force an exact match.

但是,进一步,我想我需要能够管理整个过程自己,因为进一步复杂的事情,下拉项目实际上会读取 KG / Day - kilograms / day 。但是,从中获取数据的数据库字段仅存储连字符之前的部分,因此 KG / Day

But further, I think I need to be able to govern the entire process myself, because to further complicate the matter, the drop-down item would actually read KG/Day - kilograms/day. The database field from which the data is fetched, however, is only storing the portion prior to the hyphen, so KG/Day.

所以,我需要拦截下拉动作,允许我做两件事情:

So, I need to intercept the drop-down action in a way that allows me to do two things:

1)执行自己的搜索无论我是否有自组织文本或真实匹配。因为它最初是从下拉列表中选择;换句话说,我有 KG / Day ,而不是 KG

1) Perform my own search to find whether or not I have ad-hoc text, or a "real" match. As in that it was originally selected from the drop-down; in other words, that I have KG/Day and not just KG.

2)消除ComboBox想要做的自动搜索行为。

2) Eliminate the auto-search behavior that ComboBox wants to do.

我试图在窗体中使用方法处理程序,例如

I have tried getting in front of these things using method handlers in the Form, such as


ComboBox :: DropDown()和ComboBox :: DropDownClosed(),

ComboBox::DropDown() and ComboBox::DropDownClosed(),

但似乎这些仍然不允许我停止基本的ComboBox搜索/匹配。

but it seems these still don't allow me to stop the basic ComboBox searching/matching.

我也试过创建一个我自己继承自ComboBox的类,但我不知道该重写什么,或者一般如何去获得我想要的,停止我不做什么。

I have also tried creating a class of my own inherited from ComboBox, but I don't really know what to override, or in general how to go about getting what I want, stopping what I don't.

因此,我感谢你的建议。

So, with that, I thank you for your advice.

编辑:展开我已经尝试了...
在我的继承类,我试图使用 WndProc 覆盖。根据我在另一个论坛中发现的一些建议,我的目标是拦截ComboBox消息 LB_FINDSTRING ,并替换为 LB_FINDSTRINGEXACT 。该帖子建议ComboBox默认为 LB_FiNDSTRING ,这符合我的看法,并且subbing LB_FINDSTRINGEXACT 会修复问题。麻烦的是,除非我有一个坏的定义 LB_FINDSTRING ,它从来没有收到。

to expand on what I tried already... In my inherited class, I was attempting to use a WndProc override. Based on some advice I found in another forum, my goal was to intercept the ComboBox message LB_FINDSTRING and replace it with LB_FINDSTRINGEXACT. The post suggested that ComboBox defaulted to LB_FiNDSTRING, which fits what I see it doing, and that subbing LB_FINDSTRINGEXACT would cure the problem. Trouble is, unless I got a bad definition for LB_FINDSTRING, it was never received.

这是我的枚举: / p>

Here's my enum:

[Flags]
public enum ListBoxFlags
{
    LB_ADDSTRING = 0x0180,
    LB_SETSEL = 0x0185,
    LB_GETSELITEMS = 0x0191,
    LB_GETSELCOUNT = 0x0190,
    LB_GETCURSEL = 0x0188,
    LB_SELECTSTRING = 0x018C,
    LB_SETCURSEL = 0x0186,
    LB_FINDSTRING = 0x018F,
    LB_FINDSTRINGEXACT = 0x01A2,
    LB_GETCOUNT = 0x018B,
    LB_GETSEL = 0x0187,
    LB_GETTEXT = 0x0189,
    LB_RESETCONTENT = 0x0184,
    LB_SETHORIZONTALEXTENT = 0x0194,
    LB_GETHORIZONTALEXTENT = 0x0193,
    LB_GETTOPINDEX = 0x018E,
    LB_SETTOPINDEX = 0x0197,
    LB_INSERTSTRING = 0x0181,
    LB_DELETESTRING = 0x0182,
    LB_GETITEMDATA = 0x0199
}


推荐答案

制作一些示例代码,

这个想法是处理 TextChanged ComboBox ,只是修改 ComboBox列表项。下面的示例将修改列表以添加当前文本(最重要的是,当您单击组合框时不会更改文本)和符合搜索条件的任何其他项目。

The idea is to handle the TextChanged event of the ComboBox, and really just modify the ComboBox list items at that point. The example below will modify the list to add the current text (most important, as this will not change the text when you click the combobox) and any other items that meet the search criteria.

我不认为你需要代码重新初始化列表项时焦点丢失,但留在那里,以防万一。

I don't think you need the code to re-initialize the list items when focus is lost, but left in there just in case.

    //contains a list of default items for the combobox items
    List<string> comboList = new List<string>();

    public Form1()
    {
        InitializeComponent();
        initComboList(); //initialize the defaults
        initCombobox(); //initialize the combobox list items
    }

    //fills the defaults for the combobox items
    private void initComboList()
    {
        comboList.Add("red");
        comboList.Add("blue");
        comboList.Add("green");
    }

    //initializes the combobox items
    private void initCombobox()
    {
        comboBox1.Items.Clear();
        foreach (string s in comboList)
            comboBox1.Items.Add(s);
    }

    //occurs when the text changes in the combobox
    private void comboBox1_TextChanged(object sender, EventArgs e)
    {
        string curtext = comboBox1.Text;
        insertIntoComboBox(curtext);   //insert the current text into combobox
        comboBox1.Select(curtext.Length, 0); //if you don't do this, the cursor goes back to index 0 :-(
    }

    //called whenever is desired to insert the current text into the combobox items
    private void insertIntoComboBox(string curtext)
    {
        comboBox1.Items.Clear();
        //only add the current text if it's not already in the list of defaults and not empty string
        if (comboList.Contains(curtext) == false && curtext.Length > 0)
            comboBox1.Items.Add(curtext);
        foreach (string s in comboList)
                comboBox1.Items.Add(s);
    }

    //called whenever combobox loses focus
    private void comboBox1_Leave(object sender, EventArgs e)
    {
        initCombobox();
    }

这篇关于如何防止在下拉列表中的ComboBox自动选择除完全匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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