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

问题描述

我有活动在我的 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 值包含在 =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> 文字 属性




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事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:16