I didn’t understand very well if you wanted to filter in Combobox or Listbox, so I did both, this filter direct in Combobox:
public partial class Form1 : Form
{
public string[] bairros = new string[] {
"Acari",
"Anchieta",
"Barros Filho",
"Bento Ribeiro",
"Brás de Pina",
"Bonsucesso",
"Campinho",
"Cavalcanti",
"Cascadura",
"Coelho Neto",
"Colégio",
"Complexo do Alemão",
"Cordovil",
"Costa Barros",
"Engenheiro Leal",
"Engenho da Rainha",
"Guadalupe",
"Higienópolis",
"Honório Gurgel",
"Irajá",
"Jardim América",
"Madureira",
"Marechal Hermes",
"Manguinhos",
"Oswaldo Cruz"
};
public Form1()
{
InitializeComponent();
_ListBairros.Items.AddRange(bairros);
cmbBairro.TextChanged += CmbBairro_TextChanged;
}
private void CmbBairro_TextChanged(object sender, EventArgs e) {
string texto = cmbBairro.Text;
int a = cmbBairro.Items.Count;
if (a > 0) {
int i = 0;
while (i < a) {
cmbBairro.Items.RemoveAt(0);
i++;
}
}
if (texto != "") {
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(texto, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
foreach (string lugar in bairros) {
System.Text.RegularExpressions.Match resultado = regex.Match(lugar);
if (resultado.Value != "") {
cmbBairro.Items.Add(lugar);
}
}
}
}
}
This filter in Listbox:
public partial class Form1 : Form
{
public string[] bairros = new string[] {
"Acari",
"Anchieta",
"Barros Filho",
"Bento Ribeiro",
"Brás de Pina",
"Bonsucesso",
"Campinho",
"Cavalcanti",
"Cascadura",
"Coelho Neto",
"Colégio",
"Complexo do Alemão",
"Cordovil",
"Costa Barros",
"Engenheiro Leal",
"Engenho da Rainha",
"Guadalupe",
"Higienópolis",
"Honório Gurgel",
"Irajá",
"Jardim América",
"Madureira",
"Marechal Hermes",
"Manguinhos",
"Oswaldo Cruz"
};
public Form1()
{
InitializeComponent();
_ListBairros.Items.AddRange(bairros);
cmbBairro.TextChanged += CmbBairro_TextChanged;
}
private void CmbBairro_TextChanged(object sender, EventArgs e) {
string texto = cmbBairro.Text;
_ListBairros.Items.Clear();
if (texto != "") {
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(texto, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
foreach (string lugar in bairros) {
System.Text.RegularExpressions.Match resultado = regex.Match(lugar);
if (resultado.Value != "") {
_ListBairros.Items.Add(lugar);
}
}
}
}
}
Inside the string[] neighborhoods is where the neighborhoods are.
This is my first participation in Stackoverflow, sorry for any error :P
See this implementation: http://www.codeproject.com/Tips/631196/ComboBox-with-Suggest-Ability-based-on-Substring-S @Marconi is a Windows application (Winforms) and not a web application. :)
– rubStackOverflow
Look at this other one: https://www.youtube.com/watch?v=HSBniHq5_zY
– rubStackOverflow
Opa blz, Give a look at this link it does what you’re looking for: (http://www.codeproject.com/Articles/16285/Auto-Complete-ComboBox) On this site you can download the code and put it on your system. Fix: I lowered it and just filter from the beginning of the text
– Mauricio Ferraz
@Hstackoverflow the code you quoted does exactly what I want... Just missed to give a HIGHLIGHT on the substring being typed :D Thanks
– LeandroSJRP
Not at all. @Leandrosjrp if possible post code as response to help other users with the same question. ;)
– rubStackOverflow
Sure, as soon as I finish the implementation I will post here! Hug!
– LeandroSJRP
Have you solved your problem yet? In case you haven’t solved yet, just change the Autocompletemode property from Combobox to Suggestappend, and that’s it. You don’t even need code. If served mark as answer. T+
– Fabrício Mendes
Standard Autocomplete only completes at the beginning of the string not anywhere.
– Marcus Vinicius
You can use the combobox of jqueryui which does exactly what you need. https://jqueryui.com/autocomplete/#combobox
– Luis Henrique