<?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>Sunstorm Blog &#187; C#</title>
	<atom:link href="http://www.sunstormlabs.net/blog/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sunstormlabs.net/blog</link>
	<description>We make the storm in your head</description>
	<lastBuildDate>Thu, 17 Dec 2009 17:30:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exceptions in BackgroundWorker</title>
		<link>http://www.sunstormlabs.net/blog/2009/12/17/exceptions-in-backgroundworker/</link>
		<comments>http://www.sunstormlabs.net/blog/2009/12/17/exceptions-in-backgroundworker/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 17:28:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/?p=135</guid>
		<description><![CDATA[To save 15 minutes of confusion.
If an exception is thrown in a BackgroundWorker&#8217;s DoWork handler, the backgroundworker will catch it and place the exception in RunWorkerCompleted&#8217;s e.Error.
If it seems like the Exception&#8217;s still being thrown with no apparent way to catch it, it&#8217;s because if e.Error is not null. When that&#8217;s the case, e.Result will throw [...]]]></description>
			<content:encoded><![CDATA[<p>To save 15 minutes of confusion.</p>
<p>If an exception is thrown in a BackgroundWorker&#8217;s DoWork handler, the backgroundworker <em>will</em> catch it and place the exception in RunWorkerCompleted&#8217;s e.Error.</p>
<p>If it seems like the Exception&#8217;s still being thrown with no apparent way to catch it, it&#8217;s because if e.Error is not null. When that&#8217;s the case, e.Result will throw that error (wrapped in a TargetInvocationException) when accessed. Do not access e.Result without checking for e.Error first.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> worker_RunWorkerCompleted<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, RunWorkerCompletedEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  EndCreate<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Error</span>, e.<span style="color: #0000FF;">Result</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// &lt;--- THIS WILL CRASH IF e.Error IS NOT NULL</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> worker_RunWorkerCompleted<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, RunWorkerCompletedEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// THIS WILL NOT.</span>
  <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Error</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    EndCreate<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Error</span>, e.<span style="color: #0000FF;">Result</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
  <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
    EndCreate<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Error</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// accessing e.Result throws the exception</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2009/12/17/exceptions-in-backgroundworker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Initializing DataRows</title>
		<link>http://www.sunstormlabs.net/blog/2009/02/09/initializing-datarows/</link>
		<comments>http://www.sunstormlabs.net/blog/2009/02/09/initializing-datarows/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 17:51:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/?p=108</guid>
		<description><![CDATA[How many ways are there to initialize a new DataRow with new values?


Set the DefaultValue property on the respective DataColumns.
This works fine for static values. However, you&#8217;re likely using the DataSet designer in Visual Studio, and it will hose itself if you try to set a GUID because it can&#8217;t tell them apart from a [...]]]></description>
			<content:encoded><![CDATA[<p>How many ways are there to initialize a new DataRow with new values?</p>
<ol>
<li>
<h3>Set the DefaultValue property on the respective DataColumns.</h3>
<p>This works fine for static values. However, you&#8217;re likely using the DataSet designer in Visual Studio, and it will hose itself if you try to set a GUID because it can&#8217;t tell them apart from a string. Plus, initializing with dynamic values like <code>DateTime.Now</code> won&#8217;t work because you&#8217;ll end up with the date of the creation of the column rather than the date of the row.</li>
<li>
<h3>Initialize after creating it, but before adding it to the DataTable.</h3>
<p>If you&#8217;re creating the row manually, you can just set any initial values before adding it to the table. None of the values, indices, etc. are validated until that happens. Strong typed datasets are even nice enough to generate methods that take and return rows of your type.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">MyDataSet ds <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyDataSet<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
MyTableRow row <span style="color: #008000;">=</span> ds.<span style="color: #0000FF;">MyTable</span>.<span style="color: #0000FF;">NewMyTableRow</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
ds.<span style="color: #0000FF;">MyTable</span>.<span style="color: #0000FF;">AddMyTableRow</span><span style="color: #000000;">&#40;</span>row<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</li>
<li>
<h3>Override the OnTableNewRow of the DataTable in a derived class. <em>This doesn&#8217;t actually work.</em></h3>
<p>Like the derived classes created by the strong typed table generator. You can get to this method if you use the DataSet&#8217;s helpful partial class definition:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Note: does not actually work</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyDataSet <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyTable <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnTableNewRow<span style="color: #000000;">&#40;</span>DataTableNewRowEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            MyDataSet.<span style="color: #0000FF;">MyTableRow</span> row <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>MyDataSet.<span style="color: #0000FF;">MyTableRow</span><span style="color: #000000;">&#41;</span>e.<span style="color: #0000FF;">Row</span><span style="color: #008000;">;</span>
            row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnTableNewRow</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It doesn&#8217;t work because <strong>this method is never called <a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98383">unless you also attach a handler to the TableNewRow event</a></strong>, defeating the whole point of creating a virtual method. GG, Microsoft. This set me back a whole afternoon trying to figure out why some of my tables ran the method and others didn&#8217;t.</p>
<p>You can overcome this if you&#8230;</li>
</ol>
<p><span id="more-108"></span></p>
<ol start="3">
<li>
<h3>Handle the TableNewRow event of the DataTable.</h3>
<p>Good if you want to initialize with external values of some kind, but you can also attach the handler from inside the typed DataTable if you override the <code>EndInit()</code> method, which being part of <code>ISupportInitialize</code>, Visual Studio calls every time the table is initialized in a designer. If you use the dataset outside of the designer, well, you&#8217;re screwed.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyDataSet <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyTable <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> EndInit<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TableNewRow</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, TableNewRowEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// use 'delegate' if not C# 3.0</span>
                MyDataSet.<span style="color: #0000FF;">MyTableRow</span> row <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>MyDataSet.<span style="color: #0000FF;">MyTableRow</span><span style="color: #000000;">&#41;</span>e.<span style="color: #0000FF;">Row</span><span style="color: #008000;">;</span>
                row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnTableNewRow</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</li>
<li>
<h3>Use a BindingSource and handle the AddingNew event.</h3>
<p>You bound your dataset to a bunch of controls, and now you want to create new rows with data that&#8217;s only available in your presentation tier. BindingSources let you control the row creation process completely if you handle the AddingNew event. The downside is that you have to create the whole thing manually, and to do that you have to know how BindingSources work.</p>
<p>Another caveat is that the BindingSource&#8217;s Position won&#8217;t move to the newly created item, like it would otherwise. You&#8217;ll have to do that yourself.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">myBindingSource.<span style="color: #0000FF;">AddingNew</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, AddingNewEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #000000;">&#123;</span>
    DataRowView view <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DataView<span style="color: #000000;">&#41;</span>myBindingSource.<span style="color: #0000FF;">List</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddNew</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    MyDataSet.<span style="color: #0000FF;">MyTableRow</span> row <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>MyDataSet.<span style="color: #0000FF;">MyTableRow</span><span style="color: #000000;">&#41;</span>view.<span style="color: #0000FF;">Row</span><span style="color: #008000;">;</span>
    row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
    e.<span style="color: #0000FF;">NewObject</span> <span style="color: #008000;">=</span> view<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There&#8217;s a better way in which you don&#8217;t have to create the DataRowView&#8230;</li>
<li>
<h3>Use a BindingSource and handle the ListChanged event.</h3>
<p>This is the easiest way to initialize rows directly on the BindingSource. Unlike the AddingNew method, the BindingSource creates the row, and sets the Position to the new item.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">myBindingSource.<span style="color: #0000FF;">ListChanged</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, ListChangedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">ListChangedType</span> <span style="color: #008000;">==</span> ListChangedType.<span style="color: #0000FF;">ItemAdded</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        DataRowView view <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>DataRowView<span style="color: #000000;">&#41;</span>myBindingSource<span style="color: #000000;">&#91;</span>e.<span style="color: #0000FF;">NewIndex</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>view.<span style="color: #0000FF;">IsNew</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// Check if the row is in its detached state</span>
            MyDataSet.<span style="color: #0000FF;">MyTableRow</span> row <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>MyDataSet.<span style="color: #0000FF;">MyTableRow</span><span style="color: #000000;">&#41;</span>view.<span style="color: #0000FF;">Row</span>
            row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
            bs.<span style="color: #0000FF;">ResetItem</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">NewIndex</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// this is needed to get the controls to show the new values that we just set.</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The ListChanged event is called twice. First when the row is created in its detached state, and then again when it&#8217;s added to the Rows collection of the DataTable. We only need to handle it the first time. The call to ResetItem will make sure that all the bound controls read the new values.</li>
<li>
<h3>Use control-specific events.</h3>
<p>Some controls have their own events to initialize new rows. I don&#8217;t recommend using this approach, because it&#8217;s too control-specific. It&#8217;s much more interoperable to initialize at the BindingSource. You never know how that AddNew might be called.</li>
</ol>
<p>Depending on what kind of data you&#8217;re initializing, and in which tier, you might need to mix and match. #4 for the data tier and #6 for the forms do the job for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2009/02/09/initializing-datarows/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# events are actually accessors</title>
		<link>http://www.sunstormlabs.net/blog/2008/10/15/c-events-are-actually-accessors/</link>
		<comments>http://www.sunstormlabs.net/blog/2008/10/15/c-events-are-actually-accessors/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 21:00:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[delegates]]></category>
		<category><![CDATA[events]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/?p=32</guid>
		<description><![CDATA[An interesting feature of C# I discovered recently is that events are actually accessors into Multicast Delegates, much like properties are accessors into other fields.

private string myData;
public string MyData // A property
&#123;
  get &#123; return myData; &#125; // called when the property is read
  set &#123; myData = value; &#125; // called when [...]]]></description>
			<content:encoded><![CDATA[<p>An interesting feature of C# I discovered recently is that events are actually accessors into Multicast Delegates, much like properties are accessors into other fields.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> myData<span style="color: #008000;">;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> MyData <span style="color: #008080; font-style: italic;">// A property</span>
<span style="color: #000000;">&#123;</span>
  get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> myData<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008080; font-style: italic;">// called when the property is read</span>
  set <span style="color: #000000;">&#123;</span> myData <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008080; font-style: italic;">// called when the property is written to</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> EventHandler myDataChanged<span style="color: #008000;">;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> EventHandler MyDataChanged <span style="color: #008080; font-style: italic;">// An event</span>
<span style="color: #000000;">&#123;</span>
  add <span style="color: #000000;">&#123;</span> myDataChanged <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>EventHandler<span style="color: #000000;">&#41;</span><span style="color: #FF0000;">Delegate</span>.<span style="color: #0000FF;">Combine</span><span style="color: #000000;">&#40;</span> myDataChanged, value <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008080; font-style: italic;">// called when a handler is attached</span>
  remove <span style="color: #000000;">&#123;</span> myDataChanged <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>EventHandler<span style="color: #000000;">&#41;</span><span style="color: #FF0000;">Delegate</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span> myDataChanged, value <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008080; font-style: italic;">// called when a handler is detached</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><span id="more-32"></span></p>
<p>A vanilla event declaration, without the accessors, does this internally. It also acquires a lock every time it accesses the EventHandler to be thread safe, which we&#8217;re not doing here, and you should, if you need the safety.</p>
<p>A great application of this is to simulate &#8220;bubble&#8221; events in WinForms like in ASP.NET. Take for example a form with a user control with a button in it. If I wanted to receive the button&#8217;s click event on the form, I would have to either expose the button, or handle its event in the control, and raise a different one on the control:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyControl <span style="color: #008000;">:</span> UserControl
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> EventHandler MyButtonClick<span style="color: #008000;">;</span>
  Button btn<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> MyControl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    btn <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    btn.<span style="color: #0000FF;">Click</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #000000;">&#40;</span> btn_Click <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> myBtn_Click<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">object</span> sender, EventArgs e <span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span> MyButtonClick <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #000000;">&#41;</span>
      MyButtonClick<span style="color: #000000;">&#40;</span> sender, e <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyForm <span style="color: #008000;">:</span> Form
<span style="color: #000000;">&#123;</span>
  MyControl ctl<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> Form<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    ctl <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyControl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    ctl.<span style="color: #0000FF;">MyButtonClick</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #000000;">&#40;</span> ctl_MyButtonClick <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ctl_MyButtonClick<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">object</span> sender, EventArgs e <span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Button clicked on the control!</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This can get rather bothersome and ugly if you have many buttons. Using an event accessor you can take the handler and attach it to anything you like, so you can just pass-through the handler directly to the button(s).</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyControl <span style="color: #008000;">:</span> UserControl
<span style="color: #000000;">&#123;</span>
  Button btn<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> MyControl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    btn <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> EventHandler MyButtonClick
  <span style="color: #000000;">&#123;</span>
    add <span style="color: #000000;">&#123;</span> btn.<span style="color: #0000FF;">Click</span> <span style="color: #008000;">+=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    remove <span style="color: #000000;">&#123;</span> btn.<span style="color: #0000FF;">Click</span> <span style="color: #008000;">-=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There&#8217;s one caveat to this technique, and that&#8217;s when you handle the event somewhere upstream, the &#8220;sender&#8221; would be the button, not the MyControl. This might break encapsulation, and if that&#8217;s important to you, the handle/re-raise method is the way to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2008/10/15/c-events-are-actually-accessors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get a (non-visual) ComponentModel.Component&#8217;s parent Form or Control</title>
		<link>http://www.sunstormlabs.net/blog/2008/03/07/get-component-parent-form-control/</link>
		<comments>http://www.sunstormlabs.net/blog/2008/03/07/get-component-parent-form-control/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 18:40:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[winforms]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/?p=21</guid>
		<description><![CDATA[I recently ran into the need to get the reference of the parent form of a Component that I was creating. The problem is that Components, unlike Controls, don&#8217;t have a Parent property, nor any other simple way of getting it. I&#8217;ve browsed around for solutions, but I could only find vague references to what [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into the need to get the reference of the parent form of a Component that I was creating. The problem is that Components, unlike Controls, don&#8217;t have a Parent property, nor any other simple way of getting it. I&#8217;ve browsed around for solutions, but I could only find vague references to what needed to be done. Most solutions simply suggested that I simply create a public property, that I then set manually in the parent Control. <a href="http://www.codeproject.com/KB/smart/VBNET__Windows_Forms_.aspx">One article came close</a>, but it didn&#8217;t work. It did however point me towards looking into the ErrorProvider component, which does exactly this.</p>
<p>I found the solution by <a href="http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx">looking through it&#8217;s source code</a>. We still expose a public property, however, instead of setting it manually, we can simply instruct Visual Studio to set the property for us, when it generates the designer code for any component we put it into.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> ContainerControl parentControl<span style="color: #008000;">;</span>
<span style="color: #0600FF;">public</span> ContainerControl ParentControl
<span style="color: #000000;">&#123;</span>
    get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> parentControl<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    set <span style="color: #000000;">&#123;</span> parentControl <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> ISite Site
<span style="color: #000000;">&#123;</span>
    set
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Site</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            return<span style="color: #008000;">;</span>
&nbsp;
        IDesignerHost host <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IDesignerHost<span style="color: #000000;">&#41;</span>value.<span style="color: #0000FF;">GetService</span><span style="color: #000000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span> IDesignerHost <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>host <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            IComponent baseComp <span style="color: #008000;">=</span> host.<span style="color: #0000FF;">RootComponent</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>baseComp <span style="color: #008000;">is</span> ContainerControl<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ContainerControl</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ContainerControl<span style="color: #000000;">&#41;</span>baseComp<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><span id="more-21"></span></p>
<p>We&#8217;re overriding the <code>Site</code> property, so that we can intercept it&#8217;s <code>ISite</code>, which will be set when the Component is added to its container. We can then request its <code><a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.design.idesignerhost(VS.71).aspx">IDesignerHost</a></code> service, which is an interface used to communicate during design time with an aware IDE, such as Visual Studio. We then save it in our public property, which Visual Studio will then automatically add to the generated code of any Control that you drop the component in:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// </span>
<span style="color: #008080; font-style: italic;">/// Required method for Designer support - do not modify</span>
<span style="color: #008080; font-style: italic;">/// the contents of this method with the code editor.</span>
<span style="color: #008080; font-style: italic;">/// </span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> InitializeComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">components</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">ComponentModel</span></span>.<span style="color: #0000FF;">Container</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">decorator</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Sunstorm.<span style="color: #0000FF;">Tests</span>.<span style="color: #0000FF;">FormDecorator</span><span style="color: #000000;">&#40;</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">components</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SuspendLayout</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//</span>
    <span style="color: #008080; font-style: italic;">// decorator</span>
    <span style="color: #008080; font-style: italic;">//</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">decorator</span>.<span style="color: #0000FF;">ContainerControl</span> <span style="color: #008000;">=</span> this<span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//</span>
    <span style="color: #008080; font-style: italic;">// TestForm</span>
    <span style="color: #008080; font-style: italic;">//</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;TestForm&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Size</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Drawing</span></span>.<span style="color: #0000FF;">Size</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">510</span>, <span style="color: #FF0000;">339</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ResumeLayout</span><span style="color: #000000;">&#40;</span> <span style="color: #0600FF;">false</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>From there, you can manipulate the parent in any way you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2008/03/07/get-component-parent-form-control/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
