Vous êtes sur la page 1sur 4

CIT-110 C# Programming 1: BMI

Background:
The purpose of this assignment is to apply all that new knowledge you learned during our previous
classes. To recap we learned how to create a basic form, add controls, adjust the form layout, change
properties and code click events.
You will create a form that allows for the numeric entry of a persons Height and Weight. You will
perform some calculations to determine a persons Body Mass Index, which is an indicator of healthy
weight.
There are three Label controls
There are three TextBox controls
The form will have three buttons to calculate, clear and exit.
1. When the calculate button is clicked you will need to code the calculations to compute
the BMI of the inputted values.
2. You will need to code the clear button to clear the contents and set the focus to the
Height TextBox.
Form Layout Sample:

Grading Elements
All Form properties set as instructed
All Label properties set as instructed
All TextBox properties set as instructed
All Button properties set as instructed
Calculate Button is coded correctly, variables named properly, correct
data type selected and calculations correct
Tab order coded correctly
All Controls and Variables coded with a prefix (Hungarian Notation)
Code has comments

Possible Points
(100 total)
10
5
10
10
50
5
5
5

Detailed Instructions:
1. Create a new C# Windows Forms Application called BodyMass
2. On the form do the following:
Using the Solutions Explorer Window change the File Name from Form1.cs to
frmBodyMass.cs
Using the Properties Window change the Text property to [Your Name] Body Mass
Calculator
Verify in the Properties Window the Name property is frmBodyMass
Have the form startup in the center of the screen
3. Add 3 Label controls to the form and do the following:
The first labels Name property should be lblHeight
The second labels Name property should be lblWeight
The third labels Name property should be lblBodyMassIndex
Use Visual Studio tools to make sure the Labels are the same size, aligned left and
have equal vertical spacing
4. Add 3 TextBox controls to the form and do the following:
The first Name property should be txtHeight
The second Name property should be txtWeight
The third Name property should be txtBodyMassIndex and the ReadOnly property
true
Use Visual Studio tools to make sure the TextBoxes are the same size, aligned right
and have equal vertical spacing
5. Add three Button controls:
The first buttons Name property should btnCalculate, Text property should be
Calculate with an alternate key on the C
The second buttons Name property should btnClear,Text property should be Clear
with an alternate key on the l
The third buttons Name property should btnExit, Text property should be Exit with
an alternate key on the x
Use Visual Studio tools to make sure the Buttons are the same size, aligned to the
bottom and have equal horizontal spacing
Link the btnCalculate to the forms AcceptButton property
Link the btnExit to the forms CancelButton property
6. Set the Tab Order of the form components in this order:
txtHeight set TabIndex property to 1
txtWeight set TabIndex property to 2
txtBodyMassIndex set TabStop property to False
btnCalculate set TabIndex property to 3
btnClear set TabIndex property to 4
btnExit set TabIndex property to 5

Programming Notes/Hints
Before coding Tasks 7 through 9 below please review Course Documents -->Week 3 - Chapter 2
where you will find these postings and PowerPoints presentations that should assist you with
coding tasks below.

C# Data Type this is a detailed folder that has additional postings and items to help
you

C# Variable Names shares how to declare variables and how to name them

Data Types and Conversions details how to get "Text" that is entered into a TextBox
converted to a number for calculations

Next posting is re-enforcement on how to: Converting Numeric Data from TextBox
Control

Next Posting Converting Numeric Data to TextBox Control shares how to go from
numeric data back to a Text box.

Clearing the Contents of a TextBox Control posting shares how to code a Clear
button.

The Dog Calculator solution narrates how to code a project similar to this
assignment: http://www.youtube.com/watch?v=aJEmQxz4IiQ
New Programming Concepts used in this Assignment

How to format numbers can be found in Course Documents --> Week 5 - Chapter 4
--> C# Formatting Capabilities and there are numerous samples to assist you. You
can also consult the book.

You can use the TextBoxs SetFocus method to make the cursor gets placed into
TextBox to highlight an error or to position the cursor for user input after clearing out
the contents of a TextBox. Example code: txtHeight.SetFocus();

7. Coding of Calculate button:


Convert the txtHeight and txtWeight TextBox contents using C# Convert object to a
suitable data type that support whole number only.

Declare a new variable that is a suitable data type that can contain a decimal value that
isnt too large because it will contain a value that is has two whole number positions
and a few decimal positions such as 1.829. It will store a persons Height
mathematically converted to meters using C#s arithmetic operators.
1.Variable name should be xxxMeters. You must choose the proper data type
and replace xxx with the correct Hungarian notation
What data type did you pick? ____________________
What Hungarian Notation should you use? ___
3

2. This new variable will contain the persons height in meters. The formula is:
Height / 39.36

Declare a new variable that is a suitable data type that can contain a decimal value that
isnt too large because it will contain a value that is has no more than three whole
number positions and a few decimal positions such as 163.636. It will store a persons
Weight mathematically converted to meters using C#s arithmetic operators.
1.Variable name should be xxxKilograms. You must choose the proper data type
and replace xxx with the correct Hungarian notation
What data type did you pick? ____________________
What Hungarian Notation should you use? ___
2. This new variable will contain the persons weight in kilograms. The formula
is:
Weight / 2.2

Declare a new variable that is a suitable data type that can contain a decimal value that
isnt too large because it will contain a value that is has no more than three whole
number positions and a few decimal positions such as 36.8499. It will store the
persons Body Mass Index:
1.Variable name should be xxxBMI. You must choose the proper data type and
replace xxx with the correct Hungarian notation
What data type did you pick? ____________________
What Hungarian Notation should you use? ___
2. This new variable will contain the persons BMI and the formula is:
xxxKilograms / ( xxxMeters * xxxMeters )
Can you think of another way to multiple the xxxMeters by itself?

Convert using C#s ToString method and format the xxxBMI as a number with 2
decimal positions and put the formatted value into txtBodyMassIndex TextBox (see
Programming Notes/Hints above)

8. Code the Clear button:


Clear out the contents of all four TextBox controls and set focus to txtHeight TextBox
(see Programming Notes/Hints above)
9. Code the Exit button to exit the program. This was demonstrated in the posting Make Our
First C# GUI web tutorial: http://www.youtube.com/watch?v=H3oB0CMbSeY
10. Now if you are brave enough, calculate your BMI

Vous aimerez peut-être aussi