How to create a combo box "AutoFill" or a text box to filter text containing a string

How to create an autocomplete ComboBox or TextBox that filters text based on a string?

For example: if I find "a" in a TextBox, I can only see all lines containing "a".

+5
source share
6 answers

If you mean suggestions for showing, then it's just a matter of changing the property when you selected a TextBox in your IDE:

The parameters shown in the figure allow you to change the rules for autocomplete text in the text field, as well as the source for offers. (Visual studio 2010)

Properties

, TextBox.

MSDN

+8

autocomplete Windows Forms Shell, "BeginWith" Windows Vista. , Windows Vista, IAutoComplete2:: SetOptions, ACO_NOPREFIXFILTERING. , .

+3

comboDropDown.

    void comboBox_Leave(object sender, EventArgs e)
    {
       ComboBox cbo = (sender as ComboBox);
        if (cbo.Text.Length > 0)
        {
            Int32 rowIndex = cbo.FindString(cbo.Text.Trim());
            if (rowIndex != -1)
            {
                cbo.SelectedIndex = rowIndex;
            }
            else
            {
                cbo.SelectedIndex = -1;
            }
        }
        else
        {
            cbo.SelectedIndex = -1;
        }

    }
+1

. , , :

public partial class Form1 : Form
    {

        List<String> data;
        ListView lst = new ListView();
        TextBox txt = new TextBox();

        public Form1()
        {
            InitializeComponent();
            data = new List<string>("Lorem ipsum dolor sit amet consectetur adipiscing elit Suspendisse vel".Split());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Controls.Add(txt);
            lst.Top = 20;
            txt.TextChanged += new EventHandler(txt_TextChanged);
            lst.View = View.List;
            this.Controls.Add(lst);
            list_items("");
        }

        void txt_TextChanged(object sender, EventArgs e)
        {
            list_items(txt.Text);
        }

        void list_items(string filter)
        {
            lst.Items.Clear();
            List<string> results = (from d in data where d.Contains(filter) select d).ToList();
            foreach (string s in results)
            {
                lst.Items.Add(s);
            }
        }
    }

combobox, :

cbo.AutoCompleteSource = AutoCompleteSource.ListItems;
cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

ListItems .

0

, , # , , . . , , combobox, , combobox. combobox KeyUp.

( , , ), DataTable, dt_source, combobox, : id (int) ().

DataTable dt_source = new DataTable("table");
dt_source.Columns.Add("id", typeof(int));
dt_source.Columns.Add("name", typeof(string));

And here is my KeyUp method as follows:

private void cmb_box_KeyUp(object sender, KeyEventArgs e)
{
  string srch = cmb_box.Text;
  string srch_str = "ABackCDeleteEFGHIJKLMNOPQRSpaceTUVWXYZD1D2D3D4D5D6D7D8D9D0"; 
  if (srch_str.IndexOf(e.KeyCode.ToString()) >= 0)
  {
    cmb_box.DisplayMember = "name"; // we want name list in the datatable to be shown
    cmb_box.ValueMember = "id"; // we want id field in the datatable to be the value
    DataView dv_source = new DataView(dt_source);  // make a DataView from DataTable so ...
    dv_source.RowFilter = "name LIKE '%"+ srch +"%'"; // ... we can filter it
    cmb_box.DataSource = dv_source; // append this DataView as a new source to the combobox         
    /* The 3 lines below is the tricky part. If you repopulate a combobox, the first
       item in it will be automatically selected so let unselect it*/
    cmb_box.SelectedIndex = -1; // unselection
    /* Again when a combobox repopulated the text region will be reset but we have the
       srch variable to rewrite what written before */
    cmb_box.Text = srch;
    /* And since we're rewriting the text region let make the cursor appear at the
       end of the text - assuming 100 chars is enough*/
    cmb_box.Select(100,0);
    cmb_box.DroppedDown = true; // Showing the dropdownlist
   }
}
0
source

I find it best to override the OnKeyDown event (KeyEventArgs e) and use the value to filter ComboBox.AutoCompleteCustomSource. Then, as noted above, change the autocomplete option to autocomplete. SourceListItems.

-2
source

All Articles