none
autocomplete textbox on listbox RRS feed

  • Question

  • hi may i know how do i peform the autocompletete feature on my listbox data based on the input on my textbox?
    The data is first loaded to the listbox on startup. when user key in any input to the textbox, the listbox data should be filter out based on the autocomplete feature.
    Please kindly help.
    alain
    Sunday, November 22, 2009 10:45 AM

Answers

  • Hi,

    You can use the following code for datagridview.

            DataTable dt = new DataTable();
            bool IsBound = false;
            private void Form3_Load(object sender, EventArgs e)
            {
                Form2 f1 = new Form2();
                f1.MdiParent = this;
                f1.Show();
    
                Form2 f2 = new Form2();
                f2.MdiParent = this;
                f2.Show();
    
                dt.Columns.Add("Id", typeof(int));
                dt.Columns.Add("Name", typeof(string));
                dt.Rows.Add(1, "Foo");
                dt.Rows.Add(2, "Hoo");
                dt.Rows.Add(3, "Too");
                dt.Rows.Add(4, "Fii");
                dt.Rows.Add(5, "Fee");
                dt.Rows.Add(6, "Fuu");
                dt.Rows.Add(7, "Noo");
    
                listBox1.DataSource = dt;
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Id";
    
                dataGridView1.DataSource = dt;
    
                IsBound = true;
            }
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                DataTable clone = dt.Clone();
                foreach (DataRow dr in dt.Select("Name like '" + textBox1.Text + "%'"))
                {
                    clone.ImportRow(dr);
                }
                IsBound = false;
                listBox1.DataSource = clone;
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Id";
    
                dataGridView1.DataSource = clone;
    
                IsBound = true;
            }
    
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (IsBound)
                {
                    int value = Convert.ToInt32(listBox1.SelectedValue);
                    listBox1.DataSource = dt;
                    listBox1.DisplayMember = "Name";
                    listBox1.ValueMember = "Id";
                    listBox1.SelectedValue = value;
                }
            }
    
            private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (IsBound)
                {
                    int value = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                    dataGridView1.DataSource = dt;
                    dataGridView1.ClearSelection();
                    foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                    {
                        if (Convert.ToInt32(dgvr.Cells[0].Value) == value)
                        {
                            dgvr.Selected = true;
                        }
                    }
                }
            }
    • Marked as answer by kkkJoe Monday, November 23, 2009 1:08 AM
    Sunday, November 22, 2009 1:55 PM

