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

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

问题描述

我有 TextChanged 活动在我的 ComboBox like;

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

foreach(InputBox.AutoCompleteCustomSource.Cast< string>()中的字符串项)其中(s => s.Contains(comboBox1.Text))。ToList b $ b {
comboBox1.Items.Add(item);
}
}



作为说明,当我更改combobox ,我想获得 string 值包含在 InputBox (这是 AutoCompleteCustomSource =http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.aspx =nofollow> TextBox )。



当我搜索它们时,它工作正常,但是当我选择该项目,显然 TextChanged 事件再次触发, href =http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text.aspx =nofollow> 文字 属性

如果我理解正确,我想你想隐藏组合框的TextChange事件。如果是,那么您可以创建一个由ComboBox继承的自定义控件并覆盖TextChange事件。

  public partial class MyCombo:ComboBox 
{
public MyCombo()
{
InitializeComponent();
}
bool bFalse = false;
protected override void OnTextChanged(EventArgs e)
{
//这里你可以处理TextChange事件,如果想压制它
//只是放置base.OnTextChanged(e) ;行内的条件
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; //当用户输入任何内容时,此事件将触发。但不是当用户从列表中选择项目时。
}
}

已编辑:
另一个简单的办法是使用 TextUpdate 事件而不是 TextChange ,并保持您的组合框,而不创建另一个自定义控件。

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

foreach(myCombo1.AutoCompleteCustomSource.Cast< string>()中的字符串项)其中(s => s.Contains(myCombo1.Text))。ToList b $ b {
myCombo1.Items.Add(item);
}
}

TextUpdate 事件将仅当用户在组合框中键入任何内容时才调用。但是,当用户从下拉列表中选择项目时。



如果您希望在两种情况(上和下)中返回所有匹配的项目,您也可以更改where条件。假设您在列表中有两个项目 1。 Microsoft Sql Server,2. microsoft office 然后如果我键入 microsoft 的结果会是什么。

 其中(s => s.ToLower()。包含(comboBox1.Text.ToLower()))

示例代码




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);
     }
}

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

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.

How to solve this?

解决方案

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.
    }
}

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 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.

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天全站免登陆