C#防止自定义组合框自动选择项目 [英] c# preventing custom combobox from autoselecting an item

查看:54
本文介绍了C#防止自定义组合框自动选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C#中实现自己的ComboBox类,因为直到3.5 NET Framework(如果我没有记错),建议查找都是使用 StartWith函数进行的(即,如果列表中包含 Doe,John,并且用户键入 John,则不会显示该项目)。基本上,我是在文本更改事件中添加或删除项目,从列表的初始内容中获取它们。一切都可以很好地满足我的需求,唯一的问题是,当单击ComboBox时,即使它不等于插入的文本,也仍在选择一个项目。按照我做的示例,我希望仅当用户单击它时才选择 Doe,John(并将其设置为ComboBox.Text属性),如果用户只是键入 John并且没有项目与它严格相等(不仅仅是包含它),那么Text属性必须在用户插入时保留。这是我的派生类的代码

i am trying to implement my own ComboBox class in C# because, untill 3.5 NET Framework (if i'm not mistaking) suggestion lookup is made with a "StartWith" function (i.e. if the list contains "Doe, John" and user types "John", that item is not displayed). Basically i'm adding or removing items on text change event, getting them from the initial content of the list. Everything works pretty fine for what i am looking for, the only issue is, when ComboBox is clicked out, an item is still being selected even though it is not equal to the inserted text. Following the example i did, i want that "Doe, John" is selected (and set as ComboBox.Text property) only if user clicked on it, if user just typed "John" and no item is strictly equal to it (not just contain it), then Text property must remain as the user inserted it. Here's the code of my derived class

public class customTB : ComboBox
{
    private object[] startlist;
    public customTB() : base()
    {
        this.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.None;
        this.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.None;
        this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
        this.Sorted = true;
        this.KeyPress += customTB_KeyPress;
        this.TextChanged += customTB_TextChanged;
        this.Enter += customTB_Enter;
    }

    void customTB_Enter(object sender, EventArgs e)
    {
        this.DroppedDown = (this.Items.Count > 0);
    }

    void customTB_TextChanged(object sender, EventArgs e)
    {
        UpdateList();
    }

    void customTB_KeyPress(object sender, KeyPressEventArgs e)
    {
        this.DroppedDown = (this.Items.Count>0);
    }

    void UpdateList()
    {
        if (this.startlist == null)
        {
            //get starting lists elems
            this.startlist = new Object[this.Items.Count];
            this.Items.CopyTo(this.startlist, 0);
        }
        this.BeginUpdate();
        foreach (object o in startlist)
        {
            if (o.ToString().Contains(this.Text))
            {
                if (!this.Items.Contains(o))
                    this.Items.Add(o);
            }
            else if (this.Items.Contains(o))
                this.Items.Remove(o);
        }
        this.EndUpdate();
    }
}

如果尝试过,则随时可以尝试退出ComboBox ,文本突出显示,并且其值设置为项目。
我想拥有的示例是:
项包含 Doe John, Smith John, Smith Marie。
,如果用户键入 John,则下拉菜单项为 Doe John和 Smith John,但是如果他未单击任何下拉菜单元素并退出ComboBox(即,在外部单击),则文本仍为约翰

If tried, any time you try to exit the ComboBox, Text is highlighted and its value is set to an item. As example of what i would like to have is: items contains "Doe John", "Smith John", "Smith Marie". if user types "John", then dropdown items are "Doe John" and "Smith John" but if he doesn't click any of the dropdown elements and exit the ComboBox (i.e. clicking outside), the Text remains "John"

推荐答案

有一个布尔变量itemClicked

Have one boolean variable itemClicked


  • 在Enter事件处理程序中将itemClicked设置为false

  • 在SelectionChangeCommitted事件处理程序中将itemClicked设置为true

  • 将Text属性设置为string.Empty不是DropDownClosed事件处理程序内部的itemClicked

这篇关于C#防止自定义组合框自动选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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