<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>CTRLALT313373.com - Windows Workflow</title>
    <link>http://weblog.ctrlalt313373.com/</link>
    <description>.Net Wanderings</description>
    <language>en-us</language>
    <copyright>David A. Osborn</copyright>
    <lastBuildDate>Mon, 21 Jan 2008 02:38:13 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>davido@ctrlalt313373.com</managingEditor>
    <webMaster>davido@ctrlalt313373.com</webMaster>
    <item>
      <trackback:ping>http://weblog.ctrlalt313373.com/Trackback.aspx?guid=e3b53112-1821-4a62-aa13-efa9d26afbdd</trackback:ping>
      <pingback:server>http://weblog.ctrlalt313373.com/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ctrlalt313373.com/PermaLink,guid,e3b53112-1821-4a62-aa13-efa9d26afbdd.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://weblog.ctrlalt313373.com/CommentView,guid,e3b53112-1821-4a62-aa13-efa9d26afbdd.aspx</wfw:comment>
      <wfw:commentRss>http://weblog.ctrlalt313373.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e3b53112-1821-4a62-aa13-efa9d26afbdd</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
So far I have went over how to <a href="http://weblog.ctrlalt313373.com/2007/12/27/CreatingARegExActivityForWindowsWorkflow.aspx">create
a basic workflow activity</a> that evaluates a regular expression against a string
and I have also covered <a href="http://weblog.ctrlalt313373.com/2007/12/29/AddingValidationToACustomWorkflowActivity.aspx">creating
the validation class</a> for the activity. The next thing I want to cover are some
basics to enhancing the designer experience of the workflow activity allowing you
to override the default visual display in the designer for a custom activity. Based
on the default designer class our RegEx activity appears as follows: 
</p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/012108_0237_Modifyingth1.png" alt="" />
        </p>
        <p>
The first thing that needs to be done is to add a new class to the project called
RegExDesigner.cs. 
</p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/012108_0237_Modifyingth2.png" alt="" />
        </p>
        <p>
        </p>
        <p>
Next we need to make our new class inherit from the ActivityDesigner class. To do
this we will also need to import the namespace <span style="font-family:Courier New; font-size:10pt">System.Workflow.ComponentModel.Design, </span>which
means your code should now appear as follows: 
</p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">using</span> System.Workflow.ComponentModel.Design; </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">namespace</span> FloFactory.Activities.Util.Logic </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">{ </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">class</span>
            <span style="color:#2b91af">RegExDesigner</span> : <span style="color:#2b91af">ActivityDesigner </span></span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">} </span>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
There are a lot of functions that you can override, but the main function that we
are going to work with is the Initialize function which will allow us to override
the appearance of our activity in the designer. This first thing that I am going to
do is add a nice image to the activity. Assuming you already have an image that you
want to use as a resource in your project you add the image to the activity's design
view by doing the following: 
</p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">protected</span>
            <span style="color:blue">override</span>
            <span style="color:blue">void</span> Initialize(System.Workflow.ComponentModel.<span style="color:#2b91af">Activity</span> activity) </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">base</span>.Initialize(activity); </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:#2b91af">Bitmap</span> WorkFlowImage
= Properties.<span style="color:#2b91af">Resources</span>.workflow; </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> Image = WorkFlowImage; </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
          </span>The other thing I would
like to do is change the text that appears in the activity. This will be the default
text that shows up until the user of our activity sets the name property. This is
easily done by setting the Text property in the Initialize function. After doing this
the code should appear as follows: 
</p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">protected</span>
            <span style="color:blue">override</span>
            <span style="color:blue">void</span> Initialize(System.Workflow.ComponentModel.<span style="color:#2b91af">Activity</span> activity) </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">base</span>.Initialize(activity); </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:#2b91af">Bitmap</span> WorkFlowImage
= Properties.<span style="color:#2b91af">Resources</span>.workflow; </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> Image = WorkFlowImage; </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:green">//Set
the Text That appears </span>
          </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> Text = <span style="color:#a31515">"FloFactory
RegEx"</span>; </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
          </span>Now we need to associate
the designer class with our activity. We do this by going to the top of the RegEx.cs
class and add the following attribute:<span style="font-family:Courier New; font-size:10pt"></span></p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> [<span style="color:#2b91af">Designer</span>(<span style="color:blue">typeof</span>(<span style="color:#2b91af">RegExDesigner</span>), <span style="color:blue">typeof</span>(<span style="color:#2b91af">IDesigner</span>))] </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">partial</span>
            <span style="color:blue">class</span>
            <span style="color:#2b91af">RegEx</span>: <span style="color:#2b91af">BaseActivity </span></span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> … </span>
        </p>
        <p style="margin-left: 36pt">
          <span style="font-family:Courier New; font-size:10pt">} </span>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
          </span>After adding this code
