<?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>Talking About Code &#187; Beautiful Code</title>
	<atom:link href="http://www.ambersolutions.com.au/dburstin/index.php/tag/beautiful-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ambersolutions.com.au/dburstin</link>
	<description>A journey of learning, trying and learning some more</description>
	<lastBuildDate>Wed, 05 Feb 2014 05:25:12 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Simple, Elegant and Creative &#8211; I love this solution</title>
		<link>http://www.ambersolutions.com.au/dburstin/index.php/2013/11/12/simple-elegant-and-creative-i-love-this-solution/</link>
		<comments>http://www.ambersolutions.com.au/dburstin/index.php/2013/11/12/simple-elegant-and-creative-i-love-this-solution/#comments</comments>
		<pubDate>Tue, 12 Nov 2013 11:25:05 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Beautiful Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[textbox.Enter]]></category>
		<category><![CDATA[textbox.GotFocus]]></category>
		<category><![CDATA[textbox.SelectAll]]></category>

		<guid isPermaLink="false">http://www.ambersolutions.com.au/dburstin/?p=11</guid>
		<description><![CDATA[I needed to have all of the text selected when a user clicked on a text box so it would be easy to overwrite the contents without having to hit Ctrl-A. The problem was that I just couldn&#8217;t get this code working: private void myTextbox_Enter&#40;object sender, EventArgs e&#41; &#123; selectedFolderPath.SelectAll&#40;&#41;; &#125; I could call .SelectAll() [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I needed to have all of the text selected when a user clicked on a text box so it would be easy to overwrite the contents without having to hit <em>Ctrl-A. </em>The problem was that<em> </em>I just couldn&#8217;t get this code working:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> myTextbox_Enter<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    selectedFolderPath<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectAll</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I could call .SelectAll() from anywhere else and it would work fine, but just not from the .Enter event.</p>
<p>It turns out that:</p>
<blockquote><p>Calling .SelectAll() during the .Enter or .GotFocus events won&#8217;t work because if the user clicked the textbox, the caret will be placed where he clicked, thus deselecting all text.</p></blockquote>
<p>Also, calling .SelectAll() during the .Click event won&#8217;t work because the user won&#8217;t be able to select any text with the mouse; the .SelectAll() call will keep overwriting the user&#8217;s text selection. )</p>
<p>So, how to find a way to call .SelectAll() <strong><em>after</em></strong><em> </em>the .Enter() event has been handled? My instinct (and many of the solutions I saw) all involved manually tracking the focus state with a boolean flag and either calling .SelectAll() at a later stage depending on the flag or manually redrawing the textbox as needed. All just looked like messy contortions, embarrassing compared to the code I try to write, and most likely flawed.</p>
<p>Then I came across <a title="this solution" href="http://stackoverflow.com/a/6857301">this solution</a>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> selectedFolderPath_Enter<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    BeginInvoke<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Action<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> selectedFolderPath<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectAll</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>It seems so blindingly obvious now.  <em>Just call it asynchronously! </em></p>
<p>Simple, elegant and creative.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ambersolutions.com.au/dburstin/index.php/2013/11/12/simple-elegant-and-creative-i-love-this-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