All replies

  • Hi,

    Here is the sample both selects or shows the items that matches to the criteria in textbox.

            DataTable dt = new DataTable();
            private void Form3_Load(object sender, EventArgs e)
            {
    
                dt.Columns.Add("Id", typeof(int));
                dt.Columns.Add("Name", typeof(string));
                dt.Rows.Add(1, "Foo");
                dt.Rows.Add(2, "Hoo");
                dt.Rows.Add(3, "Too");
                dt.Rows.Add(4, "Fii");
                dt.Rows.Add(5, "Fee");
                dt.Rows.Add(6, "Fuu");
                dt.Rows.Add(7, "Noo");
    
                listBox1.DataSource = dt;
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Id";
                listBox1.SelectionMode = SelectionMode.MultiExtended;
            }
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                DataTable clone = dt.Clone();
                foreach (DataRow dr in dt.Select("Name like '" + textBox1.Text + "%'"))
                {
                    clone.ImportRow(dr);
                }
                listBox1.DataSource = clone;
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Id";
                //or
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    listBox1.SetSelected(i, false);
                    if (listBox1.GetItemText(listBox1.Items[i]).StartsWith(textBox1.Text))
                    {
                        listBox1.SetSelected(i, true);
                    }
                }
    
            }
    Sunday, November 22, 2009 11:12 AM
  • Thanks for the input, it helps.
    however, after the data in the listbox is filtered, i have problem displaying all the correct data to all my textbox when i select one of the listbox item.
    i think is because of the index problem.

     

    what i did previously was this..

            private void gvEmp_MouseUp(object sender, MouseEventArgs e)
            {
                int m = gvEmp.CurrentRow.Index;
                LoadDataToTextbox(m);
            }
       private void LoadDataToTextbox(int currentRow)
            {
                try
                {
                    if (dv.Count == 0) return;
                    if (currentRow < 0 || currentRow >= gvEmp.Rows.Count) return;
                    lblEmpID.Text = dv[currentRow]["ID"].ToString();
                    txtEmpName.Text = dv[currentRow]["Name"].ToString();
                  

    if i want to use the textBox1_TextChanged event to filter the data in listbox, what is the best way to load the correct data?


    alain
    Sunday, November 22, 2009 1:09 PM
  • Hi,

    Could you try the code below.

            DataTable dt = new DataTable();
            bool IsBound = false;
            private void Form3_Load(object sender, EventArgs e)
            {
                Form2 f1 = new Form2();
                f1.MdiParent = this;
                f1.Show();
    
                Form2 f2 = new Form2();
                f2.MdiParent = this;
                f2.Show();
    
                dt.Columns.Add("Id", typeof(int));
                dt.Columns.Add("Name", typeof(string));
                dt.Rows.Add(1, "Foo");
                dt.Rows.Add(2, "Hoo");
                dt.Rows.Add(3, "Too");
                dt.Rows.Add(4, "Fii");
                dt.Rows.Add(5, "Fee");
                dt.Rows.Add(6, "Fuu");
                dt.Rows.Add(7, "Noo");
    
                listBox1.DataSource = dt;
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Id";
                IsBound = true;
            }
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                DataTable clone = dt.Clone();
                foreach (DataRow dr in dt.Select("Name like '" + textBox1.Text + "%'"))
                {
                    clone.ImportRow(dr);
                }
                IsBound = false;
                listBox1.DataSource = clone;
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Id";
                IsBound = true;
            }
    
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (IsBound)
                {
                    int value = Convert.ToInt32(listBox1.SelectedValue);
                    listBox1.DataSource = dt;
                    listBox1.DisplayMember = "Name";
                    listBox1.ValueMember = "Id";
                    listBox1.SelectedValue = value;
                }
            }
    Sunday, November 22, 2009 1:17 PM
  • would this  be the same if i want to change to gridview ? there is no gvemp_selectedindexchanged for gridview.
    alain
    Sunday, November 22, 2009 1:33 PM
  • Hi,

    You can use rowHeaderMouseClick or Rowclick event on datagridview.
    Sunday, November 22, 2009 1:36 PM
  • hi i tried this but to no avail.



    private void gvEmp_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
    if (IsBound)
    { int value = gvEmp.CurrentRow.Index; gvEmp.DataSource = dt; gvEmp.CurrentRow.Index = value;
    } }

    alain
    Sunday, November 22, 2009 1:49 PM
  • Hi,

    You can use the following code for datagridview.

            DataTable dt = new DataTable();
            bool IsBound = false;
            private void Form3_Load(object sender, EventArgs e)
            {
                Form2 f1 = new Form2();
                f1.MdiParent = this;
                f1.Show();
    
                Form2 f2 = new Form2();
                f2.MdiParent = this;
                f2.Show();
    
                dt.Columns.Add("Id", typeof(int));
                dt.Columns.Add("Name", typeof(string));
                dt.Rows.Add(1, "Foo");
                dt.Rows.Add(2, "Hoo");
                dt.Rows.Add(3, "Too");
                dt.Rows.Add(4, "Fii");
                dt.Rows.Add(5, "Fee");
                dt.Rows.Add(6, "Fuu");
                dt.Rows.Add(7, "Noo");
    
                listBox1.DataSource = dt;
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Id";
    
                dataGridView1.DataSource = dt;
    
                IsBound = true;
            }
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                DataTable clone = dt.Clone();
                foreach (DataRow dr in dt.Select("Name like '" + textBox1.Text + "%'"))
                {
                    clone.ImportRow(dr);
                }
                IsBound = false;
                listBox1.DataSource = clone;
                listBox1.DisplayMember = "Name";
                listBox1.ValueMember = "Id";
    
                dataGridView1.DataSource = clone;
    
                IsBound = true;
            }
    
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (IsBound)
                {
                    int value = Convert.ToInt32(listBox1.SelectedValue);
                    listBox1.DataSource = dt;
                    listBox1.DisplayMember = "Name";
                    listBox1.ValueMember = "Id";
                    listBox1.SelectedValue = value;
                }
            }
    
            private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (IsBound)
                {
                    int value = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                    dataGridView1.DataSource = dt;
                    dataGridView1.ClearSelection();
                    foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                    {
                        if (Convert.ToInt32(dgvr.Cells[0].Value) == value)
                        {
                            dgvr.Selected = true;
                        }
                    }
                }
            }
    • Marked as answer by kkkJoe Monday, November 23, 2009 1:08 AM
    Sunday, November 22, 2009 1:55 PM
  • Thanks very much

    It had really gave me the knowledge what i was searching for...

    please can u tell me how to add a list of data into the list from dataset, which I was working for the project to list items in the data base..

    Thursday, August 1, 2013 7:44 PM