Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Dear Experts,

I want to implement autocomplete feature in my winform for textbox just like SAP which will be based on user input instead of external file/database or hard-coded collections.


Thanks,

--- Anil Kumar
Posted

Store previously typed entries (suitably filtered, so you don't fill it with one character nonsense or partially completed entries) in a list (see later). Turn on AutoCompleteMode[^] for the text box, set AutoCompleteSource[^] to CustomSource, make an AutoCompleteStringCollection (use this as the storage location for your history) and use that as the AutoCompleteCustomSource[^].

(NB: I've not actually done this, just taking MSDN's word for it.)

If you want the history to persist between invocations of the application, you will have to store it in a file or database. If you want it to synchronise between multiple concurrent instances of the application, you will have to re-read it each time, or use inter-process communication to notify other instances when the list is changed.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Apr-11 14:16pm    
This is nearly a complete solution, my 5.
I also added my improvement based on this answer, please see my Solution.
--SA
In addition/alternative to the Solution by Box I would add the following:

1) For history storage, use System.Collections.Generic.SortedList, it will provide better performance in filtering our duplicates.

2) Do not trigger adding an item to the history by any UI "raw" events like LostFocus, etc. Trigger if by a semantic even, when you code actually uses the data from a text box.

3) For persistence between run-times, a database could be bit of overkill. Use just an XML file representing state of the application (maybe all Finite State Machine, not just history). The file should be placed in the directory found by the call System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData). For serialization, see DataContract; see System.Runtime.Serialization.DataContractSerializer, System.Runtime.Serialization.DataContractAttribute, System.Runtime.Serialization.DataMemberAttribute. This method is most non-intrusive an flexible at the same time.

—SA
 
Share this answer
 
I think BobJanova's answer is very good (follow his advices for storing an syncronizing). A little msdn reading should solve your problem (but that is true for a lot of questions here).
Anyway I created an executable example for you (just copy in a new WinForms Project and replace Program.cs with following code:)

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AutoComplete
{
    static class Program
    {
        [STAThread] // You have to set STA when using AutoCompleteCustomSource !!!!
        static void Main()
        {
            // Create a Form 
            Form form = new Form();
            // ... and a description Label 
            Label label = new Label();
            label.Text = "I will remember";
            label.Dock = DockStyle.Top;
            // Now create a TexBox  
            TextBox textbox = new TextBox();
            textbox.Dock = DockStyle.Top;
            // ... and enable AutoComplete
            textbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; // Set the AutoCompleteMode you like
            textbox.AutoCompleteSource = AutoCompleteSource.CustomSource; // we will use a custome source (the entered text)
            // create a button to simulate validate/lost focus (whatever your real strategy will be)
            Button button = new Button();
            button.Dock = DockStyle.Top;
            button.Text = "Add to history";
            // if the button is clicked the text should be added to history 
            button.Click += delegate(object sender, EventArgs e)
            {
                // add a new text to the AutoCompleteCustomSource list
                string strTextToAdd = textbox.Text;
                if (!String.IsNullOrWhiteSpace(strTextToAdd))// in reality you will want filter or validate the text
                {
                    textbox.AutoCompleteCustomSource.Add(strTextToAdd);
                }
                // lets clear the textbox after that in this example
                textbox.Text = String.Empty;
                // ...  and refocus it
                textbox.Focus();
            };
            // Add all the Controls to the Form
            form.Controls.Add(button);
            form.Controls.Add(textbox);
            form.Controls.Add(label);
            // ... and show the form
            Application.Run(form);
        }
    }
}
 
Share this answer
 
I have a suggestion which might be helpful or give some idea
Write a client side event on lose focus event on the textbox (on which you want to auto complete functionality), and store the value of text box into the cookie from java script, and use this cookie for the auto complete. Read the content of the cookie, make an array and can use JQuery autocomplete extender.
 
Share this answer
 
Comments
nit_singh 21-Apr-11 6:02am    
Sorry, this is winform, u cant use jquery
johannesnestler 21-Apr-11 10:48am    
reason for my vote of 1: This is WinForms your solution won't work for the OP. But I don't say it wont work in a web context. Don't worry, it happend to me too, now I read the posts more carefully.
nit_singh 21-Apr-11 14:46pm    
thanks for your advise...;)
HI Anil,

Windows application doesn't provide any specific location like cookies or Browser History in Web, to store previosly typed values.

For WinForms, You need to create your own history.
You can use XML Files to store the history.
And at the time of User Input, You can fetch data from that XML.

regards
Ankit
 
Share this answer
 
Comments
johannesnestler 21-Apr-11 10:48am    
good advice for the storing strategy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900