Chapter 3
Chapter 3
Processing Data
Topics
3.1 Reading Input with TextBox Controls
3.2 A First Look at Variables
3.3 Numeric Data Type and Variables
3.4 Performing Calculations
3.5 Inputting and Outputting Numeric Values
3.6 Formatting Numbers with the ToString Method
3.7 Simple Exception Handling
3.8 Using Named Constants
3.9 Declaring Variables as Fields
3.10 Using the Math Class
3.11 More GUI Details
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.1 Reading Input with TextBox
Control
TextBox control
a rectangular area
can accept keyboard input from the user
locates in the Common Control group of the
Toolbox
double click to add it to the form
default name is textBoxn
where n is 1, 2, 3,
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
The Text Property
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.2 A First Look at Variables
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Data Types
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Variable Names
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
String Variables
productLabel = productDescription;
MessageBox.Show(productDescription);
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
String Concatenation
string message;
Message = Hello + world;
12 + apples;
Total is + 25.75;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Declaring Variables Before Using
Them
You can declare variables and use them later
private void showNameButton_Click(object sender, EventArgs e)
{
// Declare a string variable to hold the full name.
string fullName;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Local Variables and Scope
A local variable belongs to the method in which it was declared
Only statements inside that method can access the variable
Scope describes the part of a program in which a variable may be
accessed
Lifetime of a variable is the time period during which the variable
exists in memory while the program is executing
private void firstButton_Click(object sender, EventArgs e)
{
string myName;
myName = nameTextBox.Text;
}
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Rules of Variables
You can assign a value to a variable only if the value is compatible
with the variables data type
string employeeID;
employeeID = 125;
Multiple variables with the same type may be declared with one
statement
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.3 Numeric Data Types and
Variables
If you need to store a number in a variable and use the number in a
mathematical operation, the variable must be of a numeric data type
Commonly used C# numeric data types:
int: whole number in the range of -2,147,483,648 to 2,147,483,647
double: real numbers including numbers with fractional parts
Numeric literals is a number that is written into a programs code:
int hoursWorked = 40;
Or
double temperature = 87.6;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
The decimal Data Type
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Explicit Conversion with Cast
Operators
C# allows you to explicitly convert among types,
which is known as type casting
You can use the cast operator which is simply a
pair of parentheses with the type keyword in it
int wholeNumber;
decimal moneyNumber = 4500m;
wholeNumber = (int) moneynumber;
double realNUmber;
decimal moneyNUmber = 625.70m;
realNumber = (double) moneyNumber;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.4 Performing Calculations
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Rules for Performing Calculations
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Integer Division
int x = 7, y = 3;
MessageBox.Show((x / y).ToString());
int x = 7, y = 3;
MessageBox.Show(((double) x / (double) y).ToString());
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.5 Inputting and Outputting
Numeric Values
Input collected from the keyboard are considered combinations of
characters (or string literals) even if they look like a number to you
A TextBox control reads keyboard input, such as 25.65. However,
the TextBox treats it as a string, not a number.
In C#, use the following Parse methods to convert string to numeric
data types
int.Parse
double.Parse
decimal.Parse
Examples:
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Displaying Numeric Values
The Text property of a control only accepts string literals
To display a number in a TextBox or Label control requires you to convert a
numeric data to string type
In C#, all variables work with ToString method that can convert variables
values to string:
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.6 Formatting Numbers with the
ToString Method
The ToString method can optionally format a number to
appear in a specific way
The following table lists the format strings and how they
work with sample outputs
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.7 Simple Exception
Handling
An exception is an unexpected error that happens while a program is
running
If an exception is not handled by the program, the program will abruptly halt
C# allows you to write codes that responds to exceptions. Such codes are
known as exception handlers.
In C# the structure is called a try-catch statement
try { }
catch { }
The try block is where you place the statements that could have exception
The catch block is where you place statements as response to the
exception when it happens
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Throwing an Exception
In the following example, the user may entered invalid data (e.g.
null) to the milesText control. In this case, an exception happens
(which is commonly said to throw an exception).
The program then jumps to the catch block.
You can use the following try
{
to display an exceptions double miles;
default error message: double gallons;
double mpg;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.8 Using Named Constants
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.9 Declaring Variables as Fields
A field is a variable that is declared at the class level
It is declared inside the class, but not inside of any method
A field is a special type of variable public
{
partial class Form1 : Form
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.10 Using the Math Class
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
3.11 More GUI Details Tab
Order
When an application is running, one of the forms controls always
has the focus
Focus means a control receives the users keyboard input
When a button is focused, pressing the Enter key can execute the
buttons Click event handler
The order in which controls receives the focus is called the tab order
When the user presses the tab key to select controls, the program will
follow the tab order
The TabIndex property contains a numeric value indicating the
controls position in the tab order
The value starts with 0. The index of first control is 0, the nth control is
n-1.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Tab Order (Contd)
To set the tab order of a control, click Tab Order on the View menu.
This activates the tab-order selection mode on the form.
Simply click the controls with the mouse in the order you want.
Notice that Label controls do not accept input from the keyboard.
They cannot receive focus.
Their TabIndex values are irrelevant
You can use the Focus method to change the focus using the
following syntax
ControlName.Focus();
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Assign Keyboard Access Key to
Buttons
An access key (aka a mnemonic) is a key that is pressed in
combination with the Alt key to quickly access a control
You can assign an access key to a buttons Text property by adding
an ampersand (&) before a letter
E&xit.
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Setting Colors
Forms and most controls have a BackColor property
Controls that can display Text also have a ForeColor property
These color-related properties support a drop-down list of colors
The list has tree tabs:
Custom: display a color palette
Web: list colors displayed with consistency in Web browsers
System: list colors defined in current Windows
You can set colors in color
The .NET Framework provides numerous values that represent colors
messageLable.BackColor = Color.Black;
messageLable.ForeColor = Color.Yellow;
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
Background Images for Forms
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.
GroupBoxes vs. Panels
A GroupBox control is a container with a thin border and an optional
title that can hold other controls
A Panel control is also a container that can hold other controls
There are several primary differences between a Panel and
GroupBox:
A panel cannot display a title and does not have a Text property, but a
GroupBox supports these two properties.
A panels border can be specified by its BorderStyle property, while the
GroupBox cannot be
Module Code and Module Title Title of Slides Copyright 2014 Pearson Education, Inc.