to the Initialize function and adding the attribute if we now look at the RegEx activity
we will see that our image has replaced the default image that appeared when the original
designer class was applied to the RegEx activity. Also, the default text that was
defined appears in the center of the activity. 
</p>
        <p>
        </p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/012108_0237_Modifyingth3.png" alt="" />
        </p>
        <p>
        </p>
        <p>
As you can see we have easily modified the image and the text that appears in the
designer for our RegEx activity. In a future post I will cover modifying the appearance
of the activity by changing its size and colors so as to create a more custom look
and feel for the activity. 
</p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
          </span>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
          </span>
        </p>
        <img width="0" height="0" src="http://weblog.ctrlalt313373.com/aggbug.ashx?id=e3b53112-1821-4a62-aa13-efa9d26afbdd" />
      </body>
      <title>Modifying the Designer Appearance of the RegEx Activity</title>
      <guid isPermaLink="false">http://weblog.ctrlalt313373.com/PermaLink,guid,e3b53112-1821-4a62-aa13-efa9d26afbdd.aspx</guid>
      <link>http://weblog.ctrlalt313373.com/2008/01/21/ModifyingTheDesignerAppearanceOfTheRegExActivity.aspx</link>
      <pubDate>Mon, 21 Jan 2008 02:38:13 GMT</pubDate>
      <description>&lt;p&gt;
So far I have went over how to &lt;a href="http://weblog.ctrlalt313373.com/2007/12/27/CreatingARegExActivityForWindowsWorkflow.aspx"&gt;create
a basic workflow activity&lt;/a&gt; that evaluates a regular expression against a string
and I have also covered &lt;a href="http://weblog.ctrlalt313373.com/2007/12/29/AddingValidationToACustomWorkflowActivity.aspx"&gt;creating
the validation class&lt;/a&gt; for the activity. The next thing I want to cover are some
basics to enhancing the designer experience of the workflow activity allowing you
to override the default visual display in the designer for a custom activity. Based
on the default designer class our RegEx activity appears as follows: 
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/012108_0237_Modifyingth1.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
The first thing that needs to be done is to add a new class to the project called
RegExDesigner.cs. 
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/012108_0237_Modifyingth2.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Next we need to make our new class inherit from the ActivityDesigner class. To do
this we will also need to import the namespace &lt;span style="font-family:Courier New; font-size:10pt"&gt;System.Workflow.ComponentModel.Design, &lt;/span&gt;which
means your code should now appear as follows: 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;using&lt;/span&gt; System.Workflow.ComponentModel.Design; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;namespace&lt;/span&gt; FloFactory.Activities.Util.Logic &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;{ &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;RegExDesigner&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;ActivityDesigner &lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;} &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
There are a lot of functions that you can override, but the main function that we
are going to work with is the Initialize function which will allow us to override
the appearance of our activity in the designer. This first thing that I am going to
do is add a nice image to the activity. Assuming you already have an image that you
want to use as a resource in your project you add the image to the activity's design
view by doing the following: 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;protected&lt;/span&gt; &lt;span style="color:blue"&gt;override&lt;/span&gt; &lt;span style="color:blue"&gt;void&lt;/span&gt; Initialize(System.Workflow.ComponentModel.&lt;span style="color:#2b91af"&gt;Activity&lt;/span&gt; activity) &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;base&lt;/span&gt;.Initialize(activity); &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:#2b91af"&gt;Bitmap&lt;/span&gt; WorkFlowImage
= Properties.&lt;span style="color:#2b91af"&gt;Resources&lt;/span&gt;.workflow; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; Image = WorkFlowImage; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;/span&gt;The other thing I would
like to do is change the text that appears in the activity. This will be the default
text that shows up until the user of our activity sets the name property. This is
easily done by setting the Text property in the Initialize function. After doing this
the code should appear as follows: 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;protected&lt;/span&gt; &lt;span style="color:blue"&gt;override&lt;/span&gt; &lt;span style="color:blue"&gt;void&lt;/span&gt; Initialize(System.Workflow.ComponentModel.&lt;span style="color:#2b91af"&gt;Activity&lt;/span&gt; activity) &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;base&lt;/span&gt;.Initialize(activity); &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:#2b91af"&gt;Bitmap&lt;/span&gt; WorkFlowImage
= Properties.&lt;span style="color:#2b91af"&gt;Resources&lt;/span&gt;.workflow; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; Image = WorkFlowImage; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:green"&gt;//Set
the Text That appears &lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; Text = &lt;span style="color:#a31515"&gt;"FloFactory
RegEx"&lt;/span&gt;; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;/span&gt;Now we need to associate
the designer class with our activity. We do this by going to the top of the RegEx.cs
class and add the following attribute:&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; [&lt;span style="color:#2b91af"&gt;Designer&lt;/span&gt;(&lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;RegExDesigner&lt;/span&gt;), &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;IDesigner&lt;/span&gt;))] &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;partial&lt;/span&gt; &lt;span style="color:blue"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;: &lt;span style="color:#2b91af"&gt;BaseActivity &lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; … &lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin-left: 36pt"&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;} &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;/span&gt;After adding this code
to the Initialize function and adding the attribute if we now look at the RegEx activity
we will see that our image has replaced the default image that appeared when the original
designer class was applied to the RegEx activity. Also, the default text that was
defined appears in the center of the activity. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/012108_0237_Modifyingth3.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
As you can see we have easily modified the image and the text that appears in the
designer for our RegEx activity. In a future post I will cover modifying the appearance
of the activity by changing its size and colors so as to create a more custom look
and feel for the activity. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;/span&gt; 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ctrlalt313373.com/aggbug.ashx?id=e3b53112-1821-4a62-aa13-efa9d26afbdd" /&gt;</description>
      <comments>http://weblog.ctrlalt313373.com/CommentView,guid,e3b53112-1821-4a62-aa13-efa9d26afbdd.aspx</comments>
      <category>.Net 3.0</category>
      <category>Development</category>
      <category>Windows Workflow</category>
    </item>
    <item>
      <trackback:ping>http://weblog.ctrlalt313373.com/Trackback.aspx?guid=5895c3e5-8465-4d3f-abdc-555fcfa4160d</trackback:ping>
      <pingback:server>http://weblog.ctrlalt313373.com/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ctrlalt313373.com/PermaLink,guid,5895c3e5-8465-4d3f-abdc-555fcfa4160d.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://weblog.ctrlalt313373.com/CommentView,guid,5895c3e5-8465-4d3f-abdc-555fcfa4160d.aspx</wfw:comment>
      <wfw:commentRss>http://weblog.ctrlalt313373.com/SyndicationService.asmx/GetEntryCommentsRss?guid=5895c3e5-8465-4d3f-abdc-555fcfa4160d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm my last <a href="http://weblog.ctrlalt313373.com/2007/12/27/CreatingARegExActivityForWindowsWorkflow.aspx">posting</a> I
