如何防止TextChanged事件,当我在组合框中选择一个项目? [英] How to prevent TextChanged event when I select an item in combobox?

查看:170
本文介绍了如何防止TextChanged事件,当我在组合框中选择一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 框TextChanged 我的事件= nofollow的> 组合框 等;

I have a TextChanged event on my ComboBox like;

private void comboBox1_TextChanged(object sender, EventArgs e)
{
     foreach (var item in comboBox1.Items.Cast<string>().ToList())
     {
         comboBox1.Items.Remove(item);
     }

     foreach (string item in InputBox.AutoCompleteCustomSource.Cast<string>().Where(s => s.Contains(comboBox1.Text)).ToList())
     {
         comboBox1.Items.Add(item);
     }
}



作为一个解释,当我改变ComboBox的文本,我想获得字符串值中包含的 AutoCompleteCustomSource 输入框(是的 文本框 )。

As an explanation, when I change the text of combobox, I want to get string values contains in AutoCompleteCustomSource on InputBox (which is TextBox).

当我寻找他们,但是当我选择该项目,显然框TextChanged 事件再次触发和正常工作<一个HREF =http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text.aspx相对=nofollow> 文本 组合框 将重置。

It works fine when I search them but when I select the item, obviously TextChanged event triggered again and Text property of Combobox will reset.

如何解决此问题?

推荐答案

如果我的理解正确的话,我想你想隐藏组合框的TextChange事件。如果是,那么你可以创建组合框继承自定义的控制和覆盖TextChange事件

If I understood correctly then i think you want to hide the TextChange event of the combobox. If it is then you can create a custom control inherited by ComboBox and override the TextChange event.

public partial class MyCombo : ComboBox
{
    public MyCombo()
    {
        InitializeComponent();
    }
    bool bFalse = false;
    protected override void OnTextChanged(EventArgs e)
    {
        //Here you can handle the TextChange event if want to suppress it 
        //just place the base.OnTextChanged(e); line inside the condition
        if (!bFalse)
            base.OnTextChanged(e);
    }
    protected override void OnSelectionChangeCommitted(EventArgs e)
    {
        bFalse = true;
        base.OnSelectionChangeCommitted(e);
    }
    protected override void OnTextUpdate(EventArgs e)
    {
        base.OnTextUpdate(e);
        bFalse = false; //this event will be fire when user types anything. but, not when user selects item from the list.
    }
}



编辑:
另一个简单soution是使用 TextUpdate 事件,而不是 TextChange 并保持你的组合框,因为它是没有创建另一个自定义控件

EDITED: Another simple soution is use TextUpdate event instead of TextChange and keep your combobox as it is without creating another custom control.

private void myCombo1_TextUpdate(object sender, EventArgs e)
{
    foreach (var item in myCombo1.Items.Cast<string>().ToList())
    {
        myCombo1.Items.Remove(item);
    }

    foreach (string item in myCombo1.AutoCompleteCustomSource.Cast<string>().Where(s => s.Contains(myCombo1.Text)).ToList())
    {
        myCombo1.Items.Add(item);
    }
}



TextUpdate 活动将调用组合框,只有当用户键入任何东西。但是,不是当用户从下拉列表中选择一项。那么,这会不会怨恨添加的项目。

TextUpdate event will call only when user types anything in combobox. But, not when user selects item from the drop down list. So, this will not resent the added items.

您也可以改变where条件,如果你想返回在两种情况下(上限和下限),所有符合条件的商品。假设你在列表中 1有两个项目。微软SQL服务器,2的Microsoft Office 那么会是什么结果,如果我键入微软而已。

You can also change the where condition if you wish to return all matched items in both cases(Upper and Lower). Suppose you have a two items in the list 1. Microsoft Sql Server, 2. microsoft office then what would be the result if i type microsoft only.

Where(s => s.ToLower().Contains(comboBox1.Text.ToLower()))

示例代码

Sample Code

这篇关于如何防止TextChanged事件,当我在组合框中选择一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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