<p>What would be the best way to develop a text box that remembers the last x number of entries that were put into it.  This is a standalone app written with C#.</p>

[{'answer_id': 43003, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 3, 'selected': False, 'text': '<p>This is actually fairly easy, especially in terms of showing the "AutoComplete" part of it.  In terms of remembering the last x number of entries, you are just going to have to decide on a particular event (or events) that you consider as an entry being completed and write that entry off to a list... an AutoCompleteStringCollection to be precise.</p>\n\n<p>The TextBox class has the 3 following properties that you will need:</p>\n\n<ul>\n<li>AutoCompleteCustomSource</li>\n<li>AutoCompleteMode </li>\n<li>AutoCompleteSource</li>\n</ul>\n\n<p>Set AutoCompleteMode to SuggestAppend and AutoCompleteSource to CustomSource.</p>\n\n<p>Then at runtime, every time a new entry is made, use the Add() method of AutoCompleteStringCollection to add that entry to the list (and pop off any old ones if you want).  You can actually do this operation directly on the AutoCompleteCustomSource property of the TextBox as long as you\'ve already initialized it.</p>\n\n<p>Now, every time you type in the TextBox it will suggest previous entries :)</p>\n\n<p>See this article for a more complete example: <a href="http://www.c-sharpcorner.com/UploadFile/mahesh/AutoCompletion02012006113508AM/AutoCompletion.aspx" rel="nofollow noreferrer">http://www.c-sharpcorner.com/UploadFile/mahesh/AutoCompletion02012006113508AM/AutoCompletion.aspx</a></p>\n\n<p>AutoComplete also has some built in features like FileSystem and URLs (though it only does stuff that was typed into IE...)</p>\n'}, {'answer_id': 45067, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 3, 'selected': True, 'text': '<p><a href="https://stackoverflow.com/questions/42966/google-suggestish-text-box#45014">@Ethan</a></p>\n\n<p>I forgot about the fact that you would want to save that so it wasn\'t a per session only thing :P But yes, you are completely correct.</p>\n\n<p>This is easily done, especially since it\'s just basic strings, just write out the contents of AutoCompleteCustomSource from the TextBox to a text file, on separate lines. </p>\n\n<p>I had a few minutes, so I wrote up a complete code example...I would\'ve before as I always try to show code, but didn\'t have time.  Anyway, here\'s the whole thing (minus the designer code).</p>\n\n<pre><code>namespace AutoComplete\n{\n    public partial class Main : Form\n    {\n        //so you don\'t have to address "txtMain.AutoCompleteCustomSource" every time\n        AutoCompleteStringCollection acsc;\n        public Main()\n        {\n            InitializeComponent();\n\n            //Set to use a Custom source\n            txtMain.AutoCompleteSource = AutoCompleteSource.CustomSource;\n            //Set to show drop down *and* append current suggestion to end\n            txtMain.AutoCompleteMode = AutoCompleteMode.SuggestAppend;\n            //Init string collection.\n            acsc = new AutoCompleteStringCollection();\n            //Set txtMain\'s AutoComplete Source to acsc\n            txtMain.AutoCompleteCustomSource = acsc;\n        }\n\n        private void txtMain_KeyDown(object sender, KeyEventArgs e)\n        {\n            if (e.KeyCode == Keys.Enter)\n            {\n                //Only keep 10 AutoComplete strings\n                if (acsc.Count &lt; 10)\n                {\n                    //Add to collection\n                    acsc.Add(txtMain.Text);\n                }\n                else\n                {\n                    //remove oldest\n                    acsc.RemoveAt(0); \n                    //Add to collection\n                    acsc.Add(txtMain.Text);\n                }\n            }\n        }\n\n        private void Main_FormClosed(object sender, FormClosedEventArgs e)\n        {\n            //open stream to AutoComplete save file\n            StreamWriter sw = new StreamWriter("AutoComplete.acs");\n\n            //Write AutoCompleteStringCollection to stream\n            foreach (string s in acsc)\n                sw.WriteLine(s);\n\n            //Flush to file\n            sw.Flush();\n\n            //Clean up\n            sw.Close();\n            sw.Dispose();\n        }\n\n        private void Main_Load(object sender, EventArgs e)\n        {\n            //open stream to AutoComplete save file\n            StreamReader sr = new StreamReader("AutoComplete.acs");\n\n            //initial read\n            string line = sr.ReadLine();\n            //loop until end\n            while (line != null)\n            {\n                //add to AutoCompleteStringCollection\n                acsc.Add(line);\n                //read again\n                line = sr.ReadLine();\n            }\n\n            //Clean up\n            sr.Close();\n            sr.Dispose();\n        }\n    }\n}\n</code></pre>\n\n<p>This code will work exactly as is, you just need to create the GUI with a TextBox named txtMain and hook up the KeyDown, Closed and Load events to the TextBox and Main form.</p>\n\n<p>Also note that, for this example and to make it simple, I just chose to detect the Enter key being pressed as my trigger to save the string to the collection.  There is probably more/different events that would be better, depending on your needs.</p>\n\n<p>Also, the model used for populating the collection is not very "smart."  It simply deletes the oldest string when the collection gets to the limit of 10. This is likely not ideal, but works for the example.  You would probably want some sort of rating system (especially if you really want it to be Google-ish)</p>\n\n<p>A final note, the suggestions will actually show up in the order they are in the collection.  If for some reason you want them to show up differently, just sort the list however you like.</p>\n\n<p>Hope that helps!</p>\n'}, {'answer_id': 1441659, 'author': 'Cheeso', 'author_id': 48082, 'author_profile': 'https://Stackoverflow.com/users/48082', 'pm_score': 1, 'selected': False, 'text': '<p>I store the completion list in the registry. </p>\n\n<p>The code I use is below.  It\'s reusable, in three steps:  </p>\n\n<ol>\n<li>replace the namespace and classname in this code with whatever you use.</li>\n<li>Call the FillFormFromRegistry() on the Form\'s <strong>Load</strong> event, and call SaveFormToRegistry on the <strong>Closing</strong> event.    </li>\n<li>compile this into your project. </li>\n</ol>\n\n<p>You need to decorate the assembly with two attributes: <code>[assembly: AssemblyProduct("...")]</code> and <code>[assembly: AssemblyCompany("...")]</code> .  (These attributes are normally set automatically in projects created within Visual Studio, so I don\'t count this as a step.) </p>\n\n<p>Managing state this way is totally automatic and transparent to the user. </p>\n\n<p>You can use the same pattern to store any sort of state for your WPF or WinForms app.  Like state of textboxes, checkboxes, dropdowns.  Also you can <a href="https://stackoverflow.com/questions/937298/restoring-window-size-position-with-multiple-monitors/937465#937465">store/restore the size of the window</a> - really handy - the next time the user runs the app, it opens in the same place, and with the same size, as when they closed it.  You can <a href="https://stackoverflow.com/questions/1233841/how-many-times-program-has-run-c/1233867#1233867">store the number of times an app has been run</a>. Lots of possibilities. </p>\n\n<pre><code>namespace Ionic.ExampleCode\n{\n    public partial class NameOfYourForm\n    {\n        private void SaveFormToRegistry()\n        {\n            if (AppCuKey != null)\n            {\n                // the completion list\n                var converted = _completions.ToList().ConvertAll(x =&gt; x.XmlEscapeIexcl());\n                string completionString = String.Join("¡", converted.ToArray());\n                AppCuKey.SetValue(_rvn_Completions, completionString);\n            }\n        }\n\n        private void FillFormFromRegistry()\n        {\n            if (!stateLoaded)\n            {\n                if (AppCuKey != null)\n                {\n                    // get the MRU list of .... whatever\n                    _completions = new System.Windows.Forms.AutoCompleteStringCollection();\n                    string c = (string)AppCuKey.GetValue(_rvn_Completions, "");\n                    if (!String.IsNullOrEmpty(c))\n                    {\n                        string[] items = c.Split(\'¡\');\n                        if (items != null &amp;&amp; items.Length &gt; 0)\n                        {\n                            //_completions.AddRange(items);\n                            foreach (string item in items)\n                                _completions.Add(item.XmlUnescapeIexcl());\n                        }\n                    }\n\n                    // Can also store/retrieve items in the registry for\n                    //   - textbox contents\n                    //   - checkbox state\n                    //   - splitter state\n                    //   - and so on\n                    //\n                    stateLoaded = true;\n                }\n            }\n        }\n\n        private Microsoft.Win32.RegistryKey AppCuKey\n        {\n            get\n            {\n                if (_appCuKey == null)\n                {\n                    _appCuKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(AppRegistryPath, true);\n                    if (_appCuKey == null)\n                        _appCuKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(AppRegistryPath);\n                }\n                return _appCuKey;\n            }\n            set { _appCuKey = null; }\n        }\n\n        private string _appRegistryPath;\n        private string AppRegistryPath\n        {\n            get\n            {\n                if (_appRegistryPath == null)\n                {\n                    // Use a registry path that depends on the assembly attributes,\n                    // that are presumed to be elsewhere. Example:\n                    // \n                    //   [assembly: AssemblyCompany("Dino Chiesa")]\n                    //   [assembly: AssemblyProduct("XPathVisualizer")]\n\n                    var a = System.Reflection.Assembly.GetExecutingAssembly();\n                    object[] attr = a.GetCustomAttributes(typeof(System.Reflection.AssemblyProductAttribute), true);\n                    var p = attr[0] as System.Reflection.AssemblyProductAttribute;\n                    attr = a.GetCustomAttributes(typeof(System.Reflection.AssemblyCompanyAttribute), true);\n                    var c = attr[0] as System.Reflection.AssemblyCompanyAttribute;\n\n                    _appRegistryPath = String.Format("Software\\\\{0}\\\\{1}",\n                                                     p.Product, c.Company);\n                }\n                return _appRegistryPath;\n            }\n        }\n\n        private Microsoft.Win32.RegistryKey _appCuKey;\n        private string _rvn_Completions = "Completions";\n        private readonly int _MaxMruListSize = 14;\n        private System.Windows.Forms.AutoCompleteStringCollection _completions;\n        private bool stateLoaded;\n    }\n\n    public static class Extensions\n    {\n        public static string XmlEscapeIexcl(this String s)\n        {\n            while (s.Contains("¡"))\n            {\n                s = s.Replace("¡", "&amp;#161;");\n            }\n            return s;\n        }\n        public static string XmlUnescapeIexcl(this String s)\n        {\n            while (s.Contains("&amp;#161;"))\n            {\n                s = s.Replace("&amp;#161;", "¡");\n            }\n            return s;\n        }\n\n        public static List&lt;String&gt; ToList(this System.Windows.Forms.AutoCompleteStringCollection coll)\n        {\n            var list = new List&lt;String&gt;();\n            foreach (string  item in coll)\n            {\n                list.Add(item);\n            }\n            return list;\n        }\n    }\n}\n</code></pre>\n\n<p>Some people <a href="http://www.codinghorror.com/blog/archives/000939.html" rel="nofollow noreferrer">shy away from using the Registry for storing state</a>, but I find it\'s really easy and convenient.  If you like, You can very easily build an installer that removes all the registry keys on uninstall. </p>\n'}]
['https://Stackoverflow.com/questions/42966', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2066/']