Task 2: Create the Custom Activity Host ApplicationĀ 

Download sample

In this task, you create the host application that is used in this tutorial.

The application that you build for the tutorial is a simple text-based Web browser that is built using a Windows Form. The form contains a text box, which is used to enter a Web page address, and a button, which is labeled Go. When the button is clicked, the application loads and starts the sequential workflow. This workflow contains a single custom activity that performs the task of downloading the text of the Web page.

NoteNote

Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following section.

Creating the CustomActivityHost Source File

First, you must create the source code file that you will use for the rest of the tutorial.

To create the Windows Form source code file

  1. In your main project file (CustomActivity), insert a new ItemGroup element before the Import element at the end of the file.

  2. In the ItemGroup element, add a new Compile element.

  3. Add a new attribute to the Compile element named Include using the file name CustomActivityHost.cs for C# projects or CustomActivityHost.vb for Visual Basic.NET projects.

  4. Add a new child element to the Compile element named SubType.

    Give this element the value Form. Your ItemGroup node will appear as follows:

    <ItemGroup>
        <Compile Include="CustomActivityHost.vb">
            <SubType>Form</SubType>
        </Compile>
    </ItemGroup>
    
    <ItemGroup>
        <Compile Include="CustomActivityHost.cs">
            <SubType>Form</SubType>
        </Compile>
    </ItemGroup>
    

Adding Code to the Host Application

Next, you add the code for the Windows Form application.

