Nitin
Nitin

Reputation: 314

TextBox AutoComplete not showing suggestions properly

I've a TextBox in C# Windows Form application. I'm using suggestions as user types in the TextBox using TextChanged() event. I'm creating AutoCompleteResoure dynamically everytime when TextChanged() is called. The problem is : let's say I type "j" in TextBox and it shows 3 results for it and Second suggestion is where I want to go. When I press Down Arrow key on keyboard to go to second suggestion all the suggestions get removed and text in TextBox is changed to the first suggestion. This means I can't go below first suggestion using keyboard because as I press Down key, text in the TextBox is replaced by suggestion and TextChanged() event is called and for that there is no other suggestion. How can I go through all the suggestions using keyboard? I've set AutoCompeleteMode to Suggest. Here is the code.

private void searchTb_TextChanged(object sender, EventArgs e){
     AutoCompleteStringCollection resource = new AutoCompleteStringCollection();
     string searchTerm = (sender as TextBox).Text;    
     searchTb.AutoCompleteCustomSource = getResource(searchTerm);
}

I tried to consume Key.Down event when user presses down arrow key but that didn't work.

Upvotes: 1

Views: 2208

Answers (2)

Rai Micheal
Rai Micheal

Reputation: 417

Dont Use TextChanged event. It shouldn't be inside the TextChanged event. You should assign it only once...Instead Use this Code in Form_load

private void Form1_Load(object sender, EventArgs e)
{
     AutoCompleteStringCollection resource = new AutoCompleteStringCollection();
     string searchTerm = (sender as TextBox).Text;    
     searchTb.AutoCompleteCustomSource = getResource(searchTerm);
}

Try tis..Hope it will YouHelp

Upvotes: 0

Mr_Green
Mr_Green

Reputation: 41832

This should work..

//In KeyUp or Keypress or KeyDown Event
if keypress == DownArrow or UpArrow
  //Unhook TextChanged event
else
  //hook TextChanged event

Once have a look at this link too. [Somehow related]

Upvotes: 1

Related Questions