Lab 2: The Virtual Doughnut Factory

Lab 2: The Virtual Doughnut Factory

In this lab, you will create the user interface for a Virtual Doughnut Factory application in either Visual Basic .NET or Visual C#. Your application will serve as a storefront and control panel for an automated doughnut factory. You will add controls, menus, and implement validation. The solution to this lab is available on the Supplemental Course Materials CD-ROM in the \Labs\Ch02\Solution folder.

Before You Begin

There are no prerequisites to completing this lab.

Estimated lesson time: 60 minutes

Exercise 2.1: Creating the User Interface

In this exercise, you will create the user interface by adding controls to the Virtual Doughnut Factory inventory form.

To create a new project

  1. Select Visual Basic .NET or Visual C# as your preference. Open a new Windows Application project, and name it Virtual Doughnut Factory.

  2. The project is opened with one form. In Solution Explorer, click Form1. The Properties window displays the File properties for Form1. Change the File Name property to frmMain.vb or frmMain.cs as appropriate.

  3. Set the properties listed in Table 2.5.

    Table 2-5. Properties of the Initial Form

    Object

    Property

    Value

    Form1

    Name

    frmMain

    Text

    Virtual Doughnut Factory

    Size

    480, 400 (approximately)

    Virtual Doughnut Factory (Visual Basic .NET only)

    Startup Object

    frmMain

  4. If you are using Visual C#, locate static void Main() and change it so that it reads as follows:

    Visual C#

    static void Main() { Application.Run(new frmMain()); }

  5. Add the controls listed in Table 2.6 to the form.

    Table 2-6. Controls and Properties for frmMain

    Control

    Control Type

    Property

    Value

    Label1

    Label

    Name

    lblTitle

    Text

    Current Inventory

    Font.Size

    14

    Font.Bold

    True

    Label2

    Label

    Name

    lblRaised

    Text

    Raised

    Label3

    Label

    Name

    lblCake

    Text

    Cake

    Label4

    Label

    Name

    lblFilled

    Text

    Filled

    Label5

    Label

    Name

    lblGlazedRaised

    Text

    Glazed

    TextBox1

    TextBox

    Name

    txtGlazedRaised

    Text

    <blank>

    ReadOnly

    True

    Label6

    Label

    Name

    lblSugarRaised

    Text

    Sugar

    TextBox2

    TextBox

    Name

    txtSugarRaised

    Text

    <blank>

    ReadOnly

    True

    Label7

    Label

    Name

    lblChocolateRaised

    Text

    Chocolate

    TextBox3

    TextBox

    Name

    txtChocolateRaised

    Text

    <blank>

    ReadOnly

    True

    Label8

    Label

    Name

    lblPlainCake

    Text

    Plain

    TextBox4

    TextBox

    Name

    txtPlainCake

    Text

    <blank>

    ReadOnly

    True

    Label9

    Label

    Name

    lblChocolateCake

    Text

    Chocolate

    TextBox5

    TextBox

    Name

    txtChocolateCake

    Text

    <blank>

    ReadOnly

    True

    Label10

    Label

    Name

    lblSugarCake

    Text

    Sugar

    TextBox6

    TextBox

    Name

    txtSugarCake

    Text

    <blank>

    ReadOnly

    True

    Label11

    Label

    Name

    lblLemonFilled

    Text

    Lemon

    TextBox7

    TextBox

    Name

    txtLemonFilled

    Text

    <blank>

    ReadOnly

    True

    Label12

    Label

    Name

    lblGrapeFilled

    Text

    Grape

    TextBox8

    TextBox

    Name

    txtGrapeFilled

    Text

    <blank>

    ReadOnly

    True

    Label13

    Label

    Name

    lblCustardFilled

    Text

    Custard

    TextBox9

    TextBox

    Name

    txtCustardFilled

    Text

    <blank>

    ReadOnly

    True

    Label14

    Label

    Name

    lblSale

    Text

    Current Sale

    Label15

    Label

    Name

    lblQuantity

    Text

    Quantity

    Label16

    Label

    Name

    lblType

    Text

    Type

    Label17

    Label

    Name

    lblPrice

    Text

    Price

    TextBox10

    TextBox

    Name

    txtQuantity

    Text

    0

    ComboBox1

    ComboBox

    Name

    cmbType

    Text

    <blank>

    TextBox11

    TextBox

    Name

    txtPrice

    Text

    <blank>

    ReadOnly

    True

    ListBox1

    ListBox

    Name

    lstSale

    Label18

    Label

    Name

    lblTotal

    Text

    Total

    TextBox12

    TextBox

    Name

    txtTotal

    Text

    <blank>

    ReadOnly

    True

    Button1

    Button

    Name

    btnAddToSale

    Text

    Add To Sale

    Button2

    Button

    Name

    btnRemoveItem

    Text

    Remove Item

    Button3

    Button

    Name

    btnCheckOut

    Text

    Check Out

  6. In the Properties window, add the following strings to the Items collection of cmbType: Raised-Glazed, Raised-Sugar, Raised-Chocolate, Cake-Plain, Cake-Chocolate, Cake-Sugar, Filled-Lemon, Filled-Grape, Filled-Custard.

    When all of the controls have been added, position them in logical groupings according to function. Figure 2.11 represents a possible layout for your controls.

    figure 2-11 the user interface with controls added.

    Figure 2-11. The user interface with controls added.

