<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; textbox</title>
	<atom:link href="http://www.dijksterhuis.org/tag/textbox/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>C# Autosuggestion Textbox</title>
		<link>http://www.dijksterhuis.org/autosuggestion-textbox/</link>
		<comments>http://www.dijksterhuis.org/autosuggestion-textbox/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 21:24:56 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[textbox]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=938</guid>
		<description><![CDATA[A few days ago I needed a textbox that automatically suggests common input options to the user. In my situation, names of companies. Because of screen space constraints I am unable to use a ComboBox (which already has this functionality). 
The idea is that if the user enters one or more characters that the Textbox [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p>A few days ago I needed a textbox that automatically suggests common input options to the user. In my situation, names of companies. Because of screen space constraints I am unable to use a ComboBox (which already has this functionality). </p>
<p>The idea is that if the user enters one or more characters that the Textbox will search its list of suggestions. In this test implementation this is done through a simple <em>List<></em>. Ultimately my list will contain many thousands of items and I will need to replace the <em>List<></em> with a more efficient search algorithm. </p>
<p>In the below example code typing a single &#8220;W&#8221; will expand to &#8220;Waterland&#8221;, if you continue typing beyond the final &#8220;d&#8221; it will suggest &#8220;Waterland Investments&#8221;. Type a &#8220;T&#8221; and it will branch to &#8220;Waterland Telecommunication Systems&#8221;. </p>
<pre class="brush: c#">
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Dijksterhuis.org
{
    class AutoSuggestControl : TextBox
    {
        List&lt;string&gt; Suggestions;
        int PreviousLength; 

        // V1.0 We are using a simple sorted list for the suggestions
        public AutoSuggestControl() : base()
        {
            Suggestions = new List&lt;string&gt;();

            // We keep track of the previous length of the string
            // If the user tries to delete characters we do not interfere
            PreviousLength = 0; 

            // Very basic list, too slow to be suitable for systems with many entries
            Suggestions.Add(&quot;Waterland&quot;);
            Suggestions.Add(&quot;Waterland Investments&quot;);
            Suggestions.Add(&quot;Waterland Telecommuncation Systems&quot;);
            Suggestions.Sort();
        }

        /// &lt;summary&gt;
        /// Search through the collection of suggestions for a match
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;Input&quot;&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;

        private string FindSuggestion(string Input)
        {
            if (Input != &quot;&quot;)
            foreach (string Suggestion in Suggestions)
            {
                if (Suggestion.StartsWith(Input))
                    return Suggestion;
            }
            return null;
        }

        /// &lt;summary&gt;
        /// We only interfere after receiving the OnTextChanged event.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;e&quot;&gt;&lt;/param&gt;
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            // We don&#039;t do anything if the user is trying to shorten the sentence
            int CursorPosition = SelectionStart;
            if (Text.Length &gt; PreviousLength &amp;&amp; CursorPosition &gt;= 0)
            {
                string Suggestion = FindSuggestion(Text.Substring(0, CursorPosition));
                if (Suggestion != null)
                {
                    // Set the contents of the textbox to the suggestion
                    Text = Suggestion;
                    // Setting text puts the cursor at the beginning of the textbox, so we need to reposition it
                    Select(CursorPosition, 0);
                }
            }
            PreviousLength = Text.Length;
        }

    }
}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/autosuggestion-textbox/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
