Task 2: Create the Simple Order Form Windows FormĀ
In this task, you create the host application that is used in the tutorial. The application is a simple order form that uses Windows Forms for its user interface. It enables users to select an item from a list and add a quantity value.
After you perform this task, you will have an application that supports this functionality. Throughout the rest of the tutorial, you will add the remaining code that is required to invoke and communicate with the state machine workflow.
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 proceeding to the steps shown in the following section. |
Creating the Windows Form Source Code File
Follow these steps to create the order form and add the code to the host application.
To create the Windows Form source code file
In your project's directory, create a new file named Program.
Give the file a .cs extension if you are creating a C# application, or a .vb extension if you are creating a Visual Basic application.
In your main project file (StateMachineWorkflow), insert a new ItemGroup element before the Import element at the end of the file.
In the ItemGroup element, add a new Compile element.
Add a new attribute to the Compile element named Include using the file name that you created in step 1 as the attribute's value.
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="Program.vb"> <SubType>Form</SubType> </Compile> </ItemGroup>
<ItemGroup> <Compile Include="Program.cs"> <SubType>Form</SubType> </Compile> </ItemGroup>
To add the Windows Form code to the host application
In the Program source code file, add the following code for the Windows Form application.
Imports Microsoft.VisualBasic Imports System Imports System.Collections.Generic Imports System.Text Imports System.Threading Imports System.Windows.Forms Imports System.Workflow.Runtime Imports System.Workflow.Runtime.Hosting Imports System.Workflow.Activities Namespace Microsoft.Samples.Workflow.Tutorials.StateMachineWorkflow Public Class MainForm : Inherits Form Private label1 As System.Windows.Forms.Label Private itemsList As System.Windows.Forms.ComboBox Private label2 As System.Windows.Forms.Label Private itemQuantity As System.Windows.Forms.NumericUpDown Private WithEvents submitButton As System.Windows.Forms.Button Private groupBox1 As System.Windows.Forms.GroupBox Private WithEvents ordersIdList As System.Windows.Forms.ComboBox Private label3 As System.Windows.Forms.Label Private orderStatus As System.Windows.Forms.TextBox Private label4 As System.Windows.Forms.Label Private components As System.ComponentModel.IContainer = Nothing Private orderHistory As Dictionary(Of String, List(Of String)) Private inventoryItems As String() = _ {"Apple", "Orange", "Banana", "Pear", "Watermelon", "Grapes"} Private Delegate Sub ItemStatusUpdateDelegate _ (ByVal orderId As Guid, ByVal newStatus As String) Public Sub New() InitializeComponent() ' Initialize the inventory items list For Each item As String In Me.inventoryItems Me.itemsList.Items.Add(item) Next item Me.itemsList.SelectedIndex = 0 ' Initialize the order history collection Me.orderHistory = New Dictionary(Of String, List(Of String)) End Sub Private Function GetOrderHistory(ByVal orderId As String) As String ' Retrieve the order status Dim itemHistory As StringBuilder = New StringBuilder() For Each status As String In Me.orderHistory(orderId) itemHistory.Append(status) itemHistory.Append(Environment.NewLine) Next status Return itemHistory.ToString() End Function Private Sub submitButton_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles submitButton.Click End Sub Private Sub ordersIdList_SelectedIndexChanged(ByVal sender As Object, _ ByVal e As EventArgs) Handles ordersIdList.SelectedIndexChanged Me.orderStatus.Text = GetOrderHistory(Me.ordersIdList.SelectedItem.ToString()) 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.label1 = New System.Windows.Forms.Label() Me.itemsList = New System.Windows.Forms.ComboBox() Me.label2 = New System.Windows.Forms.Label() Me.itemQuantity = New System.Windows.Forms.NumericUpDown() Me.submitButton = New System.Windows.Forms.Button() Me.groupBox1 = New System.Windows.Forms.GroupBox() Me.orderStatus = New System.Windows.Forms.TextBox() Me.label4 = New System.Windows.Forms.Label() Me.ordersIdList = New System.Windows.Forms.ComboBox() Me.label3 = New System.Windows.Forms.Label() CType(Me.itemQuantity, System.ComponentModel.ISupportInitialize).BeginInit() Me.groupBox1.SuspendLayout() Me.SuspendLayout() ' ' label1 ' Me.label1.AutoSize = True Me.label1.Location = New System.Drawing.Point(12, 9) Me.label1.Name = "label1" Me.label1.Size = New System.Drawing.Size(23, 13) Me.label1.TabIndex = 0 Me.label1.Text = "Item" ' ' Me.itemsList ' Me.itemsList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList Me.itemsList.FormattingEnabled = True Me.itemsList.Location = New System.Drawing.Point(13, 26) Me.itemsList.Name = "itemsList" Me.itemsList.Size = New System.Drawing.Size(161, 21) Me.itemsList.TabIndex = 0 ' ' label2 ' Me.label2.AutoSize = True Me.label2.Location = New System.Drawing.Point(12, 50) Me.label2.Name = "label2" Me.label2.Size = New System.Drawing.Size(42, 13) Me.label2.TabIndex = 2 Me.label2.Text = "Quantity" ' ' Me.itemQuantity ' Me.itemQuantity.Location = New System.Drawing.Point(13, 67) Me.itemQuantity.Minimum = New Decimal(New Integer() {1, 0, 0, 0}) Me.itemQuantity.Name = "itemQuantity" Me.itemQuantity.Size = New System.Drawing.Size(80, 20) Me.itemQuantity.TabIndex = 1 Me.itemQuantity.Value = New Decimal(New Integer() {1, 0, 0, 0}) ' ' submitButton ' Me.submitButton.Location = New System.Drawing.Point(99, 64) Me.submitButton.Name = "submitButton" Me.submitButton.Size = New System.Drawing.Size(100, 23) Me.submitButton.TabIndex = 2 Me.submitButton.Text = "Submit Order" ' ' groupBox1 ' Me.groupBox1.Controls.Add(Me.orderStatus) Me.groupBox1.Controls.Add(Me.label4) Me.groupBox1.Controls.Add(Me.ordersIdList) Me.groupBox1.Controls.Add(Me.label3) Me.groupBox1.Location = New System.Drawing.Point(13, 93) Me.groupBox1.Name = "groupBox1" Me.groupBox1.Size = New System.Drawing.Size(247, 193) Me.groupBox1.TabIndex = 3 Me.groupBox1.TabStop = False Me.groupBox1.Text = "Order Status" ' ' Me.orderStatus ' Me.orderStatus.Location = New System.Drawing.Point(7, 82) Me.orderStatus.Multiline = True Me.orderStatus.Name = "orderStatus" Me.orderStatus.ReadOnly = True Me.orderStatus.Size = New System.Drawing.Size(230, 105) Me.orderStatus.TabIndex = 3 ' ' label4 ' Me.label4.AutoSize = True Me.label4.Location = New System.Drawing.Point(8, 65) Me.label4.Name = "label4" Me.label4.Size = New System.Drawing.Size(35, 13) Me.label4.TabIndex = 2 Me.label4.Text = "History" ' ' Me.ordersIdList ' Me.ordersIdList.DropDownStyle = _ System.Windows.Forms.ComboBoxStyle.DropDownList Me.ordersIdList.FormattingEnabled = True Me.ordersIdList.Location = New System.Drawing.Point(7, 37) Me.ordersIdList.Name = "ordersIdList" Me.ordersIdList.Size = New System.Drawing.Size(230, 21) Me.ordersIdList.TabIndex = 0 ' ' label3 ' Me.label3.AutoSize = True Me.label3.Location = New System.Drawing.Point(7, 20) Me.label3.Name = "label3" Me.label3.Size = New System.Drawing.Size(41, 13) Me.label3.TabIndex = 0 Me.label3.Text = "Order Id" ' ' MainForm ' Me.AcceptButton = Me.submitButton Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(272, 298) Me.Controls.Add(Me.groupBox1) Me.Controls.Add(Me.submitButton) Me.Controls.Add(Me.itemQuantity) Me.Controls.Add(Me.label2) Me.Controls.Add(Me.itemsList) Me.Controls.Add(Me.label1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "MainForm" Me.Text = "Simple Order Form" CType(Me.itemQuantity, System.ComponentModel.ISupportInitialize).EndInit() Me.groupBox1.ResumeLayout(False) Me.groupBox1.PerformLayout() 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.Text; using System.Threading; using System.Windows.Forms; using System.Workflow.Runtime; using System.Workflow.Runtime.Hosting; using System.Workflow.Activities; namespace Microsoft.Samples.Workflow.Tutorials.StateMachineWorkflow { public class MainForm : Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.ComboBox itemsList; private System.Windows.Forms.Label label2; private System.Windows.Forms.NumericUpDown itemQuantity; private System.Windows.Forms.Button submitButton; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.ComboBox ordersIdList; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox orderStatus; private System.Windows.Forms.Label label4; private System.ComponentModel.IContainer components = null; private Dictionary<string, List<string>> orderHistory; private string[] inventoryItems = { "Apple", "Orange", "Banana", "Pear", "Watermelon", "Grapes" }; private delegate void ItemStatusUpdateDelegate(Guid orderId, string newStatus); public MainForm() { InitializeComponent(); // Initialize the inventory items list foreach (string item in this.inventoryItems) { this.itemsList.Items.Add(item); } this.itemsList.SelectedIndex = 0; // Initialize the order history collection this.orderHistory = new Dictionary<string, List<string>>(); } private string GetOrderHistory(string orderId) { // Retrieve the order status StringBuilder itemHistory = new StringBuilder(); foreach (string status in this.orderHistory[orderId]) { itemHistory.Append(status); itemHistory.Append(Environment.NewLine); } return itemHistory.ToString(); } private void submitButton_Click(object sender, EventArgs e) { } private void ordersIdList_SelectedIndexChanged(object sender, EventArgs e) { this.orderStatus.Text = GetOrderHistory(this.ordersIdList.SelectedItem.ToString()); } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.itemsList = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); this.itemQuantity = new System.Windows.Forms.NumericUpDown(); this.submitButton = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.orderStatus = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.ordersIdList = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.itemQuantity)).BeginInit(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 9); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(23, 13); this.label1.TabIndex = 0; this.label1.Text = "Item"; // // this.itemsList // this.itemsList.DropDownStyle = ComboBoxStyle.DropDownList; this.itemsList.FormattingEnabled = true; this.itemsList.Location = new System.Drawing.Point(13, 26); this.itemsList.Name = "itemsList"; this.itemsList.Size = new System.Drawing.Size(161, 21); this.itemsList.TabIndex = 0; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 50); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(42, 13); this.label2.TabIndex = 2; this.label2.Text = "Quantity"; // // this.itemQuantity // this.itemQuantity.Location = new System.Drawing.Point(13, 67); this.itemQuantity.Minimum = new decimal(new int[] { 1, 0, 0, 0}); this.itemQuantity.Name = "itemQuantity"; this.itemQuantity.Size = new System.Drawing.Size(80, 20); this.itemQuantity.TabIndex = 1; this.itemQuantity.Value = new decimal(new int[] { 1, 0, 0, 0}); // // this.submitButton // this.submitButton.Location = new System.Drawing.Point(99, 64); this.submitButton.Name = "submitButton"; this.submitButton.Size = new System.Drawing.Size(100, 23); this.submitButton.TabIndex = 2; this.submitButton.Text = "Submit Order"; this.submitButton.Click += new System.EventHandler(this.submitButton_Click); // // groupBox1 // this.groupBox1.Controls.Add(this.orderStatus); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.ordersIdList); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Location = new System.Drawing.Point(13, 93); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(247, 193); this.groupBox1.TabIndex = 3; this.groupBox1.TabStop = false; this.groupBox1.Text = "Order Status"; // // orderStatus // this.orderStatus.Location = new System.Drawing.Point(7, 82); this.orderStatus.Multiline = true; this.orderStatus.Name = "orderStatus"; this.orderStatus.ReadOnly = true; this.orderStatus.Size = new System.Drawing.Size(230, 105); this.orderStatus.TabIndex = 3; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(8, 65); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(35, 13); this.label4.TabIndex = 2; this.label4.Text = "History"; // // ordersIdList // this.ordersIdList.DropDownStyle = ComboBoxStyle.DropDownList; this.ordersIdList.FormattingEnabled = true; this.ordersIdList.Location = new System.Drawing.Point(7, 37); this.ordersIdList.Name = "ordersIdList"; this.ordersIdList.Size = new System.Drawing.Size(230, 21); this.ordersIdList.TabIndex = 0; this.ordersIdList.SelectedIndexChanged += new System.EventHandler(this.ordersIdList_SelectedIndexChanged); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(7, 20); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 13); this.label3.TabIndex = 0; this.label3.Text = "Order Id"; // // 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(272, 298); this.Controls.Add(this.groupBox1); this.Controls.Add(this.submitButton); this.Controls.Add(this.itemQuantity); this.Controls.Add(this.label2); this.Controls.Add(this.itemsList); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "MainForm"; this.Text = "Simple Order Form"; ((System.ComponentModel.ISupportInitialize)(this.itemQuantity)).EndInit(); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } } static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new MainForm()); } } }
Compiling the Code
Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.
Go to the source directory of the tutorial.
At the command prompt, type MSBUILD to build the project.
In the next exercise, Exercise 2: Create the Simple Order Form Application, you will define an interface that is used to enable communication between the state machine workflow and the Simple Order Form application.
See Also
Concepts
Other Resources
Exercise 2: Create the Simple Order Form Application
Tutorial: Create a State Machine Workflow
Ordering State Machine
Simple State Machine
Send comments about this topic to Microsoft.