Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 0.85 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Creating an autocomplete textbox that works on both first name, and last name
  2. private void txtClientName_TextChanged(object sender, EventArgs e)
  3. {
  4.     if (txtClientName.Text.Length == 1)
  5.     {
  6.         completeCollection.Clear();
  7.         CustomerRepository repo = new CustomerRepository();
  8.         var customers = repo.FindAllCustomers().Where(u => u.Name.StartsWith(txtClientName.Text) || u.LastName.StartsWith(txtClientName.Text));
  9.  
  10.         foreach (var customer in customers)
  11.         {
  12.             completeCollection.Add(customer.Name + " " + customer.LastName);
  13.         }
  14.  
  15.         txtClientName.AutoCompleteMode = AutoCompleteMode.Suggest;
  16.         txtClientName.AutoCompleteSource = AutoCompleteSource.CustomSource;
  17.         txtClientName.AutoCompleteCustomSource = completeCollection;
  18.     }
  19. }
  20.        
  21. completeCollection.Add(customer.Lastname + ", " + customer.Name);