Compartilhar via


Task 1: Create the Sequential Workflow

Download sample

In this task, you create a Windows Form application and a basic sequential workflow that are used throughout the rest of the tutorial. A sequential workflow is useful when you want to run activities in a linear, consecutive order. The workflow created in this task does not contain any child activities, but it is a full workflow that can be hosted by the Windows Workflow Foundation runtime engine.

In the task that follows this one, you will modify the Windows Form application to start the Windows Workflow Foundation runtime engine and run the workflow that you create in this task.

Note

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 then proceeding to the steps in the following section.

Creating and Running the Windows Form Application

Create the project for the tutorial and the Windows Forms application that will contain the user interface for the remainder of the tutorial.

To create the SimpleExpenseReport project and solution

  1. In Visual Studio, click File, point to New, and click Project. If you want to create a C# project, select Visual C#, Windows, choose Windows Forms Application, and name the project SimpleExpenseReport.

  2. Click OK.

To create and run the simple expense report Windows Form application

  1. In Solution Explorer, expand Form1.cs and double-click Form1.Designer.cs to open it in the code editor pane on the left.

  2. Add the following code to the InitializeComponent method implementation to define the UI elements of your Windows Form application.

    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.result = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.submitButton = new System.Windows.Forms.Button();
        this.approvalState = new System.Windows.Forms.Label();
        this.approveButton = new System.Windows.Forms.Button();
        this.rejectButton = new System.Windows.Forms.Button();
        this.amount = new System.Windows.Forms.TextBox();
        this.panel1 = new System.Windows.Forms.Panel();
        this.panel1.SuspendLayout();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(10, 13);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(43, 13);
        this.label1.TabIndex = 1;
        this.label1.Text = "Amount";
        // 
        // result
        // 
        this.result.Location = new System.Drawing.Point(13, 70);
        this.result.Name = "result";
        this.result.ReadOnly = true;
        this.result.Size = new System.Drawing.Size(162, 20);
        this.result.TabIndex = 1;
        this.result.TabStop = false;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(10, 54);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(37, 13);
        this.label2.TabIndex = 3;
        this.label2.Text = "Result";
        // 
        // submitButton
        // 
        this.submitButton.Enabled = false;
        this.submitButton.Location = new System.Drawing.Point(56, 95);
        this.submitButton.Name = "submitButton";
        this.submitButton.Size = new System.Drawing.Size(75, 23);
        this.submitButton.TabIndex = 2;
        this.submitButton.Text = "Submit";
        this.submitButton.Click += new
            System.EventHandler(this.submitButton_Click);
        // 
        // approvalState
        // 
        this.approvalState.AutoSize = true;
        this.approvalState.Location = new System.Drawing.Point(10, 9);
        this.approvalState.Name = "approvalState";
        this.approvalState.Size = new System.Drawing.Size(49, 13);
        this.approvalState.TabIndex = 4;
        this.approvalState.Text = "Approval";
        // 
        // approveButton
        // 
        this.approveButton.Enabled = false;
        this.approveButton.Location = new System.Drawing.Point(11, 25);
        this.approveButton.Name = "approveButton";
        this.approveButton.Size = new System.Drawing.Size(75, 23);
        this.approveButton.TabIndex = 0;
        this.approveButton.Text = "Approve";
        this.approveButton.Click += new
            System.EventHandler(this.approveButton_Click);
        // 
        // rejectButton
        // 
        this.rejectButton.Enabled = false;
        this.rejectButton.Location = new System.Drawing.Point(86, 25);
        this.rejectButton.Name = "rejectButton";
        this.rejectButton.Size = new System.Drawing.Size(75, 23);
        this.rejectButton.TabIndex = 1;
        this.rejectButton.Text = "Reject";
        this.rejectButton.Click += new
            System.EventHandler(this.rejectButton_Click);
        // 
        // amount
        // 
        this.amount.Location = new System.Drawing.Point(13, 29);
        this.amount.MaxLength = 9;
        this.amount.Name = "amount";
        this.amount.Size = new System.Drawing.Size(162, 20);
        this.amount.TabIndex = 0;
        this.amount.KeyPress += new
            System.Windows.Forms.KeyPressEventHandler(this.amount_KeyPress);
        this.amount.TextChanged += new
            System.EventHandler(this.amount_TextChanged);
        // 
        // panel1
        // 
        this.panel1.Controls.Add(this.approvalState);
        this.panel1.Controls.Add(this.approveButton);
        this.panel1.Controls.Add(this.rejectButton);
        this.panel1.Location = new System.Drawing.Point(7, 124);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(172, 66);
        this.panel1.TabIndex = 8;
        // 
        // MainForm
        // 
        this.AcceptButton = this.submitButton;
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(187, 201);
        this.MinimumSize = new System.Drawing.Size(197, 155);
        this.Controls.Add(this.result);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.panel1);
        this.Controls.Add(this.amount);
        this.Controls.Add(this.submitButton);
        this.Controls.Add(this.label1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "MainForm";
        this.Text = "Simple Expense Report";
        this.panel1.ResumeLayout(false);
        this.panel1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();
    
    }
    
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox result;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Button submitButton;
    private System.Windows.Forms.Label approvalState;
    private System.Windows.Forms.Button approveButton;
    private System.Windows.Forms.Button rejectButton;
    private System.Windows.Forms.TextBox amount;
    private System.Windows.Forms.Panel panel1;
    
  3. Open Form1.cs and rename the class MainForm.

  4. Add the following code to the MainForm constructor and the various event handler methods to support UI functionality used later in this tutorial:

    public MainForm()
    {
        InitializeComponent();
    
        // Collapse approve/reject panel
        this.Height = this.MinimumSize.Height;
    }
    
    private void submitButton_Click(object sender, EventArgs e)
    {
    }
    
    private void approveButton_Click(object sender, EventArgs e)
    {
    }
    
    private void rejectButton_Click(object sender, EventArgs e)
    {
    }
    
    private void amount_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!Char.IsControl(e.KeyChar) && (!Char.IsDigit(e.KeyChar)))
            e.KeyChar = Char.MinValue;
    }
    
    private void amount_TextChanged(object sender, EventArgs e)
    {
        submitButton.Enabled = amount.Text.Length > 0;
    }
    
  5. Press F5, or click Debug, and point to Start Debugging to compile the project. It should look similar to the following figure:

    Simple Expense Report Sample Dialog Box

Defining the Sequential Workflow

To define the sequential workflow

  1. In Solution Explorer, right-click the SimpleExpenseReport solution, point to Add, and click New Project.

  2. In Project types, under Visual C#, click Workflow and select Sequential Workflow Library from the Templates pane.

  3. Name the workflow project ExpenseReportWorkflow.

    Visual Studio creates Workflow1.cs and Workflow1.Designer.cs, which provide a design surface and a code-behind file for the implementation details of your workflow. Right-click Workflow1.cs in Solution Explorer and choose Rename. Enter ExpenseReportWorkflow.cs. Click Yes to confirm. For more information about sequential workflows, see Sequential Workflows.

In Task 2: Host the Sequential Workflow, you modify the Windows Form application to start the workflow that you created in this task.

See Also

Reference

SequentialWorkflowActivity
System.Workflow.Runtime
System.Workflow.Activities
System.Workflow.Activities.Rules
System.Workflow.ComponentModel

Other Resources

Task 2: Host the Sequential Workflow

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04