-1

I have an combobox poppulated with items from a database table. When I change the text i repopulate the combobox with items from the database table.

But when I enter text and the list with suggestions opens no item in de list is selected. And i want a item to be selected so when you press enter that it becomes the selected item.

This is a winforms application.

Thanks.

 cbxNaam.Items.Clear();
 string query = "SELECT bedr_naam FROM tblbedrijf WHERE bedr_naam LIKE '%" + cbxNaam.Text + "%'";
 string[] bedrijfsnamen = Functions.DataTableToArray(Global.db.Select(query));
 cbxNaam.Items.AddRange(bedrijfsnamen);
 cbxNaam.Select(cbxNaam.Text.Length + 1, 0);
chridam
  • 100,957
  • 23
  • 236
  • 235
Willem T
  • 104
  • 2
  • 12

3 Answers3

1

You need to select an item in the list - it looks like you are trying to select some text in an item.

piecing together some info from your other comments it looks like you want to essentially do a wildcard match on the text in the dropdown list, to do this you will need to modify behaviour of the handler for the text changed event/method, either derive from combobox and override it or ad an event listener on your instance.

you can then do a search on the data in your array - something like

private void cbxNaam_TextChanged(object sender, EventArgs e)
{
     var matchingStrings = bedrijfsnamen.Where(s => s.Contains(cbxNaam.Text));
     cbxNaam.SelectedItem = matchingStrings[0];
}

you will need to be a little careful of multiple matching items etc.

dice
  • 2,820
  • 1
  • 23
  • 34
0

If you are using WINFORMS,

You have to use AutoCompleteMode set to AutoCompleteMode.Append and AutoCompleteSource set to AutoCompleteSource.ListItems

Please check this answer ComboBox AutoComplete Custom Capabilities

Community
  • 1
  • 1
Krishnakumar
  • 266
  • 2
  • 7
  • But when i use this it only autocompletes from the start of the string. So when i enter a partial string of the name that is halfway in the string it does not work – Willem T Aug 29 '12 at 09:35
  • Sorry, windows combo box AutoComplete [match with prefix only](http://stackoverflow.com/a/2314248/1547740)... So you have to write your custom control. – Krishnakumar Aug 29 '12 at 09:46
0

I don't know if I understand you good, sorry if not! Here is my answer:

If you want for your comboBox to be populated when you write in you comboBox you need to set its properties:

AutoCompleteMode to Append and AutoCompleteSource to ListItems

on Enter your item will be selected.

Hope I helped you? Rock On!!!:-)

Sylca
  • 2,523
  • 4
  • 31
  • 51