<?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>theMike.com</title>
	<atom:link href="http://www.themike.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.themike.com</link>
	<description></description>
	<lastBuildDate>Wed, 21 Mar 2012 04:59:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>More than a &#8220;Hello World&#8221; in VBA</title>
		<link>http://www.themike.com/more-than-a-hello-world-in-vba</link>
		<comments>http://www.themike.com/more-than-a-hello-world-in-vba#comments</comments>
		<pubDate>Mon, 24 May 2010 18:31:42 +0000</pubDate>
		<dc:creator>mike.hanson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[vba]]></category>

		<guid isPermaLink="false">http://www.themike.com/?p=55</guid>
		<description><![CDATA[When I started at Bates Group, LLC one of my first assignments was to debug an Excel VBA macro.  Knowing nothing about the language I fought my way through the bug and fixed the macro.  After that I quickly decided &#8230; <a href="http://www.themike.com/more-than-a-hello-world-in-vba">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I started at Bates Group, LLC one of my first assignments was to debug an Excel VBA macro.  Knowing nothing about the language I fought my way through the bug and fixed the macro.  After that I quickly decided to learn more about the language.   Since &#8220;Hello World&#8221; only gets me so far, I decided to do something a little tougher.  What better way to do that than to think back to my college assignments?</p>
<p>Back then one of the assignments I had was to write a random walk function.  Imagine standing next to a lamppost on the street.  From the lamppost you can take a step in one of four directions; North, South, East, or West.  You take a step in a random direction and then look at where you are.  From your new location you take another step in a random direction and you keep taking these random steps for a while.  Finally you stop and look up, how far away from the lamppost are you?</p>
<p>The following function does that only much faster than you or I could.  It takes 20,000 steps total and colors them along the way.  Every 2,000 steps it will change colors leaving a cool trail as it goes along.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Sub</span> TakeAWalk()
    Workbooks(<span style="color: #800000;">&quot;themikecom_vba_helloworld.xls&quot;</span>).Activate
    ActiveWorkbook.Worksheets(<span style="color: #800000;">&quot;Board&quot;</span>).<span style="color: #8D38C9; font-weight: bold;">Select</span>
&nbsp;
    <span style="color: #008000;">' Where on the sheet should we start?
</span>    ActiveSheet.Range(<span style="color: #800000;">&quot;EE150&quot;</span>).<span style="color: #8D38C9; font-weight: bold;">Select</span>
&nbsp;
    <span style="color: #008000;">' How many steps per turn should we take?
</span>    STEPS_PER_TURN = 2000
&nbsp;
    <span style="color: #008000;">' How many turns should we take?
</span>    TURNS = 10
&nbsp;
    <span style="color: #8D38C9; font-weight: bold;">For</span> j = 3 <span style="color: #8D38C9; font-weight: bold;">To</span> (TURNS + 3)
    <span style="color: #8D38C9; font-weight: bold;">For</span> i = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> STEPS_PER_TURN
&nbsp;
        <span style="color: #008000;">' Should we step east or west?
</span>        randomX = Int(4 * Rnd)
&nbsp;
        <span style="color: #008000;">' Should we step north or south?
</span>        randomY = Int(4 * Rnd)
&nbsp;
        <span style="color: #008000;">' Move west-east
</span>        <span style="color: #8D38C9; font-weight: bold;">Select</span> <span style="color: #8D38C9; font-weight: bold;">Case</span> randomX
            <span style="color: #8D38C9; font-weight: bold;">Case</span> 2 <span style="color: #008000;">' Move one step west
</span>                <span style="color: #8D38C9; font-weight: bold;">If</span> ActiveCell.Column &amp;lt; 1 <span style="color: #8D38C9; font-weight: bold;">Then</span> <span style="color: #008000;">' Do not overstep the west border
</span>                    ActiveCell.Offset(0, -1).<span style="color: #8D38C9; font-weight: bold;">Select</span>
                <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span>
&nbsp;
            <span style="color: #008000;">' Case 1 - Stay in the same spot
</span>
            <span style="color: #8D38C9; font-weight: bold;">Case</span> 0 <span style="color: #008000;">' Move one step east
</span>                <span style="color: #8D38C9; font-weight: bold;">If</span> ActiveCell.Column &amp;lt;= 255 <span style="color: #8D38C9; font-weight: bold;">Then</span> <span style="color: #008000;">' Do not overstep the east border                     ActiveCell.Offset(0, 1).Select                 End If         End Select         ' Move north-south         Select Case randomY             Case 2 ' Move one step north                 If ActiveCell.Row &amp;gt; 1 Then ' Do not overstep the north border
</span>                    ActiveCell.Offset(-1, 0).<span style="color: #8D38C9; font-weight: bold;">Select</span>
                <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span>
&nbsp;
            <span style="color: #008000;">' Case 1 - Stay in the same spot
</span>
            <span style="color: #8D38C9; font-weight: bold;">Case</span> 0 <span style="color: #008000;">' Move one step south
</span>                <span style="color: #8D38C9; font-weight: bold;">If</span> ActiveCell.Row &amp;lt;= 65535 <span style="color: #8D38C9; font-weight: bold;">Then</span> <span style="color: #008000;">' Do not overstep the south border
</span>                    ActiveCell.Offset(1, 0).<span style="color: #8D38C9; font-weight: bold;">Select</span>
                <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span>
        <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">Select</span>
&nbsp;
        <span style="color: #008000;">' Leave a trail
</span>        ActiveCell.Interior.ColorIndex = j
&nbsp;
    <span style="color: #8D38C9; font-weight: bold;">Next</span> i
    <span style="color: #8D38C9; font-weight: bold;">Next</span> j
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></pre></div></div>

<p><img class="alignleft" title="Macro Flower Shot" src="http://www.themike.com/wp-content/uploads/2010/05/macro.jpg" alt="Macro Flower Shot" width="200" height="200" />With that done I wanted to add another function to learn how to create a menu.  I came up with the square flower.  This function will generate a square of random size with each section of the square filled with a different color.  This function taught me some tricks about looping in VBA, some ways are a lot faster than others.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Sub</span> Flower()
    Workbooks(<span style="color: #800000;">&quot;themikecom_vba_helloworld.xls&quot;</span>).Activate
    ActiveWorkbook.Worksheets(<span style="color: #800000;">&quot;Board&quot;</span>).<span style="color: #8D38C9; font-weight: bold;">Select</span>
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">Dim</span> start <span style="color: #151B8D; font-weight: bold;">As</span> Range
    <span style="color: #151B8D; font-weight: bold;">Dim</span> Length <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
    <span style="color: #151B8D; font-weight: bold;">Dim</span> Width <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
    <span style="color: #151B8D; font-weight: bold;">Dim</span> Color <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
&nbsp;
    <span style="color: #008000;">' The starting point of the flower
</span>    <span style="color: #151B8D; font-weight: bold;">Set</span> start = ActiveCell
&nbsp;
    <span style="color: #008000;">' The maximum size of the flower
</span>    size = Int(57 * Rnd)
&nbsp;
    <span style="color: #008000;">' Ignore boundry errors for now
</span>    <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span>
&nbsp;
    <span style="color: #8D38C9; font-weight: bold;">For</span> z = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> size
        <span style="color: #008000;">' Generate a random color for this row
</span>        Color = Int((56 - 1 + 1) * Rnd + 1)
&nbsp;
        <span style="color: #008000;">' Left side
</span>        Range(start.Offset(0, 0), start.Offset(Length, 0)).Interior.ColorIndex = Color
&nbsp;
        <span style="color: #008000;">' Bottom side
</span>        Range(start.Offset(Length, 0), start.Offset(Length, Length)).Interior.ColorIndex = Color
&nbsp;
        <span style="color: #008000;">' Upper side
</span>        Range(start.Offset(0, 0), start.Offset(0, Width)).Interior.ColorIndex = Color
&nbsp;
        <span style="color: #008000;">' Right side
</span>        Range(start.Offset(0, Width), start.Offset(Width, Width)).Interior.ColorIndex = Color
&nbsp;
        <span style="color: #151B8D; font-weight: bold;">Set</span> start = start.Offset(-1, -1)
        Length = Length + 2
        Width = Width + 2
    <span style="color: #8D38C9; font-weight: bold;">Next</span> z
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></pre></div></div>

<p>So what did I learn after all of this?  Mostly that I have a strong dislike for VBA.  It works well for small projects with small data sets.  However those small projects quickly expand into real programs which need to be maintained.  You are better off doing it right the first time instead of maintaining a large clunky macro.<img class="size-full  wp-image-56 alignright" title="Excel Random Walk" src="http://www.themike.com/wp-content/uploads/2010/05/excel.jpg" alt="Excel Random Walk" width="300" height="212" /></p>
<p><a href="http://www.themike.com/wp-content/uploads/2011/04/themikecom_vba_helloworld.xls">Download the complete macro here.</a> You will need to enable macros in your security settings to get them to work.  Once enabled, select &#8220;Random Walk&#8221; from the &#8220;theMike.com &#8211; Hello World VBA&#8221; menu.  This will start a random walk which will finish after a couple of seconds.  The &#8220;Square Flower&#8221; menu item will create a square flower under your cursor.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.themike.com/more-than-a-hello-world-in-vba/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating an Audiobook for the iPod</title>
		<link>http://www.themike.com/create-an-audiobook</link>
		<comments>http://www.themike.com/create-an-audiobook#comments</comments>
		<pubDate>Mon, 17 May 2010 05:29:05 +0000</pubDate>
		<dc:creator>mike.hanson</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[audiobook]]></category>

		<guid isPermaLink="false">http://www.themike.com/?p=22</guid>
		<description><![CDATA[iPods treat audiobooks differently than music files. For one you can leave an audiobook listen to something else, and later pick up where you left off. An iPod won&#8217;t do this when you are listening to music file. Furthermore an &#8230; <a href="http://www.themike.com/create-an-audiobook">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-43 alignright" title="An Audiobook on the iPod" src="http://www.themike.com/wp-content/uploads/2010/05/cvipod.jpg" alt="iPod" width="200" height="150" />iPods treat audiobooks differently than music files.  For one you can leave an audiobook listen to something else, and later pick up where you left off.  An iPod won&#8217;t do this when you are listening to music file.  Furthermore an audiobook can have chapter markings making it easier to find a chapter in a longer book.</p>
<p>iTunes makes it easy to create a music file for the iPod.  You can insert a CD and iTunes will import it at the push of a button.  Creating a proper audiobook however, is difficult with iTunes.  You could import a whole CD as a single file but what if your audiobook covers multiple CDs?</p>
<p>I&#8217;ve had this problem for a while now.  My initial solution involved importing each track and numbering them in a way that they would line up in a playlist.  This works, but it&#8217;s annoying.  I can play the book but if I stop I have to remember which chapter I left off with.  If I don&#8217;t go back to the book for a couple of days I forget where I am and have to start over.  A couple of weeks ago I finally had enough and went searching for a better solution.</p>
<p>The solution to my problem came from a program called <em>Chapter and Verse</em> by <a href="http://lodensoftware.com/chapter-and-verse/">lodensoftware.com</a>.  The program will take a list of AAC (.m4a) files and convert them into one single audiobook (.m4b).  This makes creating an audiobook from a CD very easy.  Simply import the CD using iTunes in the AAC format and then use <em>Chapter and Verse</em> to make the conversion.</p>
<p><img class="size-medium wp-image-44  alignleft" title="Chapter and Verse  - Main  screen" src="http://www.themike.com/wp-content/uploads/2010/05/cvmain-e1274246318237.jpg" alt="Chapter and Verse - Main screen" width="300" height="260" /></p>
<p style="text-align: left;"><em>Chapter and Verse</em> is not perfect however.  If files are not in the AAC format it will have to convert them into this format before creating the audiobook.  If you use a free audiobook solution such as <a href="http://librivox.org/">librivox.org</a> this means it&#8217;ll have to convert each file before combining them into one.  Not too big of a deal, just plan a little extra time when creating an audiobook from a set of mp3s.</p>
<h2 style="text-align: left;">Quick Audiobook Tutorial</h2>
<p style="text-align: left;">Creating an audiobook is very easy in <em>Chapter and Verse</em>.  First <a href="http://lodensoftware.com/downloads/">download</a> and install the application.  You will need to make sure iTunes is installed as well because it is used by Chapter and Verse to convert files.</p>
<p>Once Chapter and Verse is installed start the application and it will load to an empty project screen.  Each project contains a set of AAC audio files which will end up being the chapters in this simple tutorial.</p>
<p>Begin by clicking the &#8220;Add Files&#8221; button to add the audio files you want to be a part of the audiobook.  The open file dialog only shows MP4 files by default so be sure to change the filter if you are adding say .mp3 files.</p>
<p>If you added a file that is not in the AAC format Chapter and Verse will attempt to convert the file using iTunes.  In that case click &#8220;Yes &#8211; Convert&#8221; on the conversion screen.  iTunes will open for the conversion, do not close it until it&#8217;s done.  When Chapter and Verse is done with the conversion your files will be added to the &#8220;Input Files&#8221; tab.</p>
<p>Click on the &#8220;Chapters&#8221; tab to change each chapter&#8217;s name.  On this tab you can rename each chapter in the audiobook.  For my audiobooks I generally set each chapter name to be the same as that of the file.  There are several options here that you may wish to explore.  Near the bottom of the tab there is an &#8220;Input Files&#8221; dropdown where you can choose prefixes for the chapter names.  I usually to choose the &lt;Filename&gt; option and leave the rest of the settings at their defaults.</p>
<p>The &#8220;Metadata&#8221; tab allows you to change the file information for the audiobook.  I usually set the title because that is displayed in the track listing on the device.</p>
<p>Finally make sure &#8220;Autobuild&#8221; is on and then click the &#8220;Build Audiobook&#8221; button.  Chapter and Verse will ask you where to save the file and then will create the book.</p>
<p>So to sum it all up</p>
<ol>
<li>Click Add Files to add your audio tracks, Chapter and Verse will convert them if necessary.</li>
<li>Edit the chapter titles on the &#8220;Chapters&#8221; tab.</li>
<li>Edit the final file information on the &#8220;Metadata&#8221; tab.</li>
<li>Build the audiobook</li>
</ol>
<p style="text-align: left;">&nbsp;</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">
<p>Ahh programming for a living&#8230;  There&#8217;s nothing quite like working on someone else&#8217;s problem for hours on end trying to get the dumb thing to do what you want.</p>
<p>Alright I&#8217;ll admit it; there are some days when I would rather be at home working on one of my own projects.  Those days I find it very hard to focus because one thought runs through my head.  &#8220;My own projects are fun and exciting and this is soooo BORING, I want to go home!&#8221;</p>
<p>But since my fun projects don&#8217;t put food on the table I have to snap out of it and get my head back into what actually does.  I find on those days it&#8217;s best to medicate the problem a little with my trusty iPod.  With my iPod I can do something for myself while still working and lately I have been on an audiobook and podcast kick.</p>
<p>iPods treat audiobooks and podcasts differently than they do music files.  For one you can leave a podcast, listen to something else, and later pick up where you left off.  An iPod won&#8217;t do this when you are listening to music.  Furthermore an audiobook can have chapter markings making it easier to find a chapter in a longer book.</p>
<p>iTunes makes it easy to create a music file for the iPod.  You can insert a CD and iTunes will import it at the push of a button.  Creating a proper audiobook however, is impossible with iTunes.  Oh sure you could import a whole CD as a single file but what if your audiobook covers multiple CDs?</p>
<p>I&#8217;ve had this problem for a while now.  My initial solution involved importing each track and numbering them in a way that they would line up in a playlist.  This works, but it&#8217;s annoying.  I can play the book but if I stop I have to remember which chapter I left off with.  If I don&#8217;t go back to the book for a couple of days I forget where I am and have to start over.  A couple of weeks ago I finally had enough and went searching for a better solution.</p>
<p>The solution to my problem came from a program called Chapter and Verse by lodensoftware.com.  The program will take a list of AAC (.m4a) files and convert them into one single audiobook (.m4b).  This makes creating an audiobook from a CD very easy.  Simply import the CD using iTunes in the AAC format and then use Chapter and Verse to make the conversion.</p>
<p>Chapter and Verse is not perfect however.  If files are not in the AAC format it will have to convert them into this format before creating the audiobook.  If you use a free audiobook solution such as librivox.org this means it&#8217;ll have to convert each file before combining them into one.  Not too big of a deal, just plan a little extra time when creating an audiobook from a set of mp3s.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.themike.com/create-an-audiobook/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting a Windows Forms .NET Application</title>
		<link>http://www.themike.com/starting-a-windows-forms-net-application</link>
		<comments>http://www.themike.com/starting-a-windows-forms-net-application#comments</comments>
		<pubDate>Sat, 10 Apr 2010 20:53:14 +0000</pubDate>
		<dc:creator>mike.hanson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.themike.com/?p=8</guid>
		<description><![CDATA[This week at work I have been redesigning the flow of our internal application. When Sally gets into work the first thing she does is launch this internal application. The application opens to a login form which disappears when she &#8230; <a href="http://www.themike.com/starting-a-windows-forms-net-application">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This week at work I have been redesigning the flow of our internal application.  When Sally gets into work the first thing she does is launch this internal application.  The application opens to a login form which disappears when she enters her credentials.  A secondary screen then shows up asking which set of data she would like to work with.  She chooses what she&#8217;s been assigned to work on and waits for the data to load in a third screen.</p>
<p>The code required to launch such an application is very straight forward.  The login form needs to be displayed and when it is dismissed the secondary form can be shown.  The third screen can be launched off of the second and will stay alive as long as the second is open.</p>
<p>Program.cs</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// Entry point for the test application</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #008080; font-style: italic;">/// The main entry point for the application.</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #008000;">&#91;</span>STAThread<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Application<span style="color: #008000;">.</span><span style="color: #0000FF;">EnableVisualStyles</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Application<span style="color: #008000;">.</span><span style="color: #0000FF;">SetCompatibleTextRenderingDefault</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Ask the user to login</span>
        Login<span style="color: #008000;">.</span><span style="color: #0000FF;">LoginScreen</span> login <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Login<span style="color: #008000;">.</span><span style="color: #0000FF;">LoginScreen</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Application<span style="color: #008000;">.</span><span style="color: #0000FF;">Run</span><span style="color: #008000;">&#40;</span>login<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>login<span style="color: #008000;">.</span><span style="color: #0000FF;">DialogResult</span> <span style="color: #008000;">==</span> DialogResult<span style="color: #008000;">.</span><span style="color: #0000FF;">OK</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Run the main application</span>
            Application<span style="color: #008000;">.</span><span style="color: #0000FF;">Run</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> NewWindowApp<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Take a look at lines 17 and 22 in the code sample from Program.cs.  The calls to Application.Run(Form) start a message loop on the current thread enabling the form to receive Windows messages.  Application.Run(Form) essentially says to Windows, &#8220;Here take this form and show it to the user.&#8221;  The form that is supplied can launch other child forms but when it dies so does the whole application.</p>
<p>My goal this week has been to make Sally&#8217;s job a little easier by removing the secondary screen and building its functionality into the third screen.  Now normally this would be as simple as replacing line ten with Application.Run(new ThirdForm());.  However that will not work in this case because the third form has a very useful &#8220;New Window&#8221; button.</p>
<p>Oh, it will work just fine for a while.  Sally can click the button leaving her with two views of the application.  However the message loop is only hooked up to the first form and if it is closed both forms will die off.</p>
<p>The solution to this problem is in an overload to the Application.Run method that takes an ApplicationContext property.  In fact the documentation at <a href="http://msdn.microsoft.com/en-us/library/ms157901.aspx">MSDN</a> shows a partial solution to my problem.</p>
<p>Instead of supplying Windows with a Form to display to the user I now supply a custom ApplicationContext object.  This custom context can manage the new window call and keep the application alive no mater which form is closed.</p>
<p>Context.cs</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Does the work of firing off new application windows.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">class</span> Context <span style="color: #008000;">:</span> ApplicationContext
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// The number of windows currently open</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> mWindowCount<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Initializes a new instance of the &lt;see cref=&quot;Context&quot;/&gt; class.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> Context<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NewWindow</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Creates and shows a new window.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> NewWindow<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        NewWindow window <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NewWindow<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        window<span style="color: #008000;">.</span><span style="color: #0000FF;">FormClosed</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> FormClosedEventHandler<span style="color: #008000;">&#40;</span>window_FormClosed<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">++</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">mWindowCount</span><span style="color: #008000;">;</span>
&nbsp;
        window<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Close out the application when all windows have been exited</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> window_FormClosed<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, FormClosedEventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        NewWindow window <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> NewWindow<span style="color: #008000;">;</span>            
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>window <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            window<span style="color: #008000;">.</span><span style="color: #0000FF;">FormClosed</span> <span style="color: #008000;">-=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">window_FormClosed</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008000;">--</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">mWindowCount</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">mWindowCount</span> <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ExitThread</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s important to note that this object knows how many windows there are.  Also note that as each window is created by NewWindow() a listener is added to the FormClosed event.  These two elements allow the context to know when the last form is closed and therefore when to finally exit.</p>
<p><a href="http://www.themike.com/wp-content/uploads/2011/04/newwindowapp.zip">Full source code for the project.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.themike.com/starting-a-windows-forms-net-application/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

