<?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 &#187; c#</title>
	<atom:link href="http://www.themike.com/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://www.themike.com</link>
	<description></description>
	<lastBuildDate>Sat, 29 Oct 2011 18:56:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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>

