Hot News!

Not Available

Click Like or share to support us!

May 14, 2012

C#: How to Search Text in ListBox Items from TextBox

//Write in Event TextBox1_TextChanged

private void TextBox1_TextChanged(object sender, EventArgs e)
{
  ListBox2.Items.Clear();
  ListBox1.Sorted = true;

  //search
  foreach (string element_in_listbox in ListBox1.Items){
     if (TextBox1.TextLength <= element_in_listbox.Length) { 
       if ( TextBox1.Text== element_in_listbox.Substring(0, TextBox1.TextLength)){
            ListBox2.Items.Add(element_in_listbox);
        }
     }
  }

  //select first index
   if (ListBox2.Items.Count != 0) ListBox2.SelectedIndex = 0;
}

1 comment:

  1. Good Concept to build dictionary application

    ReplyDelete