walked through how to create a custom regex activity in Windows Workflow and I thought
a good next step would be to give a short example of how you would add validation
to this custom activity. In this particular instance we want to make the InputString
property and the RegExpression property required in order for the workflow designer
to consider the workflow that is being built a valid workflow. The first thing that
needs to be done is to add a class to the project called RegExValidator.cs. 
</p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/122907_0256_AddingValid1.png" alt="" />
        </p>
        <p>
        </p>
        <p>
The next thing that needs to be done to the new class is to add <span style="font-family:Courier New; font-size:10pt"><span style="color:blue">using</span> System.Workflow.ComponentModel.Compiler </span>to
the top of the class and make it inherit from ActivityValidator. The one function
that we need to override is the validate function. The validate function takes in
a ValidationManager and an object. The object is what we will be using as this is
the instance of our activity that we will be validating. The function returns a ValidationErrorCollection
which is simply a collection of error messages that we will send back after validating
the activity. In the override for this function we check that both the InputString
and the RegExpression properties are not null or empty. If they are we add a corresponding
error message to the collection. Because the error is associated with a property we
provide the property name as the last parameter in the constructor for the ValidationError
object so that the designer can associate the error message with the apporpriate property.<span style="font-family:Courier New; font-size:10pt"></span></p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">class</span>
            <span style="color:#2b91af">RegExValidator</span> : <span style="color:#2b91af">ActivityValidator </span></span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">override</span>
            <span style="color:#2b91af">ValidationErrorCollection</span> Validate(<span style="color:#2b91af">ValidationManager</span> manager, <span style="color:blue">object</span> obj) </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:#2b91af">RegEx</span> myRegEx
