1

I have a combo box called cbProduit; the combo box is being filled via a web service:

ComboBoxItemProduit produiItem = new ComboBoxItemProduit();
produiItem.Text = articleArray.GetAllArticlesResult[i].S_MODELE;
produiItem.Value = articleArray.GetAllArticlesResult[i].S_ID;
cbProduit.Items.Add(produiItem);

The problem is, the combo box, when it is filled, contains more than 30000 items and I need to make a search by text.

Note: I don't have any relation with a database all the info came from a Web Service.

Can anyone help, please?

  • 1
    did you try to use textchanged event of combobox? I think you can filter data in this event. – Vecihi Baltacı Dec 18 '18 at 11:15
  • No I did not used Text changed and if I could use how can I filter the items? – Abdellah Brashe Dec 18 '18 at 11:18
  • Try to limit item count for example with additional filter. It's not good practice to have such big items count in a control. There is a big possibility that you don't have to have them all. And the question is - do you want to filter items by text (like for example in Google)? If so, then you need to have edit and a dynamic listbox. – Adam Jachocki Dec 18 '18 at 11:18
  • it is a windows form application – Abdellah Brashe Dec 18 '18 at 11:18
  • I need to have them all listed in the combo box and yes I need when I type something in the combo it filters and show me only the items with the litters I typed – Abdellah Brashe Dec 18 '18 at 11:20
  • You can use this approach => https://stackoverflow.com/a/40990757/4315620 – Serhat Oz Dec 18 '18 at 11:21
  • Possible duplicate of [Search Combo Box like Google Search](https://stackoverflow.com/questions/15878862/search-combo-box-like-google-search) – dymanoid Dec 18 '18 at 11:22
  • @dymanoid I don't have any relation with the database all the Items came from a web service – Abdellah Brashe Dec 18 '18 at 11:23

3 Answers3

1

there are two options I can see that fits your description.

option 1:

you set an autocomplete property for the combobox like this:

comboBox1.DataSource = dt;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "VALUE";
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

but then, it would work only on your first character and will show a list like this: enter image description here

option 2:

putting a new text box with a textChanged event:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(textBox1.Text))
    {
        comboBox1.DataSource = dt; //your origin data

    }
    else
    {
        var newTable = dt.AsEnumerable()
      .Where(x => x.Field<string>("VALUE").ToUpper().Contains(textBox1.Text.ToUpper()))
      .CopyToDataTable();

        comboBox1.DataSource = newTable;

    }
}

while dt is your origin data that came from the server and the result is this: enter image description here

Dor Lugasi-Gal
  • 1,430
  • 13
  • 35
0

You can store the values of the combo box in an array and search that array for items. Use the following approach

//Declare a list of string in the general declarations section of the form as follows
List<string> liststr = new List<string>();
//Add this line when populating the combo box
liststr.Add(produiItem);
//under the text changed event of the combo box, add these lines of code.
cbProduit.Items.Clear;
foreach (string search in liststr)
{
    if (search.StartsWith(cbProduit.Text)) { 
          cbProduit.Items.Add(search);

    }    
}
preciousbetine
  • 2,959
  • 3
  • 13
  • 29
0

try this no database required for this search

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script> <p>Use this area to provide additional information.</p> <select id="drop" class="js-example-basic-single"  style="width:200px;">
    <option>Hiii</option>
    <option>I</option>
    <option>Am</option>
    <option>Doing</option>
    <option>Asp</option>
    <option>MVC</option>

</select>

<script>$(document).ready(function () {
    $('.js-example-basic-single').select2();    });
Shubham
  • 443
  • 2
  • 10