To add the Windows Form code to the host application

  1. In your project directory, create a new file named CustomActivityHost.

    Give the file a .cs extension if you are creating a C# application, or a .vb extension for a Visual Basic application.

  2. In the CustomActivityHost source code file, add the following code for the Windows Form application.

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Windows.Forms
    Imports System.Workflow.Runtime
    
    Namespace Microsoft.Samples.Workflow.Tutorials.CustomActivity
        Public Class MainForm : Inherits Form
            Private addressCaption As System.Windows.Forms.Label
            Private address As System.Windows.Forms.TextBox
            Private data As System.Windows.Forms.TextBox
            Private WithEvents goButton As System.Windows.Forms.Button
            Private components As System.ComponentModel.IContainer = Nothing
    
            Public Sub New()
                InitializeComponent()
            End Sub
    
            Private Sub goButton_Click(ByVal sender As Object, _
                ByVal e As EventArgs) Handles goButton.Click
            End Sub
    
            Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                If disposing AndAlso (Not components Is Nothing) Then
                    components.Dispose()
                End If
                MyBase.Dispose(disposing)
            End Sub
    
            Private Sub InitializeComponent()
                Me.addressCaption = New System.Windows.Forms.Label()
                Me.address = New System.Windows.Forms.TextBox()
                Me.data = New System.Windows.Forms.TextBox()
                Me.goButton = New System.Windows.Forms.Button()
                Me.SuspendLayout()
                ' 
                ' addressCaption
                ' 
                Me.addressCaption.AutoSize = True
                Me.addressCaption.Location = New System.Drawing.Point(15, 13)
                Me.addressCaption.Name = "addressCaption"
                Me.addressCaption.Size = New System.Drawing.Size(41, 13)
                Me.addressCaption.TabIndex = 0
                Me.addressCaption.Text = "Address"
                ' 
                ' address
                ' 
                Me.address.Anchor = (CType(((System.Windows.Forms.AnchorStyles.Top Or _
                             System.Windows.Forms.AnchorStyles.Left) Or _
                             System.Windows.Forms.AnchorStyles.Right), _
                             System.Windows.Forms.AnchorStyles))
                Me.address.Location = New System.Drawing.Point(16, 28)
                Me.address.Name = "address"
                Me.address.Size = New System.Drawing.Size(430, 20)
                Me.address.TabIndex = 0
                Me.address.Text = "http://"
                ' 
                ' data
                ' 
                Me.data.Anchor = (CType((((System.Windows.Forms.AnchorStyles.Top Or _
                             System.Windows.Forms.AnchorStyles.Bottom) Or _
                             System.Windows.Forms.AnchorStyles.Left) Or _
                             System.Windows.Forms.AnchorStyles.Right), _
                             System.Windows.Forms.AnchorStyles))
                Me.data.Location = New System.Drawing.Point(15, 55)
                Me.data.Multiline = True
                Me.data.Name = "data"
                Me.data.ReadOnly = True
                Me.data.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
                Me.data.Size = New System.Drawing.Size(496, 320)
                Me.data.TabIndex = 2
                ' 
                ' goButton
                ' 
                Me.goButton.Location = New System.Drawing.Point(462, 25)
                Me.goButton.Name = "goButton"
                Me.goButton.Size = New System.Drawing.Size(49, 23)
                Me.goButton.TabIndex = 1
                Me.goButton.Text = "Go"
                Me.goButton.Anchor = CType( _
                    (System.Windows.Forms.AnchorStyles.Top Or _
                    System.Windows.Forms.AnchorStyles.Right), _
                    System.Windows.Forms.AnchorStyles)
                ' 
                ' MainForm
                ' 
                Me.AcceptButton = Me.goButton
                Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
                Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
                Me.ClientSize = New System.Drawing.Size(523, 407)
                Me.Controls.Add(Me.goButton)
                Me.Controls.Add(Me.data)
                Me.Controls.Add(Me.address)
                Me.Controls.Add(Me.addressCaption)
                Me.Name = "MainForm"
                Me.Text = "Web Tear"
                Me.ResumeLayout(False)
                Me.PerformLayout()
            End Sub
        End Class
    
        Friend Class Program
            Private Sub New()
            End Sub
            <STAThread()> _
            Shared Sub Main()
                Application.EnableVisualStyles()
                Application.Run(New MainForm())
            End Sub
        End Class
    End Namespace
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Workflow.Runtime;
    
    namespace Microsoft.Samples.Workflow.Tutorials.CustomActivity
    {
        public class MainForm : Form
        {
            private System.Windows.Forms.Label addressCaption;
            private System.Windows.Forms.TextBox address;
            private System.Windows.Forms.TextBox data;
            private System.Windows.Forms.Button goButton;
            private System.ComponentModel.IContainer components = null;
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void goButton_Click(object sender, EventArgs e)
            {
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            private void InitializeComponent()
            {
                this.addressCaption = new System.Windows.Forms.Label();
                this.address = new System.Windows.Forms.TextBox();
                this.data = new System.Windows.Forms.TextBox();
                this.goButton = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // addressCaption
                // 
                this.addressCaption.AutoSize = true;
                this.addressCaption.Location = new System.Drawing.Point(15, 13);
                this.addressCaption.Name = "addressCaption";
                this.addressCaption.Size = new System.Drawing.Size(41, 13);
                this.addressCaption.TabIndex = 0;
                this.addressCaption.Text = "Address";
                // 
                // address
                // 
                this.address.Anchor = ((System.Windows.Forms.AnchorStyles)
                    (((System.Windows.Forms.AnchorStyles.Top | 
                    System.Windows.Forms.AnchorStyles.Left) | 
                    System.Windows.Forms.AnchorStyles.Right)));
                this.address.Location = new System.Drawing.Point(16, 28);
                this.address.Name = "address";
                this.address.Size = new System.Drawing.Size(430, 20);
                this.address.TabIndex = 0;
                this.address.Text = "http://";
                // 
                // data
                // 
                this.data.Anchor = ((System.Windows.Forms.AnchorStyles)
                    ((((System.Windows.Forms.AnchorStyles.Top | 
                    System.Windows.Forms.AnchorStyles.Bottom) | 
                    System.Windows.Forms.AnchorStyles.Left) | 
                    System.Windows.Forms.AnchorStyles.Right)));
                this.data.Location = new System.Drawing.Point(15, 55);
                this.data.Multiline = true;
                this.data.Name = "data";
                this.data.ReadOnly = true;
                this.data.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                this.data.Size = new System.Drawing.Size(496, 320);
                this.data.TabIndex = 2;
                // 
                // goButton
                // 
                this.goButton.Location = new System.Drawing.Point(462, 25);
                this.goButton.Name = "goButton";
                this.goButton.Size = new System.Drawing.Size(49, 23);
                this.goButton.TabIndex = 1;
                this.goButton.Text = "Go";
                this.goButton.Anchor = ((System.Windows.Forms.AnchorStyles)
                    ((System.Windows.Forms.AnchorStyles.Top | 
                    System.Windows.Forms.AnchorStyles.Right)));
                this.goButton.Click += new System.EventHandler(this.goButton_Click);
                // 
                // MainForm
                // 
                this.AcceptButton = this.goButton;
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(523, 407);
                this.Controls.Add(this.goButton);
                this.Controls.Add(this.data);
                this.Controls.Add(this.address);
                this.Controls.Add(this.addressCaption);
                this.Name = "MainForm";
                this.Text = "Web Tear";
                this.ResumeLayout(false);
                this.PerformLayout();
            }
        }
    
        static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.Run(new MainForm());
            }
        }
    }
    

Compiling the Code

  1. Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.

  2. Go to the source directory of the tutorial.

  3. At the command prompt, type MSBUILD to build the project.

  4. Run the application in the \bin\debug folder (or the \bin folder if you are using Visual Basic .NET). Your application will appear similar to the following figure:

In Task 3: Create the Custom Activity Sequential Workflow, you will create a basic sequential workflow that you will build on later.

See Also

Concepts

Creating a Workflow Host Application

Other Resources

Task 3: Create the Custom Activity Sequential Workflow
Tutorial: Create a Custom Activity
Custom Activities

Footer image

Send comments about this topic to Microsoft.