= obj <span style="color:blue">as</span><span style="color:#2b91af">RegEx</span>; </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:#2b91af">ValidationErrorCollection</span> myErrors
= <span style="color:blue">new</span><span style="color:#2b91af">ValidationErrorCollection</span>(); </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">if</span> (myRegEx
!= <span style="color:blue">null</span> &amp;&amp; myRegEx.Parent != <span style="color:blue">null</span>) </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">if</span> (myRegEx.InputString
== <span style="color:blue">null</span> || myRegEx.InputString.Equals(<span style="color:#2b91af">String</span>.Empty)) </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> myErrors.Add(<span style="color:blue">new</span><span style="color:#2b91af">ValidationError</span>(<span style="color:#a31515">"InputString
is required."</span>, 101, <span style="color:blue">false</span>, <span style="color:#a31515">"InputString"</span>)); </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">if</span> (myRegEx.RegExpression
== <span style="color:blue">null</span> || myRegEx.RegExpression.Equals((<span style="color:#2b91af">String</span>.Empty))) </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> myErrors.Add(<span style="color:blue">new</span><span style="color:#2b91af">ValidationError</span>(<span style="color:#a31515">"RegExpression
is required."</span>, 102, <span style="color:blue">false</span>, <span style="color:#a31515">"RegExpression"</span>)); </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">return</span> myErrors; </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
          </span>That is all we need
to do for the validator class. The last thing we need to do is associate our validator
with the activity. To do this we add the same using statement to our RegEx.cs activity
(<span style="font-family:Courier New; font-size:10pt"><span style="color:blue">using</span> System.Workflow.ComponentModel.Compiler</span>)
and we add an attribute to the top of the class. 
</p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> [<span style="color:#2b91af">ActivityValidator</span>(<span style="color:blue">typeof</span>(<span style="color:#2b91af">RegExValidator</span>))] </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">partial</span>
            <span style="color:blue">class</span>
            <span style="color:#2b91af">RegEx</span>: <span style="color:#2b91af">BaseActivity </span></span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> … </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> }</span>
        </p>
        <p>
That's it. If we now use the original sample workflow I created and blank out the
InputString and RegExpression properties we will get several indications that there
are errors. First in the designer it will be indicated directly on the activity. 
</p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/122907_0256_AddingValid2.png" alt="" />
        </p>
        <p>
In the properties window it will also indicate each individual property that has an
error. 
</p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/122907_0256_AddingValid3.png" alt="" />
        </p>
        <p>
Finally the build will error and the error messages will be provided in the error
window. 
</p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/122907_0256_AddingValid4.png" alt="" />
        </p>
        <p>
        </p>
        <p>
That's all I have to say about validation. Obviously the validation can be much more
complicated than just checking if the property has been provided, but this example
hopefully provides a basic idea of how to get things going. Feel free to post a comment
if you found this helpful and would like for me to dedicate more postings to the wonderful
world of workflow. Now I'm off to go find a life since it appears that I am blogging
on a Friday night. Unfortunately that's not something you can Google for!
</p>
        <img width="0" height="0" src="http://weblog.ctrlalt313373.com/aggbug.ashx?id=5895c3e5-8465-4d3f-abdc-555fcfa4160d" />
      </body>
      <title>Adding Validation to a Custom Workflow Activity</title>
      <guid isPermaLink="false">http://weblog.ctrlalt313373.com/PermaLink,guid,5895c3e5-8465-4d3f-abdc-555fcfa4160d.aspx</guid>
      <link>http://weblog.ctrlalt313373.com/2007/12/29/AddingValidationToACustomWorkflowActivity.aspx</link>
      <pubDate>Sat, 29 Dec 2007 02:54:49 GMT</pubDate>
      <description>&lt;p&gt;
I'm my last &lt;a href="http://weblog.ctrlalt313373.com/2007/12/27/CreatingARegExActivityForWindowsWorkflow.aspx"&gt;posting&lt;/a&gt; I
walked through how to create a custom regex activity in Windows Workflow and I thought
a good next step would be to give a short example of how you would add validation
to this custom activity. In this particular instance we want to make the InputString
property and the RegExpression property required in order for the workflow designer
to consider the workflow that is being built a valid workflow. The first thing that
needs to be done is to add a class to the project called RegExValidator.cs. 
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/122907_0256_AddingValid1.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The next thing that needs to be done to the new class is to add &lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;using&lt;/span&gt; System.Workflow.ComponentModel.Compiler &lt;/span&gt;to
the top of the class and make it inherit from ActivityValidator. The one function
that we need to override is the validate function. The validate function takes in
a ValidationManager and an object. The object is what we will be using as this is
the instance of our activity that we will be validating. The function returns a ValidationErrorCollection
which is simply a collection of error messages that we will send back after validating
the activity. In the override for this function we check that both the InputString
and the RegExpression properties are not null or empty. If they are we add a corresponding
error message to the collection. Because the error is associated with a property we
provide the property name as the last parameter in the constructor for the ValidationError
object so that the designer can associate the error message with the apporpriate property.&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;RegExValidator&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;ActivityValidator &lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;override&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ValidationErrorCollection&lt;/span&gt; Validate(&lt;span style="color:#2b91af"&gt;ValidationManager&lt;/span&gt; manager, &lt;span style="color:blue"&gt;object&lt;/span&gt; obj) &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt; myRegEx
= obj &lt;span style="color:blue"&gt;as&lt;/span&gt; &lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:#2b91af"&gt;ValidationErrorCollection&lt;/span&gt; myErrors
= &lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ValidationErrorCollection&lt;/span&gt;(); &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;if&lt;/span&gt; (myRegEx
!= &lt;span style="color:blue"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; myRegEx.Parent != &lt;span style="color:blue"&gt;null&lt;/span&gt;) &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;if&lt;/span&gt; (myRegEx.InputString
== &lt;span style="color:blue"&gt;null&lt;/span&gt; || myRegEx.InputString.Equals(&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;.Empty)) &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; myErrors.Add(&lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ValidationError&lt;/span&gt;(&lt;span style="color:#a31515"&gt;"InputString
is required."&lt;/span&gt;, 101, &lt;span style="color:blue"&gt;false&lt;/span&gt;, &lt;span style="color:#a31515"&gt;"InputString"&lt;/span&gt;)); &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;if&lt;/span&gt; (myRegEx.RegExpression
== &lt;span style="color:blue"&gt;null&lt;/span&gt; || myRegEx.RegExpression.Equals((&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;.Empty))) &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; myErrors.Add(&lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ValidationError&lt;/span&gt;(&lt;span style="color:#a31515"&gt;"RegExpression
is required."&lt;/span&gt;, 102, &lt;span style="color:blue"&gt;false&lt;/span&gt;, &lt;span style="color:#a31515"&gt;"RegExpression"&lt;/span&gt;)); &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;return&lt;/span&gt; myErrors; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;/span&gt;That is all we need
to do for the validator class. The last thing we need to do is associate our validator
with the activity. To do this we add the same using statement to our RegEx.cs activity
(&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;using&lt;/span&gt; System.Workflow.ComponentModel.Compiler&lt;/span&gt;)
and we add an attribute to the top of the class. 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; [&lt;span style="color:#2b91af"&gt;ActivityValidator&lt;/span&gt;(&lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;RegExValidator&lt;/span&gt;))] &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;partial&lt;/span&gt; &lt;span style="color:blue"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;: &lt;span style="color:#2b91af"&gt;BaseActivity &lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; … &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; }&lt;/span&gt; 
&lt;/p&gt;
&lt;p&gt;
That's it. If we now use the original sample workflow I created and blank out the
InputString and RegExpression properties we will get several indications that there
are errors. First in the designer it will be indicated directly on the activity. 
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/122907_0256_AddingValid2.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
In the properties window it will also indicate each individual property that has an
error. 
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/122907_0256_AddingValid3.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
Finally the build will error and the error messages will be provided in the error
window. 
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/122907_0256_AddingValid4.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
That's all I have to say about validation. Obviously the validation can be much more
complicated than just checking if the property has been provided, but this example
hopefully provides a basic idea of how to get things going. Feel free to post a comment
if you found this helpful and would like for me to dedicate more postings to the wonderful
world of workflow. Now I'm off to go find a life since it appears that I am blogging
on a Friday night. Unfortunately that's not something you can Google for!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ctrlalt313373.com/aggbug.ashx?id=5895c3e5-8465-4d3f-abdc-555fcfa4160d" /&gt;</description>
      <comments>http://weblog.ctrlalt313373.com/CommentView,guid,5895c3e5-8465-4d3f-abdc-555fcfa4160d.aspx</comments>
      <category>.Net 3.0</category>
      <category>Development</category>
      <category>Windows Workflow</category>
    </item>
    <item>
      <trackback:ping>http://weblog.ctrlalt313373.com/Trackback.aspx?guid=ff2cc65e-d0c0-42d4-a618-46c7263b21d5</trackback:ping>
      <pingback:server>http://weblog.ctrlalt313373.com/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ctrlalt313373.com/PermaLink,guid,ff2cc65e-d0c0-42d4-a618-46c7263b21d5.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://weblog.ctrlalt313373.com/CommentView,guid,ff2cc65e-d0c0-42d4-a618-46c7263b21d5.aspx</wfw:comment>
      <wfw:commentRss>http://weblog.ctrlalt313373.com/SyndicationService.asmx/GetEntryCommentsRss?guid=ff2cc65e-d0c0-42d4-a618-46c7263b21d5</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
While having lunch today at Old Chicago with <a href="http://www.developernotes.com">Nick</a> we
began discussing Windows Workflow a bit and I started to realize that even though
I have been pounding away on a project for six months that is highly dependent on
WF I have yet to write a single blog post on the topic. I found the need today to
work on an activity that simply applies a regular expression against a string and
returns the value that it finds. Since this is a fairly basic activity I thought I
would go ahead and blog about the process of creating it. 
</p>
        <p>
First off you need to create a new activity: 
</p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/122707_0341_CreatingaRe1.png" alt="" />
        </p>
        <p>
        </p>
        <p>
Visual Studio 2008 will create the skeleton of an activity for us that inherits from
SequenceActivity. For my purposes I need to have it inherit from Activity so I will
go ahead and change this. The next thing we need to do is to define a dependency property
for our input string. To do this we do the following 
</p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue"> #region</span> "Dependency
Properties" </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">static</span>
            <span style="color:#2b91af">DependencyProperty</span> InputStringProperty
= <span style="color:#2b91af">DependencyProperty</span>.Register(<span style="color:#a31515">"InputString"</span>, <span style="color:blue">typeof</span>(<span style="color:#2b91af">String</span>), <span style="color:blue">typeof</span>(<span style="color:#2b91af">RegEx</span>)); </span>
        </p>
        <p>
        </p>
        <p>
          <span style="color:blue; font-family:Courier New; font-size:10pt"> #endregion </span>
        </p>
        <p>
What we are doing here is registering a dependency property called InputStringProperty
that is bound to the property InputString (which we will be creating in a second),
the property of InputString is of type string and the owner of InputString (our activity)
is of type RegEx. Now we need to define our property InputString. 
</p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue"> #region</span> "Properties" </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">string</span> InputString </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">get</span> { <span style="color:blue">return</span> ((<span style="color:#2b91af">String</span>)(<span style="color:blue">base</span>.GetValue(<span style="color:#2b91af">RegEx</span>.InputStringProperty)));
} </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">set</span> { <span style="color:blue">base</span>.SetValue(<span style="color:#2b91af">RegEx</span>.InputStringProperty, <span style="color:blue">value</span>);
} </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
        </p>
        <p>
          <span style="color:blue; font-family:Courier New; font-size:10pt"> #endregion </span>
        </p>
        <p>
The layout of this property is fairly similar to what you should be useto, but instead
of getting and setting from a private member variable you instead set and get from
the dependency property. Piece of cake right? The next thing we are going to do is
define our output parameter the same way. The only thing that we are going to do differently
with this property is to put an attribute on it of readonly. This will prevent the
user from binding this property to something since the user should only be binding
the properties on other activities to this property. 
</p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">static</span>
            <span style="color:#2b91af">DependencyProperty</span> OutputStringProperty
= <span style="color:#2b91af">DependencyProperty</span>.Register(<span style="color:#a31515">"OutputString"</span>, <span style="color:blue">typeof</span>(<span style="color:#2b91af">String</span>), <span style="color:blue">typeof</span>(<span style="color:#2b91af">RegEx</span>)); </span>
        </p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> [<span style="color:#2b91af">ReadOnly</span>(<span style="color:blue">true</span>)] </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">string</span> OutputString </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">get</span> { <span style="color:blue">return</span> ((<span style="color:#2b91af">String</span>)(<span style="color:blue">base</span>.GetValue(<span style="color:#2b91af">RegEx</span>.OutputStringProperty)));
} </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">set</span> { <span style="color:blue">base</span>.SetValue(<span style="color:#2b91af">RegEx</span>.OutputStringProperty, <span style="color:blue">value</span>);
} </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> }</span>
        </p>
        <p style="margin-left: 54pt">
        </p>
        <p style="margin-left: 54pt">
        </p>
        <p>
