<?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>Tim Van Wassenhove &#187; Windows Forms</title>
	<atom:link href="http://www.timvw.be/category/information-technology/windows-forms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timvw.be</link>
	<description>The journey of a thousand miles begins with one step.</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:07:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Presenting FilterList</title>
		<link>http://www.timvw.be/presenting-filterlist/</link>
		<comments>http://www.timvw.be/presenting-filterlist/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 10:18:27 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1504</guid>
		<description><![CDATA[Earlier today i decided to add &#8216;Filtering&#8217; to my SortableBindingList. This resulted in writing a FilterList class. This class can be easily used as following: void textBoxFilter_KeyUp(object sender, KeyEventArgs e) { var filterChars = this.textBoxFilter.Text.ToLower(); this.Filter(filterChars); } void Filter(string filterChars) { var persons = (FilterList&#60;Person&#62;)this.dataGridView1.DataSource; persons.Filter(p =&#62; p.Firstname.ToLower().Contains(filterChars)); } I even created a screencast to [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier today i decided to add &#8216;Filtering&#8217; to my SortableBindingList. This resulted in writing a <a href="http://www.timvw.be/wp-content/code/csharp/FilterList.txt">FilterList</a> class. This class can be easily used as following:</p>

<pre class="brush: csharp;">void textBoxFilter_KeyUp(object sender, KeyEventArgs e)
{
 var filterChars = this.textBoxFilter.Text.ToLower();
 this.Filter(filterChars);
}

void Filter(string filterChars)
{
 var persons = (FilterList&lt;Person&gt;)this.dataGridView1.DataSource;
 persons.Filter(p =&gt; p.Firstname.ToLower().Contains(filterChars));
}</pre>

<p>I even created a screencast to demonstrate it:</p>

<a href="http://www.timvw.be/presenting-filterlist/#mediaPlayer_1504_0">Play Video</a>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/presenting-filterlist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://www.timvw.be/screencasts/filterlist.wmv" length="357569" type="video/x-ms-wmv" />
		</item>
		<item>
		<title>Presenting ControlStateMachine</title>
		<link>http://www.timvw.be/presenting-controlstatemachine/</link>
		<comments>http://www.timvw.be/presenting-controlstatemachine/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 07:10:42 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1170</guid>
		<description><![CDATA[Here is a situation we are all familiar with: A form that only displays a certain set of controls depending on the mode or state of the application. Let me start with an example: At design time there are three buttons The user can look at the data and decide to edit it: Or the [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a situation we are all familiar with: A form that only displays a certain set of controls depending on the mode or state of the application. Let me start with an example: At design time there are three buttons</p>

<img src="http://www.timvw.be/wp-content/images/controlstatemachine.design.png" alt="screenshot of flowlayoutpanel with three buttons: edit, save and cancel." />

<p>The user can look at the data and decide to edit it:</p>

<img src="http://www.timvw.be/wp-content/images/controlstatemachine.display.png" alt="screenshot of flowlayoutpanel with only one visible button: edit." />

<p>Or the user is editing the data and can decide to commit or discard her changes:</p>

<img src="http://www.timvw.be/wp-content/images/controlstatemachine.edit.png" alt="screenshot of flowlayoutpanel with two visible buttons: save and cancel." />

<p>A couple of years i ago i used to spread such display logic all over my code and it was hard to figure out which control was visible at a given point. Later on i refactored that code and encapsulated it in functions like: MakeControlsForDisplayVisible and MakeControlsForEditVisible which felt like a huge improvement. These days i have the feeling that a very simple state machine can improve the readability even better.</p>

<p>Ok, so how simple is simple? Currently the requirements list is pretty limited:</p>

<img src="http://www.timvw.be/wp-content/images/controlstatemachine.specs.png" alt="screenshot of unittests for controlstatemachine" />

<p>Anyway, here is how i would write the code today (Yeah, for a stupid example this looks like overkill):</p>

<pre class="brush: csharp;">private void InitializeButtonLayoutPanelMachine()
{
 controlStateMachine = new ControlStateMachine&lt;DisplayAndEditStates&gt;(buttonLayoutPanel);

 controlStateMachine.WhenStateChangesTo(DisplayAndEditStates.Display)
  .TheOnlyVisibleControlsAre(buttonEdit);

 controlStateMachine.WhenStateChangesTo(DisplayAndEditStates.Edit)
  .TheOnlyVisibleControlsAre(buttonSave, buttonCancel);
}</pre>

<p>As always, here is the source: <a href="http://www.timvw.be/wp-content/code/csharp/ControlStateMachine.cs.txt">ControlStateMachine</a> and <a href="http://www.timvw.be/wp-content/code/csharp/WhenChangingState.cs.txt">WhenChangingState</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/presenting-controlstatemachine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Presenting ControlChanges</title>
		<link>http://www.timvw.be/presenting-controlchanges/</link>
		<comments>http://www.timvw.be/presenting-controlchanges/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 18:16:02 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1163</guid>
		<description><![CDATA[Because i noticed that i kept writing the same operations on control over and over again i decided to capture them in a couple of functions. I presume most of you have done this already. Here is the list of operations: In case it is not clear what these methods should do i have defined [...]]]></description>
			<content:encoded><![CDATA[<p>Because i noticed that i kept writing the same operations on control over and over again i decided to capture them in a couple of functions. I presume most of you have done this already. Here is the list of operations:</p>

<img src="http://www.timvw.be/wp-content/images/controlchanges.cd.png" alt="screenshot of a class diagram with the following operations: MakeVisible, MakeInvisible and TheOnlyVisibleControlsAre." />

<p>In case it is not clear what these methods should do i have defined the following specifications for them:</p>

<img src="http://www.timvw.be/wp-content/images/controlchanges.specs.png" alt="screenshot of requirements list for controlchanges." />

<p>Get the code here: <a href="http://www.timvw.be/wp-content/code/csharp/ControlChanges.cs.txt">ControlChanges</a> and <a href="http://www.timvw.be/wp-content/code/csharp/WhenExecutingControlChanges.cs.txt">WhenExecutingControlChanges</a>. Stay tuned for more!</p>

]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/presenting-controlchanges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Experimenting with ControlStateMachine and Fluent interfaces</title>
		<link>http://www.timvw.be/experimenting-with-controlstatemachine-and-fluent-interfaces/</link>
		<comments>http://www.timvw.be/experimenting-with-controlstatemachine-and-fluent-interfaces/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 18:52:25 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1157</guid>
		<description><![CDATA[A long time ago i read Build your own CAB series and recently i noticed that there is a wiki: Presentation Patterns Wiki! and it inspired me to experiment with state machines. Here are a couple of examples: controlStateMachine = new ControlStateMachine&#60;States&#62;(this); controlStateMachine.AfterEachStateChange() .Do(MakeRelevantButtonsVisible); controlStateMachine.WhenStateChangesTo(States.RetrievingSubscriptionPeriod) .TheOnlyVisibleControlsAre(flowLayoutPanel1, datePicker1); controlStateMachine.WhenStateChangesTo(States.RetrievingCustomerInformation) .MakeVisible(customerInput1) .Do(() =&#62; customerInput1.Dock = DockStyle.Fill); controlStateMachine.WhenStateChangesTo(States.Ready) [...]]]></description>
			<content:encoded><![CDATA[<p>A long time ago i read <a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/07/25/the-build-your-own-cab-series-table-of-contents.aspx">Build your own CAB series</a> and recently i noticed that there is a wiki: <a href="http://www.jeremydmiller.com/ppatterns/Default.aspx?Page=MainPage&#038;AspxAutoDetectCookieSupport=1">Presentation Patterns Wiki!</a> and it inspired me to experiment with state machines. Here are a couple of examples:</p>

<pre class="brush: csharp;">controlStateMachine = new ControlStateMachine&lt;States&gt;(this);

controlStateMachine.AfterEachStateChange()
 .Do(MakeRelevantButtonsVisible);

controlStateMachine.WhenStateChangesTo(States.RetrievingSubscriptionPeriod)
 .TheOnlyVisibleControlsAre(flowLayoutPanel1, datePicker1);

controlStateMachine.WhenStateChangesTo(States.RetrievingCustomerInformation)
 .MakeVisible(customerInput1)
 .Do(() =&gt; customerInput1.Dock = DockStyle.Fill);

controlStateMachine.WhenStateChangesTo(States.Ready)
 .MakeInvisible(customerInput1);
</pre>

<p>And here is another example:</p>

<pre class="brush: csharp;">wizardStateMachine = new WizardStateMachine&lt;States&gt;(controlStateMachine);

wizardStateMachine.InState(States.RetrievingSubscriptionPeriod)
 .OnCommand(WizardCommands.Next)
  .TransitionTo(States.RetrievingCustomerInformation);

wizardStateMachine.InState(States.RetrievingCustomerInformation)
 .OnCommand(WizardCommands.Back)
  .TransitionTo(States.RetrievingSubscriptionPeriod)
 .OnCommand(WizardCommands.Finish)
  .TransitionTo(States.Ready);

wizardStateMachine.InState(States.Ready)
 .OnCommand(WizardCommands.New)
  .Do(() =&gt; MessageBox.Show(&quot;Currently not supported&quot;));
</pre>

<p>Stay tuned for future posts where i describe the problem space that have lead to this API.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/experimenting-with-controlstatemachine-and-fluent-interfaces/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Presenting AssemblyTypePicker</title>
		<link>http://www.timvw.be/presenting-assemblytypepicker/</link>
		<comments>http://www.timvw.be/presenting-assemblytypepicker/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 15:32:27 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=476</guid>
		<description><![CDATA[I really like the way the Object Browser makes the types in an assembly visible. Because i have a couple of programs that require a given type as input, i have decided to add a TypeTree control to BeTimvwFramework that mimicks the Object Browser. Here are a couple of screenshots of the control in a [...]]]></description>
			<content:encoded><![CDATA[<p>I really like the way the <a href="http://msdn.microsoft.com/en-us/library/exy1facf(VS.80).aspx">Object Browser</a> makes the types in an assembly visible. Because i have a couple of programs that require a given type as input, i have decided to add a TypeTree control to <a href="http://www.codeplex.com/BeTimvwFramework">BeTimvwFramework</a> that mimicks the Object Browser. Here are a couple of screenshots of the control in a demo application that allows the user to generate interfaces and wrapper classes based on a selected type:</p>

<img src="http://www.timvw.be/wp-content/images/codegenerator_01.gif" alt="screenshot of assemblytypepicker with no values" />
<br/>
<img src="http://www.timvw.be/wp-content/images/codegenerator_02.gif" alt="screenshot of dialog that requests the user to pick an assembly file"/>
<br/>
<img src="http://www.timvw.be/wp-content/images/codegenerator_03.gif" alt="screenshot of dialog that requests the user to pick a type in the previously selected assembly"/>

<p>Feel free to download <a href="http://www.timvw.be/wp-content/code/csharp/CodeGenerator.zip">CodeGenerator.zip</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/presenting-assemblytypepicker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display WorkItems in a WinForms application</title>
		<link>http://www.timvw.be/display-workitems-in-a-winforms-application/</link>
		<comments>http://www.timvw.be/display-workitems-in-a-winforms-application/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 18:51:10 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/display-workitems-in-a-winforms-application/</guid>
		<description><![CDATA[Using the Microsoft.TeamFoundation.WorkItemTracking.Controls assembly it is possibe to display WorkItems. Here is a little demo application that will display all the WorkItems that have been changed by one of the given users in the given range: Feel free to download the source: WorkItemTracker.zip Edit (05/03/2008): Refactored the code a little and added some features like [...]]]></description>
			<content:encoded><![CDATA[<p>Using the Microsoft.TeamFoundation.WorkItemTracking.Controls assembly it is possibe to display WorkItems. Here is a little demo application that will display all the WorkItems that have been changed by one of the given users in the given range:</p>
<img src="http://www.timvw.be/wp-content/images/workitemtracker.gif" alt="screenshot of workitemtracker application"/>
<p>Feel free to download the source: <a href="http://www.timvw.be/wp-content/code/csharp/WorkItemTracker.zip">WorkItemTracker.zip</a></p>

<p>
<b>Edit (05/03/2008): </b><br/>
Refactored the code a little and added some features like sortable columns, loading default tfsserver and users from App.Config, &#8230;
</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/display-workitems-in-a-winforms-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How the name of an embedded resource is generated in a C# project</title>
		<link>http://www.timvw.be/how-the-name-of-an-embedded-resource-is-generated-in-a-c-project/</link>
		<comments>http://www.timvw.be/how-the-name-of-an-embedded-resource-is-generated-in-a-c-project/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 17:32:04 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/how-the-name-of-an-embedded-resource-is-generated-in-a-c-project/</guid>
		<description><![CDATA[A while ago i was wondering how the name of an embedded resource is generated in a C&#35; project. Earlier today i was looking in Microsoft.CSharp.targets and found the answer: The CreateManifestResourceNames target create the manifest resource names from the .RESX files. [IN] @(ResxWithNoCulture) - The names the non-culture .RESX files. @(ResxWithCulture) - The names [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago i was wondering how the name of an embedded resource is generated in a C&#35; project. Earlier today i was looking in Microsoft.CSharp.targets and found the answer:</p>
<blockquote><pre>
The CreateManifestResourceNames target create the manifest resource names from the .RESX files.

[IN]
@(ResxWithNoCulture) - The names the non-culture .RESX files.
@(ResxWithCulture) - The names the culture .RESX files.
@(NonResxWithNoCulture) - The names of the non-culture non-RESX files (like bitmaps, etc).
@(NonResxWithCulture) - The names of the culture non-RESX files (like bitmaps, etc).

[OUT]
@(ManifestResourceWithNoCultureName) - The corresponding manifest resource name (.RESOURCE)
@(ManifestResourceWithCultureName) - The corresponding manifest resource name (.RESOURCE)
@(ManifestNonResxWithNoCulture) - The corresponding manifest resource name.
@(ManifestNonResxWithCulture) - The corresponding manifest resource name.

For C# applications the transformation is like:

Resources1.resx => RootNamespace.Resources1 => Build into main assembly
SubFolder\Resources1.resx => RootNamespace.SubFolder.Resources1 => Build into main assembly
Resources1.fr.resx => RootNamespace.Resources1.fr => Build into satellite assembly
Resources1.notaculture.resx => RootNamespace.Resources1.notaculture => Build into main assembly

For other project systems, this transformation may be different.
</pre></blockquote>

<p>With <a href="http://www.attrice.info/msbuild/index.htm">Attrice Corporation Microsoft Build Sidekick v2</a> you can easily visualize the flow throughout the targets via Tools -> View Targets Diagram.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/how-the-name-of-an-embedded-resource-is-generated-in-a-c-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using DateTimePicker and Custom Format</title>
		<link>http://www.timvw.be/using-datetimepicker-and-custom-format/</link>
		<comments>http://www.timvw.be/using-datetimepicker-and-custom-format/#comments</comments>
		<pubDate>Wed, 29 Aug 2007 15:32:42 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/using-datetimepicker-and-custom-format/</guid>
		<description><![CDATA[Today we ran into a nasty problem with DateTimePickerFormat.Custom. We allow the user to input a month/date with a DateTimePicker as following (nothing fancy): private void Form1_Load(object sender, EventArgs e) { this.dateTimePicker1.Value = new DateTime(2007, 8, 31); this.dateTimePicker1.Format = DateTimePickerFormat.Custom; this.dateTimePicker1.CustomFormat = &#34;MM/yyyy&#34;; } Now, change to 09/2007 and notice that you get an Exception, [...]]]></description>
			<content:encoded><![CDATA[<p>Today we ran into a nasty problem with <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.datetimepickerformat.aspx">DateTimePickerFormat</a>.Custom. We allow the user to input a month/date with a DateTimePicker as following (nothing fancy):</p>
<pre class="brush: csharp;">private void Form1_Load(object sender, EventArgs e)
{
 this.dateTimePicker1.Value = new DateTime(2007, 8, 31);
 this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
 this.dateTimePicker1.CustomFormat = &quot;MM/yyyy&quot;;
}</pre>
<p>Now, change to 09/2007 and notice that you get an Exception, because the control tries to create an unrepresentable new DateTime(2007, 8+1, 31). Thus, if you&#8217;re going to use the DateTimePicker for MM/yyyy input make sure to set it&#8217;s value to a DateTimeTime with a day component that exists for all months/years (thus a value between 1 and 28).</p>
<p><b>Addendum:</b> As usual, super moderator and MVP <a href="https://mvp.support.microsoft.com/default.aspx/profile=6c93adc6-026f-42bf-823c-8e65ca732af2">Hans Passant</a> provided a nice workaround for the problem:)</p>
<pre class="brush: csharp;">public class MonthPicker : DateTimePicker {
  public MonthPicker() {
    this.Format = DateTimePickerFormat.Custom;
    this.CustomFormat = &quot;MM/yyyy&quot;;
    DateTime now = DateTime.Now;
    this.Value = new DateTime(now.Year, now.Month, 1);
  }
  protected override void WndProc(ref Message m) {
    if (m.Msg == 0x204e) {
      NMHDR hdr = (NMHDR)m.GetLParam(typeof(NMHDR));
      if (hdr.code == -759) {
        NMDATETIMECHANGE dt = (NMDATETIMECHANGE)m.GetLParam(typeof(NMDATETIMECHANGE));
        this.Value = new DateTime(dt.st.wYear, dt.st.wMonth, 1);
        return;
      }
    }
    base.WndProc(ref m);
  }
  // P/Invoke declarations
  [StructLayout(LayoutKind.Sequential)]
  private struct NMHDR {
    public IntPtr hWnd;
    public IntPtr id;
    public int code;
  }
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  private class NMDATETIMECHANGE {
    public NMHDR nmhdr;
    public int dwFlags;
    public SYSTEMTIME st;
  }
  [StructLayout(LayoutKind.Sequential)]
  private class SYSTEMTIME {
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;
  }
}</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/using-datetimepicker-and-custom-format/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bending the code generation of IExtenderProvider to your will</title>
		<link>http://www.timvw.be/bending-the-code-generation-of-iextenderprovider-to-your-will/</link>
		<comments>http://www.timvw.be/bending-the-code-generation-of-iextenderprovider-to-your-will/#comments</comments>
		<pubDate>Tue, 21 Aug 2007 17:15:24 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/bending-the-code-generation-of-iextenderprovider-to-your-will/</guid>
		<description><![CDATA[In Exploring CodeDomSerializer i already explained how we can modify the code that the Visual Studio designer generates for us. With a typical IExtenderProvider the designer generates an initializer, SetXXX methods and a variable declaration, which looks like: this.constantsExtenderProvider1 = new WindowsApplication1.ConstantsExtenderProvider(); this.constantsExtenderProvider1.SetConstants(this.button1, new string[] { &#34;Operation1&#34;, &#34;Operation5&#34;}); this.constantsExtenderProvider1.SetConstants(this, null); private ConstantsExtenderProvider constantsExtenderProvider1; Now, what [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.timvw.be/exploring-codedomserializer/">Exploring CodeDomSerializer</a> i already explained how we can modify the code that the Visual Studio designer generates for us. With a typical <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.iextenderprovider.aspx">IExtenderProvider</a> the designer generates an initializer, SetXXX methods and a variable declaration, which looks like:</p>

<pre class="brush: csharp;">this.constantsExtenderProvider1 = new WindowsApplication1.ConstantsExtenderProvider();

this.constantsExtenderProvider1.SetConstants(this.button1, new string[] {
 &quot;Operation1&quot;,
 &quot;Operation5&quot;});

this.constantsExtenderProvider1.SetConstants(this, null);

private ConstantsExtenderProvider constantsExtenderProvider1;</pre>

<p>Now, what if we&#8217;re not happy with those generated SetXXX methods on each Component? The problem is that this code is not generated by the serializer for the ConstantsExtenderProvider but by the serializers for the Components. An easy workaround for this problem is to set the <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.aspx">DesignerSerializationVisibilityAttribute</a> on the GetXXX method in our IExtenderProvider to <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibility.aspx">Hidden</a>.</p>

<p>With those ugly SetXXX methods out of the way it&#8217;s up to us to do it better. We do this by implementing a custom serializer for our ConstantsExtenderProvider:</p>

<pre class="brush: csharp;">class ConstantsSerializer&lt;T&gt; : CodeDomSerializer
{
 public override object Serialize(IDesignerSerializationManager manager, object value)
 {
  ConstantsExtenderProvider provider = value as ConstantsExtenderProvider;

  CodeDomSerializer baseClassSerializer = manager.GetSerializer(typeof(ConstantsExtenderProvider).BaseType, typeof(CodeDomSerializer)) as CodeDomSerializer;
  CodeStatementCollection statements = baseClassSerializer.Serialize(manager, value) as CodeStatementCollection;

  IDesignerHost host = (IDesignerHost)manager.GetService(typeof(IDesignerHost));
  ComponentCollection components = host.Container.Components;
  this.SerializeExtender(manager, provider, components, statements);

  return statements;
 }

 private void SerializeExtender(IDesignerSerializationManager manager, ConstantsExtenderProvider provider, ComponentCollection components, CodeStatementCollection statements)
 {
  foreach (IComponent component in components)
  {
   Control control = component as Control;
   if (control != null &amp;&amp; (control as Form == null))
   {
    CodeMethodInvokeExpression methodcall = new CodeMethodInvokeExpression(base.SerializeToExpression(manager, provider), &quot;SetConstants&quot;);
    methodcall.Parameters.Add(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), control.Name));

    string[] constants = provider.GetConstants(control);
    if (constants != null)
    {
     StringBuilder sb = new StringBuilder();
     sb.Append(&quot;new string[] { &quot;);

     foreach (string constant in constants)
     {
      sb.Append(typeof(T).FullName);
      sb.Append(&quot;.&quot;);
      sb.Append(constant);
      sb.Append(&quot;, &quot;);
     }

     sb.Remove(sb.Length - 2, 2);
     sb.Append(&quot; }&quot;);

     methodcall.Parameters.Add(new CodeSnippetExpression(sb.ToString()));
    }
    else
    {
     methodcall.Parameters.Add(new CodePrimitiveExpression(null));
    }
    
    statements.Add(methodcall);
   }
  }
 }
}</pre>

<p>And now the generated code looks like:</p>

<pre class="brush: csharp;">this.constantsExtenderProvider1.SetConstants(this.button1, new string[] { WindowsApplication1.Constants.Operation1, WindowsApplication1.Constants.Operation5 });</pre>

<p>As always, feel free to download the <a href="http://www.timvw.be/wp-content/code/csharp/ConstantsExtenderProvider.zip">ConstantsExtenderProvider</a> source.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/bending-the-code-generation-of-iextenderprovider-to-your-will/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging custom UITypeEditors</title>
		<link>http://www.timvw.be/debugging-custom-uitypeeditors/</link>
		<comments>http://www.timvw.be/debugging-custom-uitypeeditors/#comments</comments>
		<pubDate>Fri, 10 Aug 2007 16:26:42 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.timvw.be/debugging-custom-uitypeeditors/</guid>
		<description><![CDATA[If you read this you&#8217;re probably gonna think: What a moron! Anyway, i&#8217;m sharing this in the hope that i&#8217;ll be the last to undergo the following. In order to test my custom UITypeEditor i did the following: Create a UserControl Add a property to the control Add an Editor attribute to the property Build [...]]]></description>
			<content:encoded><![CDATA[<p>If you read this you&#8217;re probably gonna think: What a moron! Anyway, i&#8217;m sharing this in the hope that i&#8217;ll be the last to undergo the following. In order to test my custom <a href="http://msdn2.microsoft.com/en-us/library/system.drawing.design.uitypeeditor.aspx">UITypeEditor</a> i did the following:</p>
<ol>
<li>Create a <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx">UserControl</a></li>
<li>Add a property to the control </li>
<li>Add an <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.editorattribute.aspx">Editor</a> attribute to the property</li>
<li>Build</li>
<li>Drag a UserControl on the designer form</li>
<li>Test via Visual Studio&#8217;s Property Window if the UITypeEditor works as expected</li>
<li>Everytime i changed some code: <b>Restart Visual Studio</b>, load the project and repeat 6.</li>
</ol>
<p>A tedious task to say the least. Yesterday i figured out that i could <b>drag a <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.propertygrid.aspx">PropertyGrid</a> on the designer form, and set it&#8217;s SelectedObject property to a class with a property that uses the custom UITypeEditor; Instead of having to reload visual studio i can simply start a debug session, and click on the property in the PropertyGrid.</b> Now it&#8217;s a breeze to develop custom UITypeEditors <img src='http://www.timvw.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://www.timvw.be/wp-content/images/uitypeeditor.gif" alt=""/>
<pre class="brush: csharp;">private void Form1_Load(object sender, EventArgs e)
{
 // display an instance of PersonEntry, 
 // a class with a property that should use the custom UITypeEditor i want to test
 this.propertyGrid1.SelectedObject = new PersonEntry(new Person(&quot;Tim&quot;, &quot;Van Wassenhove&quot;));
}

public class PersonEntry
{
 ...

 // instruct the PropertyGrid to use my custom PersonUITypeEditor
 [Editor(typeof(PersonUITypeEditor), typeof(UITypeEditor))]
 public Person Person
 {
  get { return this.person; }
  set { this.person = value; }
 }
}

public class PersonUITypeEditor : UITypeEditor
{
 public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
 {
  return UITypeEditorEditStyle.Modal;
 }

 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
 {
  Person person = value as Person;

  IWindowsFormsEditorService svc = context.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
  if (svc != null)
  {
   using (PersonEditorForm personEditorForm = new PersonEditorForm(person))
   {
    if (svc.ShowDialog(personEditorForm) == DialogResult.OK)
    {
     return personEditorForm.Person;
    }
   }
  }

  return value;
 }
}</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/debugging-custom-uitypeeditors/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