Exercise 2.2: Adding a Menu

In this exercise, you will use the MainMenu component to add a menu bar to your form and create an event handler for the Click event of the Exit menu item.

To add a menu to the project

  1. In the Toolbox, double-click the MainMenu component. A MainMenu component is added to the component tray of the designer, and a menu box is added to the form.

  2. In the menu box, type &File.

  3. A menu item is added to the menu bar, and new menu boxes are added below and to the right of the menu item. The ampersand (&) provides an access key to the menu. In the Properties window, name this menu item mnuFile.

  4. Repeat Step 2 to create the menu structure shown in Table 2.7. Indented items are subordinates of the item in the preceding step.

    Table 2-7. Menu Item Properties

    Menu Item

    Name

    &File

    mnuFile

    E&xit

    mnuExit

    &Doughnuts

    mnuDoughnuts

    &Make

    mnuMake

    &Remove Stale

    mnuRemoveStale

    &Customers

    mnuCustomers

    &Add a Customer

    mnuAddaCustomer

    &View Customers

    mnuViewCustomers

    &Help

    mnuHelp

    &About

    mnuAbout

    &Contents

    mnuContents

To create an event handler for the Exit menu item

  1. In the designer, double-click the Exit menu item. The code window opens to the mnuExit_Click event handler.

  2. Call the Close method of the form to create the event handler. For example:

    Visual Basic .NET

    Private Sub mnuExit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mneExit.Click Me.Close() End Sub

    Visual C#

    private void mnuExit_Click(object sender, System.EventArgs e) { this.Close(); }

  3. Save and test the application.

Exercise 2.3: Creating Validation Handlers

In this exercise, you will use the ErrorProvider component to implement simple validation handlers for txtQuantity. In future labs, you will add additional validation to your form.

To create validation handlers with Visual Basic .NET

  1. In the Toolbox, double-click the ErrorProvider component. An ErrorProvider is added to the component tray in the designer.

  2. Right-click the designer, and choose View Code to open the code editor. In the code editor, select txtQuantity in the left drop-down menu and KeyPress in the right drop-down menu. An empty event handler for txtQuantity_KeyPress appears in the code editor.

  3. Write code that will display an error if non-numeric characters are entered into the text box.

    Visual Basic .NET

    Private Sub txtQuantity_KeyPress(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) _ Handles txtQuantity.KeyPress If Char.IsDigit(e.KeyChar) = False Then ErrorProvider1.SetError(txtQuantity, _  "Please enter a numeric value") Else ErrorProvider1.SetError(txtQuantity, "") End If End Sub

  4. In the drop-down menu on the left, choose txtQuantity, and in the drop-down menu on the right, choose Validating. An empty event handler is added to the code editor.

  5. Add code to validate that the txtQuantity field is not empty and has no other errors on it. For example:

    Visual Basic .NET

    Private Sub txtQuantity_Validating(ByVal sender As System.Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles txtQuantity.Validating ' Tests that the textbox is not empty If txtQuantity.Text = "" Then ErrorProvider1.SetError(txtQuantity, _  "Please enter a quantity") e.Cancel = True ' Tests that there are no other errors on the textbox. ElseIf ErrorProvider1.GetError(txtQuantity) <> "" Then e.Cancel = True Else ErrorProvider1.SetError(txtQuantity, "") End If End Sub

  6. Press F5 to build and test your code.

To create validation handlers with Visual C#

  1. In the Toolbox, double-click the ErrorProvider component. An ErrorProvider is added to the component tray in the designer.

  2. In the designer, select txtQuantity. In the Properties window, press the Events button. Double-click the entry for KeyPress. An empty event handler for txtQuantity_KeyPress appears in the code editor.

  3. Write code that will display an error if non-numeric characters are entered into the text box.

    Visual C#

    private void txtQuantity_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (Char.IsDigit(e.KeyChar) == false) errorProvider1.SetError(txtQuantity,  "Please enter a numeric value"); else errorProvider1.SetError(txtQuantity, ""); }

  4. In the Solution Explorer, right-click frmMain and choose View Designer. Select txtQuantity. In the Properties window, press the Events button and double-click the entry for Validating. An empty event handler is added to the code editor.

  5. Add code to validate that the txtQuantity field is not empty and has no other errors on it. For example:

    Visual C#

    private void txtQuantity_Validating(object sender, System.ComponentModel.CancelEventArgs e) { // Tests that the textbox is not empty if (txtQuantity.Text == "") { errorProvider1.SetError(txtQuantity,  "Please enter a quantity"); e.Cancel = true; } // Tests that there are no other errors on the textbox. else if (errorProvider1.GetError(txtQuantity) != "") e.Cancel = true; else errorProvider1.SetError(txtQuantity, ""); } 

  6. Press F5 to build and test your code.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net