Though we are not complete yet, if you build this activity and drop it onto a test
workflow you will see the following in your Visual Studio Properties Window. We have
a property call input string that is bindable and a property called OutputString that
is readonly. 
</p>
        <p style="margin-left: 54pt">
        </p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/122707_0341_CreatingaRe2.png" alt="" />
        </p>
        <p>
        </p>
        <p>
The next things we need to do is to declare a property that will hold our regular
expression for the user of our activity to input. The will not be a dependency property,
but a standard property as it will not need the ability for binding. 
</p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">private</span>
            <span style="color:blue">string</span> _regexpression;</span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">public</span>
            <span style="color:blue">string</span> RegExpression </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">get</span> { <span style="color:blue">return</span> _regexpression;
} </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">set</span> {
_regexpression = <span style="color:blue">value</span>;} </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
        </p>
        <p>
If you now drop this activity onto a test workflow you will see the following on the
properties window. 
</p>
        <p>
        </p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/122707_0341_CreatingaRe3.png" alt="" />
        </p>
        <p>
        </p>
        <p>
We're almost done and all that is left to do is to implement the Execute method of
our activity which will actually evaluate our regular expression against the input
string. First off make sure you put <span style="font-family:Courier New; font-size:10pt"><span style="color:blue">using</span> System.Text.RegularExpressions </span>at
the top of the class as we will obviously be using the .Net RegEx features. Then we
just have the following function left. 
</p>
        <p>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">protected</span>
            <span style="color:blue">override</span>
            <span style="color:#2b91af">ActivityExecutionStatus</span> Execute(<span style="color:#2b91af">ActivityExecutionContext</span> executionContext) </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> { </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:#2b91af">Regex</span> myRegEx
= <span style="color:blue">new</span><span style="color:#2b91af">Regex</span>(RegExpression, <span style="color:#2b91af">RegexOptions</span>.IgnoreCase); </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> OutputString = myRegEx.Match(InputString).Value; </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt">
            <span style="color:blue">return</span>
            <span style="color:#2b91af">ActivityExecutionStatus</span>.Closed; </span>
        </p>
        <p>
          <span style="font-family:Courier New; font-size:10pt"> } </span>
        </p>
        <p>
        </p>
        <p>
