Sie sind auf Seite 1von 8

Lab 2: The Virtual Doughnut

Factory
In this lab, you will create the user interface for a Virtual
Doughnut Factory application in 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.

Before You Begin


Microsoft comment: There are no prerequisites to
completing this lab.
Note: You should hands-on all the labs. If you have
questions for some definitions/meanings, use F1 or
help to see MSDN documentation. Our labs are
simulation of the real industrial/commercial
environment: you have to find something by
yourself! Of course, you can get help from one of
the 3 teachers here.
Microsoft Estimated lab time: 60 minutes

Exercise 2.1: Creating the User Interface


In this exercise, you will create the user interface by adding
controls to the Joe's Doughnut Factory inventory form.
To create a new project

1. Select C# as your preference. Open a new Windows Application


project and name it <ID2>'s Doughnut Factory.
2. The project is opened with one form. In the Solution Explorer,
click Form1. The Properties window displays the File properties
for Form1. Change the File Name property to frm<ID2>Main.
3. Set the properties listed in Table 2.2.
Table 2.2 Properties of the Initial Form
Objec Propert
Value
t
y
Form1 Name
Frm<ID2>Main

Text

<ID2> Doughnut Factory

Size
480, 400 (approximately)
4. Locate static void Main() and change it so that it reads as
follows:
static void Main()
{
Application.Run(new frm<ID2>Main());
}
5. Add the controls listed in Table 2.3 to the form.
Table 2.3 Controls and Properties for frm<ID2>Main
Control

Label1

Control
Type
Label

Property

Value

Name

lblTitle

Text

Current Inventory

Font.Size 14

Label2

Label

Label3

Label

Label4

Label

Label5

Label

Font.Bold True
Name
lblRaised
Text
Raised
Name
lblCake
Text
Cake
Name
lblFilled
Text
Filled
Name
lblGlazedRaised
Text
Glazed

Control

Control
Type
TextBox1 TextBox

Label6

Label

TextBox2 TextBox

Label7

Label

TextBox3 TextBox

Label8

Label

TextBox4 TextBox

Property

Value

Name

txtGlazedRaised

Text

<blank>

ReadOnly
Name
Text
Name

True
lblSugarRaised
Sugar
txtSugarRaised

Text

<blank>

ReadOnly
Name
Text
Name

True
lblChocolateRaised
Chocolate
txtChocolateRaised

Text

<blank>

ReadOnly
Name
Text
Name

True
lblPlainCake
Plain
txtPlainCake

Text

<blank>

ReadOnly True
Label9

Label

TextBox5 TextBox

Label10

Label

TextBox6 TextBox

Label11

Label

Name
Text
Name

lblChocolateCake
Chocolate
txtChocolateCake

Text

<blank>

ReadOnly
Name
Text
Name

True
lblSugarCake
Sugar
txtSugarCake

Text

<blank>

ReadOnly True
Name
lblLemonFilled

Control

Control
Type

TextBox7 TextBox

Label12

Label

TextBox8 TextBox

Label13

Label

TextBox9 TextBox

Label14

Label

Label15

Label

Label16

Label

Label17

Label

TextBox10 TextBox
ComboBox1 ComboBox
TextBox11 TextBox

ListBox1 ListBox
Label18 Label
TextBox12 TextBox

Property

Value

Text
Name

Lemon
txtLemonFilled

Text

<blank>

ReadOnly
Name
Text
Name

True
lblGrapeFilled
Grape
txtGrapeFilled

Text

<blank>

ReadOnly
Name
Text
Name

True
lblCustardFilled
Custard
txtCustardFilled

Text

<blank>

ReadOnly
Name
Text
Name
Text
Name
Text
Name
Text
Name
Text
Name
Text
Name

True
lblSale
Current Sale
lblQuantity
Quantity
lblType
Type
lblPrice
Price
txtQuantity
0
cmbType
<blank>
txtPrice

Text

<blank>

ReadOnly
Name
Name
Text
Name

True
lstSale
lblTotal
Total
txtTotal

Control

Control
Type

Property

Text

Value

<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 completed, your form should look something like Figure on
my ftp site named lab2.bmp.

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.4.
Indented items are subitems of the item in the preceding step.
Table 2.4 Menu Item Properties

Menu Item
&File
E&xit
&Doughnuts
&Make
&Remove Stale
&Customers
&Add a Customer
&View Customers
&Help
&About
&Contents

Name
mnuFile
mnuExit
mnuDoughnuts
mnuMake
mnuRemoveStale
mnuCustomers
mnuAddaCustomer
mnuViewCustomers
mnuHelp
mnuAbout
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:
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 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, "");
}

Das könnte Ihnen auch gefallen