Click here to Skip to main content
15,850,921 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Autocomplete Combobox match any part of string not only beginning

if combobox have item
abc 123
pqr 234
123 abc
pqr 123

and if any one search 23 and all will pop up if any one search 12 then 3 result will get popup in combobox

please help me its very urgent

What I have tried:

i tried many code which is google but not got result

combobox suggesst and append all are fail to work in visual studio
Posted
Updated 20-May-23 10:50am
Comments
OriginalGriff 10-Jan-20 4:26am    
So show us the code you did try, explain exactly what it did that you didn't expect, or didn't do that you did.
Tell us what you tried to fix it.
Help us to help you!

Use the "Improve question" widget to edit your question and provide better information.

ComboBox with Suggestions Based on Loose Character Search[^]


Follow above link the man with great logic and great solution...
 
Share this answer
 
I've solved it :)

You need to:

1. Have a normal ComboBox (DropDownStyle = DropDown, AutoCompleteMode = None, AutoCompleteSource = None), lets call it: comboBox1
2. 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("pqr 234", 2);
            CBFullList.Add("123 abc", 3);
            CBFullList.Add("pqr 123", 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);
        }
    }
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900