The Exceute function is the meat of the activity where our processing actually happens.
We first create a new RegEx object using our RegExpression property and in this case
we are passing the option to ignore case. We then evaluate the regular expression
and place the value into our OutputString property. Finally we return with a closed
status and that's it. Just to test it out I put together a basic workflow that consists
of my activity with the InputString property set to "my email address is testperson@test.com
and of course it isn't a real email" and the RegExpression property set to "\w+([-+.']\w+)<a href="mailto:*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*@\w+([-.]\w+)*\.\w+([-.]\w+)*</a>"
which should pull out the email address. I then used a basic code activity to output
the email address to the console. 
</p>
        <p>
        </p>
        <p>
          <img src="http://weblog.ctrlalt313373.com/content/binary/122707_0341_CreatingaRe4.png" alt="" />
        </p>
        <p>
        </p>
        <p>
There you have it, a workflow activity that evaluates a regular expression against
a string. If an email address had not been found then a blank line would have been
written out to the console, but in our case we got <a href="mailto:testpersion@test.com">testpersion@test.com</a>.
Obviously in a more practical scenario you would not type the string directly into
the InputString property, but instead you would bind that property to the string property
on an activity earlier in the workflow, but you get the idea. That's it for now folks.
If you enjoyed this posting or found it helpful and would like to see me post more
on WF feel free to post a comment. 
</p>
        <img width="0" height="0" src="http://weblog.ctrlalt313373.com/aggbug.ashx?id=ff2cc65e-d0c0-42d4-a618-46c7263b21d5" />
      </body>
      <title>Creating a RegEx Activity for Windows Workflow</title>
      <guid isPermaLink="false">http://weblog.ctrlalt313373.com/PermaLink,guid,ff2cc65e-d0c0-42d4-a618-46c7263b21d5.aspx</guid>
      <link>http://weblog.ctrlalt313373.com/2007/12/27/CreatingARegExActivityForWindowsWorkflow.aspx</link>
      <pubDate>Thu, 27 Dec 2007 03:39:10 GMT</pubDate>
      <description>&lt;p&gt;
