0

I want to implement something like Google's search bar behavior for our application: user must be able to enter user name as free text and based on entered data system must provide a couple of suggestions on a popup bar based on already existent user names.


Here's the brief algorithm:

  • user enters some character to text edit box
  • system fires some changing event with web service call inside it that updates suggestions list data
  • Text edit must also provide an ability to enter and keep a free text to create new user, not just looking up for existent

I can't use devexpress's lookupeditds - they allow to keep only values, presented in datasource - even if new value has being processed inside ProcessNewValue by adding to datasource,

Changing event fires one more time with refreshing my datasource overwriting new unique value. Now I am looking forward Combobox control. But looks like there is no ability to enter free text alongside with displaying suggestions popup.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
johnny-b-goode
  • 3,792
  • 12
  • 45
  • 68
  • 1
    If you're open to third party libraries, the Infragistics NetAdvantage for Windows Forms has a great auto-suggest combo box that we use extensively. It does exactly what you're trying to achieve. – Jon Aug 03 '16 at 15:12
  • @Mangist thanks for your suggestion. Not sure it's possible in my project :-( – johnny-b-goode Aug 03 '16 at 15:14
  • 1
    you can use a regular Windows Forms combo then, and use the AutoCompleteMode property. https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.autocompletemode(v=vs.110).aspx as the user types, you will have to hit the database and then reload the list of Items in the ComboBox. I would use DataSource, DisplayMember, ValueMember. You can make it work, I think. – Jon Aug 03 '16 at 15:16
  • 1
    Check this [approach](http://stackoverflow.com/questions/11780558/c-sharp-winforms-combobox-dynamic-autocomplete), it could be useful to do what you need. – BogadoDiego Aug 03 '16 at 15:19
  • I have a code that works something like you suggest. The problem is that after calling for new suggests combobox automatically selects one of the values without a chance to continue editing a free text. – johnny-b-goode Aug 03 '16 at 15:22
  • I don't know about the ASP side, but the Dx Winforms has a MRU edit that, I believe does this. Maybe check the controls for an MRU edit on the web side and see if it has this functionality? – Hambone Aug 04 '16 at 03:09

1 Answers1

1

I can't use devexpress's lookup edits - they allow to keep only values, presented in datasource - even if new value has being processed inside ProcessNewValue by adding to datasource,

I believe you're wrong here because you can use the DevExpress LookUpEdit with easy:

class AutoCompleteLookUpEdit : LookUpEdit {
    List<string> suggestions = new List<string>();
    public AutoCompleteLookUpEdit() {
        Properties.DataSource = suggestions;
        Properties.ImmediatePopup = true;
    }
    protected override void ProcessFindItem(KeyPressHelper helper, char pressedKey) {
        suggestions.Clear();
        // add search suggestions here depending on helper.Text value
        suggestions.Add("google");
        suggestions.Add("devexpress");
        // ...
        base.ProcessFindItem(helper, pressedKey);
    }
}

Take a look at the How to create an editor with a dynamic autocomplete list for the detailed example.

P.S. You can use the AcceptEditorTextAsNewValue property to control whether or not lookup accepts entered text as valid value even it does not belong the underlying data source.

DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • Unfortunately, still the same result - after leaving editor it contains [Edit Value is null] instead of new value... – johnny-b-goode Aug 04 '16 at 08:35
  • The only way to keep entered value is to add them to datasource, but i dont want it to be visible alongside with actual suggestions. – johnny-b-goode Aug 04 '16 at 08:36
  • Finally got it - new values must be processed inside ProcessNewValue event. Now it works seamless and as expected! Thank you so much Dmitry! – johnny-b-goode Aug 04 '16 at 08:41