0

I'm using an AutoCompleteCustomSource for a TextBox in Append auto-complete mode. This works as expected; however, when an auto-complete suggestion appears, I still need access to just the text that the user entered. That is, when a suggestion appears, the Text property of the TextBox will return the suggestion.

Is there a way to also retrieve just the user typed string?

Update: One of my restrictions is that I must use the Append auto-complete mode.

Pooven
  • 1,744
  • 1
  • 25
  • 44

3 Answers3

1

Have you tried setting the property to just Suggest, that way the text property of the text box will always be the user typed in string until the user selects a suggestion.

swiftgp
  • 997
  • 1
  • 9
  • 17
  • Thank you for your reply; one of my restrictions is that I need to use the `Append` auto-complete mode. I apologize for not being more specific in my question. – Pooven Aug 16 '12 at 06:48
0

you will need to do the following as example this is pasted from my windows project that i just tested in the Designer.cs file

Win Forms Example

this.txtStudGrade.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.txtStudGrade.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;

Set in the Designer DropDownList AutoCompleteSource.ListItems; Set in the Designer DropDownList AutoCompleteMode = AutoCompleteMode.Suggest;

WebForm You Could use JQuery to do it check out the Link below for a working example

AutoComplete using JQuery

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Thank you for your reply; I'm uncertain how to go about implementing what you've suggested. In particular, are you referring to a `System.Web.UI.WebControls.DropDownList`? It seems that you're suggesting that if the `DropDownList` is set to the `Append` auto-complete mode, it will somehow affect the auto-complete mode of the `TextBox`? – Pooven Aug 16 '12 at 06:51
  • you have your Question Tagged as .NET WinForms you may want to edit it and add ASP.NET that way I would have provided a different answer.. – MethodMan Aug 16 '12 at 15:08
  • I'm sorry, I didn't mean to imply I am using ASP.NET. The only `DropDownList` I could find came from `System.Web.UI.WebControls` so I was just clarifying if that's what you meant? – Pooven Aug 16 '12 at 15:16
0

There doesn't appear to be a simple mechanism to retrieve only the user entered data and after trying to solve this problem, I can understand why. What constitutes user entered data is not always clear and it seems that Microsoft has decided to not tackle that question.

I've decided to manually keep track of the user data by registering for a TextChanged event. Since each key stroke will trigger this event, by keeping track of the previous value of Text, I can determine if either an Append auto-complete or paste operation has occurred.

The code snippet below if from the TextChanged event handler of my custom TextBox class; UserText contains the user entered data (of type string).

if (string.IsNullOrEmpty(UserText) || 
    UserText.Length > Text.Length || 
    UserText.Length + 1 == Text.Length)
{
  UpdateUserText(Text);
}

UpdateUserText(string) first determines if a change has occurred, and if so, assigns the new value to UserText and issues an event. In the case of a paste operation, one could use the solution posted here; however, I opted to do the following:

MouseClick += UpdateUserText;
KeyUp += UpdateUserText;

//--------------------------------------------------------

private void UpdateUserText(object sender, EventArgs args)
{
  if (SelectionLength == 0)
  {
    UpdateUserText(Text);
  }
}

In this way, if text in the TextBox is no longer highlighted, then I assume it has become user entered text. This takes care of the paste operation (either via the keyboard or mouse) and caters for the user pressing the arrow keys in attempt to accept the auto-complete suggestion.

One of the edge cases that I decided to ignore is when the user has typed the entire word but the last character, in this case I would not be able to differentiate between the auto-complete suggestion and the user input.

I also considered using a StringBuilder to manually keep track of the user entered data but I think that requires more effort than keeping track of the already constructed string in Text.

I'm always open to a better implementation if anyone has a suggestion :)

Community
  • 1
  • 1
Pooven
  • 1,744
  • 1
  • 25
  • 44