Instant Download Programming in Visual C 2008 3rd Edition Julia Case Bradley PDF All Chapters
Instant Download Programming in Visual C 2008 3rd Edition Julia Case Bradley PDF All Chapters
com
https://ebookname.com/product/programming-in-
visual-c-2008-3rd-edition-julia-case-bradley-2/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookname.com/product/programming-in-visual-c-2008-3rd-
edition-julia-case-bradley/
ebookname.com
https://ebookname.com/product/programming-in-visual-basic-2008-7th-
edition-julia-case-bradley/
ebookname.com
https://ebookname.com/product/beginning-visual-c-2012-programming-1st-
edition-karli-watson/
ebookname.com
https://ebookname.com/product/the-routledge-companion-to-
sociolinguistics-carmen-llamas/
ebookname.com
https://ebookname.com/product/power-of-fate-mystic-harbor-book-1-1st-
edition-suki-williams/
ebookname.com
https://ebookname.com/product/handbook-of-firearms-and-ballistics-
brian-j-heard/
ebookname.com
https://ebookname.com/product/investigations-in-environmental-
geology-3rd-edition-duncan-d-foley/
ebookname.com
Computer and Information Technology
Programming in Visual C#
2008
Bradley−Millspaugh
McGraw-Hill
=>?
McGraw−Hill Primis
ISBN−10: 0−39−017402−5
ISBN−13: 978−0−39−017402−4
Text:
http://www.primisonline.com
Copyright ©2009 by The McGraw−Hill Companies, Inc. All rights
reserved. Printed in the United States of America. Except as
permitted under the United States Copyright Act of 1976, no part
of this publication may be reproduced or distributed in any form
or by any means, or stored in a database or retrieval system,
without prior written permission of the publisher.
Contents
Front Matter 1
Preface 1
To the Student 7
8. Arrays 339
Text 339
iii
9. Web Applications 377
Text 377
iv
Bradley−Millspaugh: Front Matter Preface © The McGraw−Hill 1
Programming in Visual C# Companies, 2010
2008
PREFACE
Approach
This text incorporates the basic concepts of programming, problem solving,
and programming logic, as well as the design techniques of an object-oriented
event-driven language.
Chapter topics are presented in a sequence that allows the programmer to
learn how to deal with a visual interface while acquiring important program-
ming skills such as creating projects with objects, decisions, loops, and data
management.
A high priority is given to writing applications that are easy for the user to
understand and use. Students are presented with interface design guidelines
throughout the text.
This text follows essentially the same sequence as the Bradley/Millspaugh
Visual Basic text. Object-oriented programming (OOP) is introduced in Chap-
ter 1 and is used consistently in every chapter of the book.
The code for all in-chapter projects is available to instructors.
v
2 Bradley−Millspaugh: Front Matter Preface © The McGraw−Hill
Programming in Visual C# Companies, 2010
2008
TEXT FEATURES
Object-Oriented Concepts Interface Design Guidelines
are presented throughout the text to offer are presented to offer students a better under-
students an introduction to object-oriented standing of meeting user needs and employing
design before learning to create their own industry standards.
classes.
Good Programming Habits
try
1. Always test the tab order on your forms. Fix it if necessary by changing
{
the TabIndex properties of the controls.
//C Convert
H A input
P T values
E R to numeric and 2. Provide visual separation for input fields and output fields and always
quantityInteger = int.Parse(quantityTex make it clear to the user which are which.
try
{
1priceDecimal = decimal.Parse(priceT
// Calculate values.
extendedPriceDecimal = quantityInte
3. Make sure that your forms can be navigated and entered from the
keyboard. Always set a default button (AcceptButton property) for
every form.
4. To make a label maintain its size regardless of the value of the Text
property, set AutoSize to false.
5. To make the text in a text box right justified or centered, set the
TextAlign property.
Introduction to
discountDecimal = Decimal.Round(
(extendedPriceDecimal * DISCOUNT
6. You can use the Checked property of a check box to set other properties
that must be true or false.
amountDueDecimal = extendedPriceDec
Programming and
totalAmountDecimal += amountDueDeci
numberTransactionsInteger++;
Visual C# 2008
// Format and display answers.
extendedPriceTextBox.Text = extende Tips
at the completion of this chapter, you will be able to . . .
1. Describe the process of visual program design and development. in the margins help students avoid potential
2. Explain the term object-oriented programming. trouble spots in their programs and encourage
3. Explain the concepts of classes, objects, properties, methods, and
events. them to develop good programming habits.
4. List and describe the three steps for writing a C# program.
viii P R E F A C E
P R E F A C E ix
x P R E F A C E
Thank You
Many people have worked very hard to design and produce this text. We would
like to thank our editors, Scott Davidson and Alaina Grayson. Our thanks also
to the many people who produced this text, including Marlena Pechan and
Betsy Blumenthal.
We greatly appreciate Robert Price and Peter van der Goes for their thor-
ough technical reviews, constructive criticism, and many valuable suggestions.
Thank you to Theresa Berry for her work on the exercise solutions. And, most
importantly, we are grateful to Dennis and Richard for their support and
understanding through the long days and busy phone lines.
The Authors
We have had fun writing about C#. We hope that this feeling is evident as you
read this book and that you will enjoy learning or teaching this outstanding
programming language.
Julia Case Bradley
Anita C. Millspaugh
Bradley−Millspaugh: Front Matter To the Student © The McGraw−Hill 7
Programming in Visual C# Companies, 2010
2008
TO THE STUDENT
The best way to learn to program in Visual C# is to do it. If you enter and run
the sample projects, you will be on your way to writing applications. Reading
the examples without trying to run them is like trying to learn a foreign lan-
guage or mathematics by just reading about it. Enter the projects, look up your
questions in the extensive MSDN Help files, and make those projects run.
Installing Visual C#
For the programs in this text, you need to install the .NET Framework v 3.5,
Visual C# 2008, and the MSDN (Microsoft Developers Network) library, which
contains all of Help and many instructive articles.
You can download the Express Edition of Visual C# and Visual Web De-
veloper from msdn.microsoft.com/express. Using these two products, you can
complete most of the exercises in this text.
xi
8 Notes
Bradley−Millspaugh: 1. Introduction to Text © The McGraw−Hill 9
Programming in Visual C# Programming and Visual Companies, 2010
2008 C# 2008
try
{
//C Convert
H A input
P T values
E R to numeric and assign
quantityInteger = int.Parse(quantityTextBox.T
try
{
1priceDecimal = decimal.Parse(priceTextBox
// Calculate values.
extendedPriceDecimal = quantityInteger *
Introduction to
discountDecimal = Decimal.Round(
(extendedPriceDecimal * DISCOUNT_RATE_
amountDueDecimal = extendedPriceDecimal -
Programming and
totalAmountDecimal += amountDueDecimal;
numberTransactionsInteger++;
Visual C# 2008
// Format and display answers.
extendedPriceTextBox.Text = extendedPrice
at the completion of this chapter, you will be able to . . .
Check box
Radio
buttons
Picture Buttons
box
Labels
Menu bar
Group box
Drop-down list
List box
Beginning in Chapter 9 you will create programs using Web Forms and
Visual Web Developer. You can run Web applications in a browser such as
Internet Explorer or Mozilla FireFox, on the Internet, or on a company intranet.
Figure 1.2 shows a Web Forms application.
You also will become acquainted with Microsoft’s new screen design tech-
nology, Windows Presentation Foundation (WPF), which is covered in Chapter
14. WPF uses its own designer and design elements, which are different from
those used for Windows forms.
Bradley−Millspaugh: 1. Introduction to Text © The McGraw−Hill 11
Programming in Visual C# Programming and Visual Companies, 2010
2008 C# 2008
C H A P T E R 1 3
Figure 1.2
Programming Languages—Procedural,
Event Driven, and Object Oriented
There are literally hundreds of programming languages. Each was developed to
solve a particular type of problem. Most traditional languages, such as BASIC,
C, COBOL, FORTRAN, PL/1, and Pascal, are considered procedural lan-
guages. That is, the program specifies the exact sequence of all operations.
Program logic determines the next instruction to execute in response to condi-
tions and user requests.
The newer programming languages such as C#, Java, and Visual Basic
(VB) use a different approach: object-oriented programming (OOP).
In the OOP model, programs are no longer procedural. They do not follow
a sequential logic. You, as the programmer, do not take control and determine
the sequence of execution. Instead, the user can press keys and click various
buttons and boxes in a window. Each user action can cause an event to occur,
which triggers a method (a set of programming statements) that you have writ-
ten. For example, the user clicks on a button labeled Calculate. The clicking
causes the button’s Click event to occur, and the program automatically jumps
to a method you have written to do the calculation.
12 Bradley−Millspaugh: 1. Introduction to Text © The McGraw−Hill
Programming in Visual C# Programming and Visual Companies, 2010
2008 C# 2008
Objects
Think of an object as a thing, or a noun. Examples of objects are forms and
controls. Forms are the windows and dialog boxes you place on the screen;
controls are the components you place inside a form, such as text boxes, but-
tons, and list boxes.
Properties
Properties tell something about or control the behavior of an object such as
its name, color, size, or location. You can think of properties as adjectives that
describe objects. TIP
When you refer to a property, you first name the object, add a period, and The term members is used to refer to
then name the property. For example, refer to the Text property of a form called both properties and methods. ■
SalesForm as SalesForm.Text (pronounced “sales form dot text”).
Methods
Actions associated with objects are called methods. Methods are the verbs of
object-oriented programming. Some typical methods are Close, Show, and
Clear . Each of the predefined objects has a set of methods that you can
use. You will learn to write additional methods to perform actions in your
programs.
You refer to methods as Object.Method (“object dot method”). For exam-
ple, a Show method can apply to different objects: BillingForm.Show shows
the form object called BillingForm; exitButton.Show shows the button object
called exitButton.
Events
You can write methods that execute when a particular event occurs. An event
occurs when the user takes an action such as clicking a button, pressing a key,
scrolling, or closing a window. Events also can be triggered by actions of other
objects, such as repainting a form or a timer reaching a preset point.
Classes
A class is a template or blueprint used to create a new object. Classes contain
the definition of all available properties, methods, and events.
Each time that you create a new object, it must be based on a class. For
example, you may decide to place three buttons on your form. Each button is
based on the Button class and is considered one object, called an instance of the
class. Each button (or instance) has its own set of properties, methods, and
events. One button may be labeled “OK”, one “Cancel”, and one “Exit”. When
the user clicks the OK button, that button’s Click event occurs; if the user clicks
on the Exit button, that button’s Click event occurs. And, of course, you have
written different program instructions for each of the button’s Click events.
An Analogy
If the concepts of classes, objects, properties, methods, and events are still a
little unclear, maybe an analogy will help. Consider an Automobile class. When
Bradley−Millspaugh: 1. Introduction to Text © The McGraw−Hill 13
Programming in Visual C# Programming and Visual Companies, 2010
2008 C# 2008
C H A P T E R 1 5
we say automobile, we are not referring to a particular auto, but we know that
an automobile has a make and model, a color, an engine, and a number of
doors. These elements are the properties of the Automobile class.
Each individual auto is an object, or an instance of the Automobile class.
Each Automobile object has its own settings for the available properties. For
example, each Automobile object has a Color property, such as myAuto.Color =
Blue and yourAuto.Color = Red.
The methods, or actions, of the Automobile class might be Start, SpeedUp,
SlowDown, and Stop. To refer to the methods of a specific object of the class,
use myAuto.Start and yourAuto.Stop.
The events of an Automobile class could be Arrive or Crash. In a C# pro-
gram, you write event-handling methods that specify the actions you want to
take when a particular event occurs for an object. For example, you might write
a method to handle the yourAuto.Crash event.
Note: Chapter 12 presents object-oriented programming in greater depth.
C#
Microsoft C# is a part of Visual Studio. You also can purchase C# by itself
(without the other languages but with the .NET Framework). C# is available in
an Express Edition, a Standard Edition, a Professional Edition, and four
specialized versions of Team System Editions for large enterprise application
development. You can find a matrix showing the features of each edition in
Help. Anyone planning to do professional application development that in-
cludes the advanced features of database management should use the Profes-
sional Edition or the Team System Database version. The full Professional
Edition is available to educational institutions through the Microsoft Academic
Alliance program and is the best possible deal. When a campus department
purchases the Academic Alliance, the school can install Visual Studio on all
classroom and lab computers and provide the software to all students and fac-
ulty at no additional charge. For more information, have your instructor visit:
http://msdn.microsoft.com/en-us/academic/default.aspx
14 Bradley−Millspaugh: 1. Introduction to Text © The McGraw−Hill
Programming in Visual C# Programming and Visual Companies, 2010
2008 C# 2008
Writing C# Programs
When you write a C# application, you follow a three-step process for planning
the project and then repeat the three-step process for creating the project. The
three steps involve setting up the user interface, defining the properties, and
then creating the code.
Programming
After you have completed the planning steps and have approval from your user,
you are ready to begin the actual construction of the project. Use the same
three-step process that you used for planning.
1. Define the user interface. When you define the user interface, you create
the forms and controls that you designed in the planning stage.
Think of this step as defining the objects you will use in your
application.
Bradley−Millspaugh: 1. Introduction to Text © The McGraw−Hill 15
Programming in Visual C# Programming and Visual Companies, 2010
2008 C# 2008
C H A P T E R 1 7
2. Set the properties. When you set the properties of the objects, you give
each object a name and define such attributes as the contents of a label,
the size of the text, and the words that appear on top of a button and in
the form’s title bar.
You might think of this step as describing each object.
3. Write the code. You will use C# programming statements (called C#
code) to carry out the actions needed by your program. You will be sur-
prised and pleased by how few statements you need to create a powerful
Windows program.
You can think of this third step as defining the actions of your
program.
C# Application Files
A C# application, called a solution, can consist of one or more projects. Since
all of the solutions in this text have only one project, you can think of one
solution = one project. Each project can contain one or more form files. In
Chapters 1 through 5, all projects have only one form, so you can think of one
project = one form. Starting in Chapter 6, your projects will contain multiple
forms and additional files. As an example, the HelloWorld application that you
will create later in this chapter creates the following files:
HelloForm.cs A .cs (C#) file that holds the code methods that
you write. This is a text file that you can open
in any editor. Warning: You should not modify
this file unless you are using the editor in the
Visual Studio environment.
Note: You can display file extensions. In Windows Vista, open the Explorer and
select Organize / Folders and Search Options, click on the View tab and deselect
the check box for Hide extensions for known file types. In Windows XP, in the My
Computer Tools menu, select Folder Options and the View tab. Deselect the
check box for Hide extensions for known file types. If you do not display the
extensions, you can identify the file types by their icons.
After you run your project, you will find several more files created by the
system. The only file that you open directly is the .sln, or solution file.
C H A P T E R 1 9
Figure 1.3
Note: If you plan to develop in more than one language, such as VB and
C#, you can save each group of settings and switch back and forth between the
two. Select Tools / Import and Export Settings and choose to Reset all settings.
Figure 1.4
The Visual Studio IDE with the Start Page open, as it first appears in Windows Vista, without an open project. You can close
the Start Page by clicking on its Close button.
Close button for
Start Page
Figure 1.5
C H A P T E R 1 11
The Visual Studio environment. Each window can be moved, resized, closed, or customized.
The Toolbars
You can use the buttons on the toolbars as shortcuts for frequently used opera-
tions. Each button represents a command that also can be selected from a menu.
Figure 1.7a shows the toolbar buttons on the Standard toolbar for the Profes-
sional Edition, which displays in the main window of the IDE; Figure 1.7b shows
the Layout toolbar, which is useful for designing forms in the Form Designer; and
Figure 1.7c shows the Text Editor toolbar, which contains buttons to use in the
Editor window. Select View / Toolbars to display or hide these and other toolbars.
20 Bradley−Millspaugh: 1. Introduction to Text © The McGraw−Hill
Programming in Visual C# Programming and Visual Companies, 2010
2008 C# 2008
Figure 1.7
The Visual Studio toolbars contain buttons that are shortcuts for menu commands. You can display or hide each of the
toolbars: a. the Standard toolbar; b. the Layout toolbar; and c. the Text Editor toolbar.
Ne
Ad Pro
Op Ne t
Sa en F Ite
Sa F
Cu e A e
Co t
Pa y
Unste
Re
Na
Na igat
So
So
Fi
So
Pr utio
Ob er x
To ject ies lor
St lbo Bro ind r
ar x ws o
nd
op n E
o
ve ile m
v il
lu
lu
l
p
w
do
v
vi e B
d jec
do
tP
tio
tio
ga a
t p
ag
te ck
n
ll
w
Co
Pl
e
Fo wa
W e
at
nf
rw rd
fo
er w
ig
ar
rm
ur
d
s
at
io
(a)
ns
Al
Al
Al Le
Al C
Al Rig rs
Al To
Al Mi
M Bo es
M e Sa ms
M e Sa e W
Si Sa e H th
M o G Siz t
In Ho
De ease zon
Re reas ori Sp
M ve H oriz tal S g E
In e
De ease cal l Sp cin
Re eas erti acin ing
Ce ve erti Spa qu
Ce er H tic Sp g
Br r Ve zont aci g
Se o
M oB t
Ta e Ce k
ig
ig
ig ft
ig en
ig
ig ps
ig dd
ak tto
ak m
ak m id
ze m ei
ak ri e
ak
er
cr ri
cr rti nta pa g
b
in rti al ng
nd Fr ly
nt Ver cal cin al
nt or al S aci
c H tal
m e H zon aci
cr V Sp ac g
m e V cal g E
n
n
n
n te
n hts
n
n
g ac
Or lls
e
g T ca ly
o
e i
T e gh
T
to
de
Gr
r
s
id
on
o
l
o
p n
l S
n
i n
q
(b)
Co Un
Di
Di play
Di lay bje
Di lay aram Me
De lay uic eter ber
In reas ord Info fo ist
M le
M e To ook
M ve T Pre ark
M e T Ne ous
M e T Pre Bo ook
M e T Nex ous ma ark
Cl e T Prev Boo ook
To
m co
ov B
o
ov o vi
ov o xt B
ov o vi ok m
ov o t B rk
m m
cr e C
ea o N io km m
s
sp O
sp P ct
sp Q
gg
c W
en m
ea In om
r B e us a ark
t O en
se de p
oo xt B Bo rk i in
ut t th
In nt let
km oo ok n C
de
th e
m
ar km ma Cu urr
k
nt
e S Se
ks a rk rre en
el lec
m
ec te
In L
te d
rk i n t
io
d Li
n
Li ne
in n C t F Fol
ne s
Cu ur old de
s
rre ren er r
nt t D
Do oc
cu um
m en
en t
t
(c)
C H A P T E R 1 13
TIP
You can sort the tools in the tool-
box: Right-click the toolbox and se-
lect Sort Items Alphabetically from
the context menu (the shortcut
menu). ■
“Well, I must say one thing for Mrs. Nelson,” said our coachman,
as he resumed his seat on the box, “and that’s this. There’s no
pinning at the Bull. She sets her face against everything but the
patent boxes. She may come to a runaway with a bolter—or drop
the ribbons—or make a mistake in clearing a gate, by being a little
lushy—but you’ll never see Mrs. Nelson lying flat on her side in the
middle of the road, with her insides gone to smash, and her outsides
well distributed, because she’s been let go out of the yard without
one of her pins.”
THE STAMP DUTY ON SCOTCH LINEN.
SONNET
TO A SCOTCH GIRL, WASHING LINEN AFTER HER
COUNTRY FASHION.
WELL done and wetly, thou Fair Maid of Perth,
Thou mak’st a washing picture well deserving
The pen and pencilling of Washington Irving:
Like dripping Naiad, pearly from her birth,
Dashing about the water of the Firth,
To cleanse the calico of Mrs. Skirving,
And never from thy dance of duty swerving
As there were nothing else than dirt on earth!
Yet what is thy reward? Nay, do not start!
I do not mean to give thee a new damper,
But while thou fillest this industrious part
Of washer, wearer, mangler, presser, stamper,
Deserving better character—thou art
What Bodkin would but call—“a common tramper.”
MY APOLOGY.
GENTLE READERS,
For the present month, there must be what Dr. Johnson called
a solution of continuity in my “Literary Reminiscences.” Confined to
my chamber by what ought to be termed roomatism—then attacked
by my old livery complaint—and finally, by a minor, but troublesome
malady, the Present has too much prevailed over the Past, to let me
indulge in any retrospective reviews. In such cases, on the stage,
when a Performer is unable to support his character, a substitute is
usually found to read the part; but, unfortunately, in the present
case there is no part written, and consequently it cannot be read.
But apropos of theatricals—there is an anecdote on point.
In the Olympic days of the great Elliston, there was one evening
a tremendous tumult at his Theatre, in consequence of the absence
of a favourite performer. One man in the pit—a Butcher—was
especially vociferous in his cry for “Carl! Carl! Carl!” Others called for
the Manager, who duly made his appearance, and black as the
weather looked, he was the very sort of pilot to weather the storm.
With one of his princely bows he proceeded to address the House.
“Ladies and Gentlemen—but by your leave I will address myself to a
single individual. I will ask that gentleman (pointing to the vociferous
Butcher) what right he has to demand the appearance of Mr. Carl?”
“’Cos,” said the Butcher, “’cos he’s down in the Bill.” Such an
undeniable answer would have staggered any other Manager than
Elliston, but he was not easily to be disconcerted. “Because he is
down in the bill!” he echoed, in a tone of the loftiest indignation:
“Ladies and Gentlemen, the Mr. Carl, so unseasonably, so
vociferously and so unfeelingly called for, is at this very moment
labouring under severe illness—he is in bed. And let me ask, is a
man, a fellow-creature, a human being, to be torn from his couch,
from his home, on a cold night, from the affectionate attentions of
his wife and family, at the risk of his valuable life perhaps, to go
through a fatiguing part because he happens to be DOWN IN THE
BILL?” [Cries of “Shame, shame!” from all parts of the house.] “And
yet, ladies and gentlemen, there stands a man—if I may call him so
—a Butcher, that for his own selfish gratification—the amusement of
a few short hours—would risk the very existence of a deserving
member of society, a good husband, father, friend, and one of your
favourite actors, and all, forsooth, because he is DOWN IN THE
BILL!” [Universal hooting, with cries of “Turn him out.”] “By all
means,” acquiesced the Manager, with one of his best bows—and
the indignant pittites actually hooted and kicked their own champion
out of the theatre, as something more than a Butcher, and less than
a Christian.
Now I am myself, gentle readers, in the same predicament with
Mr. Carl. Like him I am an invalid—and like him I am unfortunately
down in the Bill. It would not become me to set forth my own
domestic or social virtues, or to hint what sort of gap my loss would
make in society—still less would it consist with modesty to compare
myself with a favourite actor—but as a mere human being I throw
myself on your mercy, and ask, in common charity, would you have
had me leave my warm bed, to shiver in a printer’s damp sheets, at
the risk of my reputation perhaps, and for the mere amusement of
some half hour, or more probably for no amusement at all—simply
because I was “down in the Bill?”
But there is no such Butcher, or Butcheress, or little Butcherling,
amongst you; and by your good leave and patience, the instalment
of my Reminiscences that is over due, shall be paid with interest in
the NEXT NUMBER.
THE TOP OF HIS PROFESSION.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com