Monday, June 14, 2010

Search an Value in ListBox with compare to TextBox Text(Incremental Search) in C#

Here is an example to find the value in ListBox according to the text of Text box.
means when you type any character or word in textbox the matched value will be select in list box-

this 4 lines of code will remove your all headache to apply the incremental search option on listbox-

on TextBox's TextChangedEvent code like this-

private void textBox1_TextChanged(object sender, EventArgs e)
{
int index = listBox1.FindString(this.textBox1.Text);
if (0 <= index)
{
listBox1.SelectedIndex = index;
}
}

17 comments:

  1. Thanks a lot, spared me a lot of time!

    ReplyDelete
  2. This code working fine in windows form... plz i want the code for this problem in windows phone 7 c#..

    on TextBox's TextChangedEvent code like this-

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    int index = listBox1.FindString(this.textBox1.Text);
    if (0 <= index)
    {
    listBox1.SelectedIndex = index;
    }
    }

    ReplyDelete
  3. This code is working fine in windows form.. plz, i want the code in c# windows phone 7 for this same problem..


    on TextBox's TextChangedEvent code like this-

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    int index = listBox1.FindString(this.textBox1.Text);
    if (0 <= index)
    {
    listBox1.SelectedIndex = index;
    }
    }

    ReplyDelete
  4. Hi if you are working on the mobile application development platform for window 7. Then please make sure that you are binding the control listbox through observablecollecton. Apply filter on the observablecollection through linq ...as following

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    int index = observablecollection = observablecollection.where(obcoltypeobj=>obcoltypeobj.fielname.startswith(this.textBox1.Text)).ToList();
    }

    now the above will show the filtered data in the listbox... hope you are well aware of the linq to collections.

    ReplyDelete
  5. pincode lookup:-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.Xml.Linq;
    using System.Xml;



    namespace WindowsPhoneApplication1
    {
    public partial class Chennai : PhoneApplicationPage
    {
    public Chennai()
    {
    InitializeComponent();

    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
    XElement ob = XElement.Load("Chennai.xml");
    var p = from temp in ob.Descendants("Pincode")
    select new Pincode
    {
    PropArea = (string)temp.Element("Area"),
    PropCode = (int)temp.Element("Code"),
    };

    lst_area.ItemsSource = p;
    }

    private void lst_area_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    Pincode ob = (Pincode)lst_area.SelectedItem;
    tb_pincode.Text = ob.PropCode.ToString();
    }

    private void txt_area_TextChanged(object sender, TextChangedEventArgs e)
    {
    //int index = observablecollection = observablecollection.where(obcoltypeobj => obcoltypeobj.fielname.startswith(this.txt_area.Text)).ToList();

    }
    }
    }



    what code should I write.. I dont knw.. please, solve this problem and help me to complete this incremental search of textbox.. I hope u will help.. windows phone 7 c#

    ReplyDelete
  6. windows phone 7 this coding gives error.. how to resolve this.

    using System;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
    public Form1() {
    InitializeComponent();
    listBox1.Items.AddRange(new object[] { "abc", "abd", "ac", "baa", "bab", "bb" });
    textBox1.TextChanged += SearchListbox;
    }
    void SearchListbox(object sender, EventArgs e) {
    int len = textBox1.Text.Length;
    foreach (string item in listBox1.Items)
    if (len <= item.Length && item.Substring(0, len) == textBox1.Text) {
    listBox1.SelectedItem = item;
    return;
    }
    listBox1.SelectedIndex = -1;
    }
    }
    }

    ReplyDelete
  7. Hi..

    I know how to bind the listbox with xml in windows phone 7..

    similar to that how can i bind the autocompletebox with xml..

    I want full sample code for that..

    plz...

    ReplyDelete
  8. Thanks this code works fine...

    ReplyDelete
  9. This piece of code is very smart and working very fine. Thanks.
    Dickson

    ReplyDelete
  10. Hi,
    I have a page which displays a listbox of Objects.There is a search page which should display the objects only if they contain the textbox string. How to do it.

    Here is the xaml code
    For Textbox






    For Listbox











    Then in C# side
    private void searchText_TextChanged(object sender, TextChangedEventArgs e)
    {
    // How to search the entered textstring in the listbox and display the listbox objects only if the search string exist
    }

    ReplyDelete
  11. good information of search box really nice thank you..

    ReplyDelete
  12. I cant get listbox1."FindString" method

    ReplyDelete
  13. Cant find FindString method in listbox. Pl. Help.

    ReplyDelete
  14. Exactly what I needed!
    Thanks.

    ReplyDelete