While having lunch today at Old Chicago with &lt;a href="http://www.developernotes.com"&gt;Nick&lt;/a&gt; we
began discussing Windows Workflow a bit and I started to realize that even though
I have been pounding away on a project for six months that is highly dependent on
WF I have yet to write a single blog post on the topic. I found the need today to
work on an activity that simply applies a regular expression against a string and
returns the value that it finds. Since this is a fairly basic activity I thought I
would go ahead and blog about the process of creating it. 
&lt;/p&gt;
&lt;p&gt;
First off you need to create a new activity: 
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/122707_0341_CreatingaRe1.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Visual Studio 2008 will create the skeleton of an activity for us that inherits from
SequenceActivity. For my purposes I need to have it inherit from Activity so I will
go ahead and change this. The next thing we need to do is to define a dependency property
for our input string. To do this we do the following 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt; #region&lt;/span&gt; "Dependency
Properties" &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt; InputStringProperty
= &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt;.Register(&lt;span style="color:#a31515"&gt;"InputString"&lt;/span&gt;, &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;), &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;)); &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:blue; font-family:Courier New; font-size:10pt"&gt; #endregion &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
What we are doing here is registering a dependency property called InputStringProperty
that is bound to the property InputString (which we will be creating in a second),
the property of InputString is of type string and the owner of InputString (our activity)
is of type RegEx. Now we need to define our property InputString. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt; #region&lt;/span&gt; "Properties" &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;string&lt;/span&gt; InputString &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;get&lt;/span&gt; { &lt;span style="color:blue"&gt;return&lt;/span&gt; ((&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;)(&lt;span style="color:blue"&gt;base&lt;/span&gt;.GetValue(&lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;.InputStringProperty)));
} &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;set&lt;/span&gt; { &lt;span style="color:blue"&gt;base&lt;/span&gt;.SetValue(&lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;.InputStringProperty, &lt;span style="color:blue"&gt;value&lt;/span&gt;);
} &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:blue; font-family:Courier New; font-size:10pt"&gt; #endregion &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
The layout of this property is fairly similar to what you should be useto, but instead
of getting and setting from a private member variable you instead set and get from
the dependency property. Piece of cake right? The next thing we are going to do is
define our output parameter the same way. The only thing that we are going to do differently
with this property is to put an attribute on it of readonly. This will prevent the
user from binding this property to something since the user should only be binding
the properties on other activities to this property. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt; OutputStringProperty
= &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt;.Register(&lt;span style="color:#a31515"&gt;"OutputString"&lt;/span&gt;, &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;), &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;)); &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; [&lt;span style="color:#2b91af"&gt;ReadOnly&lt;/span&gt;(&lt;span style="color:blue"&gt;true&lt;/span&gt;)] &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;string&lt;/span&gt; OutputString &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;get&lt;/span&gt; { &lt;span style="color:blue"&gt;return&lt;/span&gt; ((&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;)(&lt;span style="color:blue"&gt;base&lt;/span&gt;.GetValue(&lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;.OutputStringProperty)));
} &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;set&lt;/span&gt; { &lt;span style="color:blue"&gt;base&lt;/span&gt;.SetValue(&lt;span style="color:#2b91af"&gt;RegEx&lt;/span&gt;.OutputStringProperty, &lt;span style="color:blue"&gt;value&lt;/span&gt;);
} &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; }&lt;/span&gt; 
&lt;/p&gt;
&lt;p style="margin-left: 54pt"&gt;
&lt;/p&gt;
&lt;p style="margin-left: 54pt"&gt;
&lt;/p&gt;
&lt;p&gt;
Though we are not complete yet, if you build this activity and drop it onto a test
workflow you will see the following in your Visual Studio Properties Window. We have
a property call input string that is bindable and a property called OutputString that
is readonly. 
&lt;/p&gt;
&lt;p style="margin-left: 54pt"&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/122707_0341_CreatingaRe2.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The next things we need to do is to declare a property that will hold our regular
expression for the user of our activity to input. The will not be a dependency property,
but a standard property as it will not need the ability for binding. 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;private&lt;/span&gt; &lt;span style="color:blue"&gt;string&lt;/span&gt; _regexpression;&lt;/span&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;public&lt;/span&gt; &lt;span style="color:blue"&gt;string&lt;/span&gt; RegExpression &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;get&lt;/span&gt; { &lt;span style="color:blue"&gt;return&lt;/span&gt; _regexpression;
} &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;set&lt;/span&gt; {
_regexpression = &lt;span style="color:blue"&gt;value&lt;/span&gt;;} &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
If you now drop this activity onto a test workflow you will see the following on the
properties window. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/122707_0341_CreatingaRe3.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
We're almost done and all that is left to do is to implement the Execute method of
our activity which will actually evaluate our regular expression against the input
string. First off make sure you put &lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;using&lt;/span&gt; System.Text.RegularExpressions &lt;/span&gt;at
the top of the class as we will obviously be using the .Net RegEx features. Then we
just have the following function left. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt;&lt;span style="color:blue"&gt;protected&lt;/span&gt; &lt;span style="color:blue"&gt;override&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ActivityExecutionStatus&lt;/span&gt; Execute(&lt;span style="color:#2b91af"&gt;ActivityExecutionContext&lt;/span&gt; executionContext) &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; { &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:#2b91af"&gt;Regex&lt;/span&gt; myRegEx
= &lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Regex&lt;/span&gt;(RegExpression, &lt;span style="color:#2b91af"&gt;RegexOptions&lt;/span&gt;.IgnoreCase); &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; OutputString = myRegEx.Match(InputString).Value; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; &lt;span style="color:blue"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ActivityExecutionStatus&lt;/span&gt;.Closed; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-family:Courier New; font-size:10pt"&gt; } &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The Exceute function is the meat of the activity where our processing actually happens.
We first create a new RegEx object using our RegExpression property and in this case
we are passing the option to ignore case. We then evaluate the regular expression
and place the value into our OutputString property. Finally we return with a closed
status and that's it. Just to test it out I put together a basic workflow that consists
of my activity with the InputString property set to "my email address is testperson@test.com
and of course it isn't a real email" and the RegExpression property set to "\w+([-+.']\w+)&lt;a href="mailto:*@\w+([-.]\w+)*\.\w+([-.]\w+)*"&gt;*@\w+([-.]\w+)*\.\w+([-.]\w+)*&lt;/a&gt;"
which should pull out the email address. I then used a basic code activity to output
the email address to the console. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://weblog.ctrlalt313373.com/content/binary/122707_0341_CreatingaRe4.png" alt="" /&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
There you have it, a workflow activity that evaluates a regular expression against
a string. If an email address had not been found then a blank line would have been
written out to the console, but in our case we got &lt;a href="mailto:testpersion@test.com"&gt;testpersion@test.com&lt;/a&gt;.
Obviously in a more practical scenario you would not type the string directly into
the InputString property, but instead you would bind that property to the string property
on an activity earlier in the workflow, but you get the idea. That's it for now folks.
If you enjoyed this posting or found it helpful and would like to see me post more
on WF feel free to post a comment. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ctrlalt313373.com/aggbug.ashx?id=ff2cc65e-d0c0-42d4-a618-46c7263b21d5" /&gt;</description>
      <comments>http://weblog.ctrlalt313373.com/CommentView,guid,ff2cc65e-d0c0-42d4-a618-46c7263b21d5.aspx</comments>
      <category>.Net 3.0</category>
      <category>Development</category>
      <category>Windows Workflow</category>
    </item>
  </channel>
</rss>