I've solved it :)
sorry it is in C# too, but I think it is very easy to convert it to VB.Net
You need to:
- Have a normal ComboBox (DropDownStyle = DropDown, AutoCompleteMode = None, AutoCompleteSource = None), lets call it: comboBox1
- Add the events SelectedIndexChanged, and TextUpdate
Then use the following code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace CB_Contains
{
public partial class Form1 : Form
{
private Dictionary<String, System.Int32> CBFullList;
private Dictionary<String, System.Int32> CBFilteredList;
bool ComboBoxBusy;
public Form1()
{
InitializeComponent();
ComboBoxBusy = false;
CBFullList = new Dictionary<string, Int32>();
CBFilteredList = new Dictionary<string, Int32>();
}
private void Form1_Load(object sender, EventArgs e)
{
CBFullList.Add("None", 0);
CBFullList.Add("ABC 123", 1);
CBFullList.Add("Costco, 123 1st Avenue, Sherbrooke", 2);
CBFullList.Add("Provigo, 344 Ball Street, Sherbrooke", 3);
CBFullList.Add("Sherbox, 93 7th Street, Montreal", 4);
FilterList(false);
}
private void FilterList(bool show)
{
if (ComboBoxBusy == false)
{
String orgText;
ComboBoxBusy = true;
orgText = comboBox1.Text;
comboBox1.DroppedDown = false;
CBFilteredList.Clear();
foreach (KeyValuePair<string, Int32> item in CBFullList)
{
if (item.Key.ToUpper().Contains(orgText.ToUpper()))
CBFilteredList.Add(item.Key, item.Value);
}
if (CBFilteredList.Count < 1)
CBFilteredList.Add("None", 0);
comboBox1.BeginUpdate();
comboBox1.DataSource = new BindingSource(CBFilteredList, null);
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
comboBox1.DroppedDown = show;
comboBox1.SelectedIndex = -1;
comboBox1.Text = orgText;
comboBox1.Select(comboBox1.Text.Length, 0);
comboBox1.EndUpdate();
Cursor.Current = Cursors.Default;
ComboBoxBusy = false;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ComboBoxBusy == false)
{
FilterList(false);
}
}
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
FilterList(true);
}
}
}
Well, that's it :) I hope my way is very easy