'combobox auto complete any string occurrence
I'm using a ComboBox featuring auto complete. This code is being used:
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox1.DataSource = items;
DataSource's items is a List<String>. Assuming I have a list of names (like John May), searching for May will not show the ComboBox item "John May". Instead, I have to enter "John" for John May to be displayed.
What do I need to adjust so that the entire ComboBox entry is searched instead of just the first part of the string?
I'm looking forward to seeing some suggestions.
Solution 1:[1]
Obviously you can't perform such operations with an autocomplete class. Instead you can create your custom source with data table. Data table allows to search words with "Like" function.
- Create data table as a main table (Don't change the data of this table while typing in combo box).
- Load your list to the table
- perform search operations.
- Perform filter operations and load your filtered results in temporary table.
- Set temporary table as customer source of combo box.
For clear answer check this MSDN link.
Solution 2:[2]
I couldn't get the first referenced MSDN to work Mitja Bonca and the second option by Pouya Lariyan didn't work straight off for me ( I think it relies on the items being set in the UI. So with credit I'm positng the code I adapted from Pouya'
usage
autoCompleteCmb = new AutoCompleteCombobox();
autoCompleteCmb.SelectedIndexChanged += selectedIndexChanged;
this.Controls.Add(autoCompleteCmb );
var listTest = new List<string> { "Paul", "Pauline", "Daniel" };
listTest.ForEach(i=> {autoCompleteCmb.Items.Add(i)});
UserControl AutoCompleteCombobox.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyNamespace
{
public partial class AutoCompleteCombobox : ComboBox
{
public AutoCompleteCombobox()
{
InitializeComponent();
}
public IList<object> m_collectionList = null;
public AutoCompleteCombobox(IContainer container)
{
container.Add(this);
}
protected override void OnTextUpdate(EventArgs e)
{
if (m_collectionList == null)
{
m_collectionList = this.Items.OfType<object>().ToList();
}
IList<object> values = m_collectionList
.Where(x => x.ToString().ToLower().Contains(Text.ToLower()))
.ToList<object>();
this.Items.Clear();
this.Items.AddRange(this.Text != string.Empty ? values.ToArray() : m_collectionList.ToArray());
this.SelectionStart = this.Text.Length;
this.DroppedDown = true;
this.Cursor = Cursors.Default;
}
protected override void OnTextChanged(EventArgs e)
{
if (this.Text != string.Empty) return;
this.Items.Clear();
this.Items.AddRange(m_collectionList.ToArray());
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zx485 |
| Solution 2 | David Wilton |
