{"id":9072,"date":"2015-04-06T06:00:00","date_gmt":"2015-04-06T13:00:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=9072"},"modified":"2015-04-06T13:38:08","modified_gmt":"2015-04-06T20:38:08","slug":"adding-auto-complete-to-an-input-textbox","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2015\/04\/06\/adding-auto-complete-to-an-input-textbox\/","title":{"rendered":"Adding auto-complete to an input textbox"},"content":{"rendered":"<p><a class=\"twitter-follow-button\" href=\"https:\/\/twitter.com\/juneb_get_help\" data-lang=\"en\">Follow @juneb_get_help<\/a><\/p>\n<p>Well-designed user interfaces make it as easy as possible for the user to enter the right input in the right format. One of the most effective ways to help a user is to provide suggested input or automatically complete the input that the user begins to type.<\/p>\n<p>As a UI newbie, I thought that implementing a sophisticated feature like auto-complete was going to be very difficult, but I was surprised at how very easy this task is in PowerShell Studio. Honestly, I set aside a week to do this and it took me 10 minutes \u2013 30 minutes with testing and polishing. I couldn&#8217;t believe it.<\/p>\n<p>Remember that auto-complete doesn&#8217;t constrain or validate the values that the user types. To constrain values, use a <strong>ComboBox<\/strong> control. For information about validating form input, which is a critical topic, see our four-part series, beginning with <a href=\"http:\/\/www.sapien.com\/blog\/2011\/09\/02\/primalforms-2011-validating-the-form-part-1\/\">PrimalForms 2011: Validating the Form \u2013 Part 1<\/a>. And, for fundamentals, see the Spotlight post on the <a href=\"http:\/\/www.sapien.com\/blog\/2011\/06\/13\/primalforms-2011-spotlight-on-the-textbox-control\/\" target=\"_blank\">TextBox control<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h1>Step 1: Add TextBox<\/h1>\n<p>Add a TextBox control to your form.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb.png\" alt=\"image\" width=\"576\" height=\"293\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>When you run the PSF file, the form has no auto-completion features. The experience is like typing in Notepad.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb1.png\" alt=\"image\" width=\"442\" height=\"305\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<h1>Step 2: Enable AutoCompleteMode<\/h1>\n<p>To enable AutoCompleteMode, set the value of AutoCompleteMode property of the textbox to <strong>Suggest<\/strong>, <strong>Append<\/strong>, or <strong>SuggestAppend<\/strong>.<\/p>\n<p>You can do this in the Properties pane for the toolbox.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb2.png\" alt=\"image\" width=\"290\" height=\"245\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>Or, in your script. The script is the best choice when AutoCompleteMode is not enabled by default, or only conditionally, but if you enable it in form_Load, the effect is the same as enabling it in property pane.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image3.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb3.png\" alt=\"image\" width=\"372\" height=\"127\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h1>Step 3: Set the AutoCompleteSource<\/h1>\n<p>The <strong>AutoCompleteSource<\/strong> property determines the content that is automatically completed in the textbox. Windows Forms provides six preset sources and includes a option for custom sources. For information about the preset sources, see <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.autocompletesource(v=vs.110).aspx\" target=\"_blank\">AutoCompleteSource Enumeration<\/a>.<\/p>\n<p>The value of <strong>AutoCompleteSource<\/strong> has no effect when <strong>AutoCompleteMode<\/strong> is set to <strong>None<\/strong>, so remember to set both properties.<\/p>\n<p>You can set the <strong>AutoCompleteSource<\/strong> in the Properties pane for the textbox &#8230;<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb4.png\" alt=\"image\" width=\"289\" height=\"314\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>&#8230;or in your script.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image5.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb5.png\" alt=\"image\" width=\"590\" height=\"185\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h1>Add a Preset Auto-Complete Source<\/h1>\n<p>If you&#8217;re using a preset auto-complete source, you&#8217;re done! That&#8217;s all that&#8217;s required for auto-complete. Here&#8217;s the experience.<\/p>\n<p>&nbsp;<\/p>\n<h3>URL Preset Sources: <strong>AllUrl, HistoryList, RecentlyUsedList<\/strong><\/h3>\n<p>These preset sources auto-complete URLs in the browser recently-used URL list, its history list (cached URLs), or both (AllUrl).<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image6.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb6.png\" alt=\"image\" width=\"395\" height=\"245\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h3>File System Preset Sources: <strong>FileSystem, FileSystemDirectory<\/strong><\/h3>\n<p>The FileSystem and FileSystemDirectory sources complete file paths. FileSystemDirectory completes only directories. FileSystem completes both files and directories.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb7.png\" alt=\"image\" width=\"408\" height=\"347\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h3>AllSystemSources<\/h3>\n<p>AllSystemSources combines AllUrl and FileSystem. It completes URLs, files, and folders from all sources.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image8.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb8.png\" alt=\"image\" width=\"408\" height=\"342\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h1>Add a Custom Source<\/h1>\n<p>The preset sources are very simple, but custom sources aren&#8217;t difficult at all. Just remember that the sources must be string values.<\/p>\n<p>In this example, I want users to enter the names of Windows PowerShell commands in the input textbox.<\/p>\n<p>In the properties pane for the textbox, I set <strong>AutoCompleteMode<\/strong> to <strong>SuggestAppend<\/strong> and <strong>AutoCompleteSource<\/strong> to <strong>CustomSource<\/strong>.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb9.png\" alt=\"image\" width=\"359\" height=\"263\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>In my script, I use the <strong>Get-Command<\/strong> cmdlet to get the commands and its <strong>CommandType<\/strong> parameter to exclude aliases. (I have code to handle aliases, but I don&#8217;t want to encourage their use.). To get string values, I get the Name property of each command. Then, I save the results in the $cmdList variable.<\/p>\n<pre lang=\"PowerShell\">$cmdList = (Get-Command -CommandType 'Cmdlet', 'Function', 'Workflow').Name<\/pre>\n<p>&nbsp;<\/p>\n<p>Then, I set the <strong>AutoCompleteCustomSource<\/strong> property to the value of $cmdList. The value of <strong>AutoCompleteCustomSource<\/strong> must be a <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.stringcollection(v=vs.110).aspx\" target=\"_blank\">StringCollection<\/a>, so I use the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.stringcollection.addrange(v=vs.110).aspx\" target=\"_blank\">AddRange<\/a> method of StringCollection instances to add the values in $cmdList to the auto-complete source.<\/p>\n<pre lang=\"PowerShell\">$textboxInput.AutoCompleteCustomSource.AddRange($cmdList)<\/pre>\n<p>&nbsp;<\/p>\n<p>I only need to get the command names once and I don&#8217;t want to slow the script down by getting them more than once, so I placed these commands in the Load event of the form. (I didn&#8217;t put them in my init function, because it&#8217;s called several times.)<\/p>\n<pre lang=\"PowerShell\">$formAutoCompleteExample_Load={\r\n    init\r\n    $cmdList = (Get-Command -CommandType 'Cmdlet', 'Function', 'Workflow').Name\r\n    $textboxInput.AutoCompleteCustomSource.AddRange($cmdList)\r\n}<\/pre>\n<p>Done! Now, when I use the form, the command names are auto-completed.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image10.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb10.png\" alt=\"image\" width=\"411\" height=\"350\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h1>Add a Fixed Custom Source<\/h1>\n<p>You can also add a fixed collection as a custom source. Typically, if you want to present a fixed collection, you use a ComboBox control, but just to be complete, I&#8217;ll show you how it&#8217;s done with a textbox.<\/p>\n<p>In this example, we&#8217;ll use the names of planets as a source. For scientific accuracy, I&#8217;ll omit Pluto, with nostalgic regret.<\/p>\n<p>In the properties pane for the textbox, I set <strong>AutoCompleteMode<\/strong> to <strong>SuggestAppend<\/strong> and <strong>AutoCompleteSource<\/strong> to <strong>CustomSource<\/strong>.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image11.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb11.png\" alt=\"image\" width=\"347\" height=\"223\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>Then, I click the ellipsis beside <strong>AutoCompleteCustomSource<\/strong>. In the <strong>StringCollection Editor <\/strong>box, I type the names of the planets.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image12.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb12.png\" alt=\"image\" width=\"308\" height=\"253\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>You can also populate the AutoCompleteCustomSource in your script. The effect is the same.<\/p>\n<pre lang=\"PowerShell\">$formAutoCompleteExample_Load={\r\n    $textboxInput.AutoCompleteCustomSource.AddRange('Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune')\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image13.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb13.png\" alt=\"image\" width=\"375\" height=\"260\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<h1>AutoComplete Modes<\/h1>\n<p>To finish, let&#8217;s look at the AutoCompleteMode property values: Suggest, Append, and SuggestAppend, which combines the first two.<\/p>\n<p>You can <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx\" target=\"_blank\">read the definitions<\/a> of the AutoCompleteMode property values, but you need to touch it to see the effect.<\/p>\n<p>&nbsp;<\/p>\n<p>Suggest<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image14.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb14.png\" alt=\"image\" width=\"401\" height=\"277\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Append<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image15.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb15.png\" alt=\"image\" width=\"404\" height=\"279\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>SuggestAppend<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image16.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/04\/image_thumb16.png\" alt=\"image\" width=\"398\" height=\"275\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>For me, the best part about implementing auto-complete in my GUI app was the small investment of time and huge payback.<\/p>\n<p><em>June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at <\/em><a href=\"mailto:juneb@sapien.com\"><em>juneb@sapien.com<\/em><\/a><em> or follow her on Twitter at <\/em><a href=\"https:\/\/twitter.com\/juneb_get_help\"><em>@juneb_get_help<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Follow @juneb_get_help Well-designed user interfaces make it as easy as possible for the user to enter the right input in the right format. One of the most effective ways to help a user is to provide suggested input or automatically complete the input that the user begins to type. As a UI newbie, I thought [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[946,703],"tags":[135,934,1016,930],"class_list":["post-9072","post","type-post","status-publish","format-standard","hentry","category-guiprogramming","category-powershell-studio","tag-beginner","tag-juneb","tag-powershell-studio","tag-ui"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/9072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=9072"}],"version-history":[{"count":13,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/9072\/revisions"}],"predecessor-version":[{"id":9115,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/9072\/revisions\/9115"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=9072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=9072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=9072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}