Lecture01 - ITE 5230
Lecture01 - ITE 5230
1
Before we start…
■ www.menti.com
■ Code:40 56 63
2
Before we start (2)…
3
Total: 100%
4
5
Chapter 1
C1, Slide 6
Objectives
Applied
1. Use Visual Studio 2015 to do any of these operations:
Open and close an existing C# project or solution
Display the Form Designer for each of the forms in a project
Display the Code Editor for each of the forms in a project
Use the Solution Explorer to review the files in a project
Open, hide, and adjust the windows for a project
Build and run a project
Knowledge
1. Describe the main difference between a Windows Forms
application and a Web Forms application.
2. List two additional types of .NET applications that you can
develop with Visual Studio 2015.
C1, Slide 7
Objectives (cont.)
3. Name the two languages that you can use for rapid application
development with Visual Studio 2015.
4. Describe the two main components of the .NET Framework.
5. In general terms, describe the C# compiler, Microsoft
Intermediate Language, the assembly, and the Common Language
Runtime.
6. Describe the use of each of these windows in the Visual Studio
IDE: Form Designer, Code Editor, and Solution Explorer.
7. In general terms, explain what makes it possible to select a target
framework with Visual Studio 2015.
C1, Slide 8
A Windows Forms application running on the desktop
C1, Slide 9
Common types of .NET applications
Type Description
Windows Forms Runs in its own window on the user’s PC
and consists of one or more Windows.
ASP.NET Web Forms Runs on a web server and consists of one
or more web forms that define pages that
are displayed in a browser on the client.
ASP.NET MVC Runs on a web server and consists of
pages that are displayed in a browser on
the client machine. Unlike ASP.NET
Web Forms, ASP.NET MVC uses a
Model-View-Controller design pattern to
create its pages.
WPF Runs in windows on the user’s PC and
provides an enhanced user experience.
C1, Slide 10
Visual Studio 2015 Editions
Community Edition
Professional Edition
Enterprise Edition
C1, Slide 11
Programming languages supported by Visual Studio 2015/17/19
Visual Basic
Visual C#
Visual C++
Visual F#
C1, Slide 12
The .NET Framework
C1, Slide 13
The Visual Studio IDE
C1, Slide 14
How a C# application is compiled and run
C1, Slide 15
The Visual Studio Start Page
C1, Slide 16
The Open Project dialog box
C or H?
C1, Slide 17
Project and solution concepts
Every C# project has a project file with an extension of csproj that
keeps track of the files that make up the project and records
various settings for the project.
Every solution has a solution file with an extension of sln that
keeps track of the projects that make up the solution.
When you open a project file, Visual Studio opens the solution
that contains the project. And when you open a solution file,
Visual Studio automatically opens all the projects contained in the
solution.
The project file can be stored in the same directory as the solution
file or in one of its subdirectories.
C1, Slide 18
The Form Designer window of Visual
C1, Slide 19
The Code Editor window of Visual
C1, Slide 20
The Solution Explorer
C1, Slide 21
Project files
The Solution Explorer uses different icons to distinguish between
source code files that define forms and source code files that
don’t.
Each form is defined by two C# source files where each file
contains a partial class. The file with the cs extension contains the
code that’s entered by the programmer, and the file with the
Designer.cs extension contains the code that’s generated when the
programmer works with the Form Designer. When the project is
compiled, these partial classes are combined into a single class
that determines the appearance and operation of the form.
A source code file that doesn’t define a form is usually stored in a
single C# source file that contains a single class.
C1, Slide 22
Project files (cont.)
The Program.cs file contains the C# code that starts the
application and runs the first form. The app.config file contains
configuration information for the application.
The References folder contains references to the assemblies for
the namespaces that the application can use.
C1, Slide 23
Two floating windows, a hidden window,
and a pinned window
C1, Slide 24
How to rearrange windows
To undock a docked window so it floats on the screen, drag it by
its title bar away from the edge of the application window or
double-click its title bar.
To dock a floating window, drag it by its title bar onto one of the
positioning arrows that become available. Or, hold down the Ctrl
key and then double-click its title bar to return it to its previous
docked location.
To hide a docked window, click its Auto Hide button. Then, the
window is displayed as a tab at the edge of the screen, and you
can display it by clicking on the tab.
To display a window in a group of tabbed windows, click on its
tab.
If you dock, undock, hide, or unhide a tabbed window, all the
windows in the group are docked, undocked, hidden, or unhidden.
C1, Slide 25
How to rearrange windows (cont.)
To pin a window to the left side of the Form Designer, click its
Toggle Pin Status button.
You can also use the commands in the Window menu to work
with windows. For example, you can use the Reset Window
Layout command to return the windows to their default layout.
You can also save, apply, and manage custom layouts.
C1, Slide 26
The form that’s displayed when
the Financial Calculations project is run
C1, Slide 27
How to build a project without running it
Use the BuildBuild Solution command. Or, right-click the
project in the Solution Explorer and select the Build command
from the shortcut menu.
C1, Slide 28
The dialog box for upgrading a project
to Visual Studio 2015
C1, Slide 29
The application properties for a project
C1, Slide 30
Chapter 2
How to design
a Windows Forms
application
C2, Slide 31
Objectives
Applied
1. Given the form design and property settings for a simple
application, use the Form Designer to design the form.
2. When necessary, rename the form, project, and solution files for
an application.
3. Customize the Visual Studio environment for use with C# by
setting the Visual Studio options and the import and export
settings.
C2, Slide 32
Objectives (cont.)
Knowledge
1. Describe the use of the Toolbox and Properties window.
2. Describe the Name and Text properties for a form or control.
3. Describe these additional properties for a control: Enabled,
ReadOnly, and TextAlign.
4. Describe the way you adjust the tab order for the controls on a
form, set access keys for controls, and set the default buttons for
the Enter and Esc keys for a form.
C2, Slide 33
The Options dialog box
for setting the project options
C2, Slide 34
The New Project dialog box
C2, Slide 35
The Invoice Total form
C2, Slide 36
A form after some controls have been added to it
C2, Slide 37
Three ways to add a control to a form
Select the control in the Toolbox. Then, click in the form where
you want to place the control. Or, drag the pointer on the form to
place the control and size it at the same time.
Double-click the control in the Toolbox. Then, the control is
placed in the upper left corner of the form.
Drag the control from the Toolbox and drop it on the form. Then,
the control is placed wherever you drop it.
C2, Slide 38
How to select and work with controls
Use standard Windows techniques to select, move, size, or group
the controls on a form.
You can also select a group of controls by clicking on a blank spot
in the form and then dragging around the controls.
To align, size, or space a group of selected controls, click on a
control to make it the primary control. Then, use the commands in
the Format menu or the buttons on the Layout toolbar to align,
size, or space the controls relative to the primary control.
You can also size all of the controls in a group by sizing the
primary control in the group.
Note
A label is sized automatically based on the amount of text that it
contains. As a result, you can’t size a label by dragging its handles
unless you change its AutoSize property to False.
C2, Slide 39
A form after the properties have been set
C2, Slide 40
The Name property
Sets the name you use to identify a control in your C# code.
Can be changed to provide a more descriptive and memorable
name for forms and controls that you will refer to when you write
your code (such as text boxes and buttons).
Doesn’t need to be changed for controls that you won’t refer to
when you write your code (such as most labels).
Can use a three-letter prefix to indicate whether the name refers to
a form (frm), button (btn), label (lbl), or text box (txt).
C2, Slide 41
The Text property
Sets the text that’s displayed on the form or control.
For a form, the Text value is displayed in the title bar. For
controls, the Text value is displayed directly on the control.
For a text box, the Text value changes when the user types text
into the control, and you can write code that uses the Text
property to get the text that was entered by the user.
C2, Slide 42
Other properties for forms
Property Description
AcceptButton Identifies the button that will be activated
when the user presses the Enter key.
CancelButton Identifies the button that will be activated
when the user presses the Esc key.
StartPosition Sets the position at which the form is
displayed. To center the form, set this
property to CenterScreen.
C2, Slide 43
Other properties for controls
Property Description
Enabled Determines whether the control will be enabled or
disabled.
ReadOnly Determines whether the text in some controls like
text boxes can be edited.
TabIndex Indicates the control’s position in the tab order,
which determines the order in which the controls
will receive the focus when the user presses the
Tab key.
TabStop Determines whether the control will accept the
focus when the user presses the Tab key to move
from one control to another. Some controls, like
labels, don’t have the TabStop property.
TextAlign Sets the alignment for the text displayed on a
control.
C2, Slide 44
How to adjust the tab order
Tab order refers to the sequence in which the controls receive the
focus when the user presses the Tab key. You should adjust the
tab order so the Tab key moves the focus from one control to the
next in a logical sequence.
Each control has a TabIndex property that indicates the control’s
position in the tab order. You can change this property to change a
control’s tab order position.
If you don’t want a control to receive the focus when the user
presses the Tab key, change that control’s TabStop property to
False.
Label controls don’t have a TabStop property so they can’t
receive the focus.
C2, Slide 45
How to set access keys
Access keys are shortcut keys that the user can use in combination
with the Alt key to quickly move to individual controls on the
form.
You use the Text property to set the access key for a control by
placing an ampersand immediately before the letter you want to
use for the access key.
Since the access keys aren’t case sensitive, &N and &n set the
same access key.
When you set access keys, make sure to use a unique letter for
each control.
You can’t set the access key for a text box. However, if you set an
access key for a label that immediately precedes the text box in
the tab order, the access key will take the user to the text box.
If you assign an access key to a button, the button is activated
when you press Alt plus the access key.
C2, Slide 46
How to set the Enter and Esc keys
The AcceptButton property of the form sets the button that will be
activated if the user presses the Enter key.
The CancelButton property of the form sets the button that will be
activated if the user presses the Esc key. This property should
usually be set to the Exit button.
You set the AcceptButton or CancelButton values by choosing the
button from a drop-down list that shows all of the buttons on the
form. So be sure to create and name the buttons you want to use
before you attempt to set these values.
C2, Slide 47
The property settings for the Invoice Total form
C2, Slide 48
The property settings for the controls
Default name Property Setting
label1 Text &Subtotal:
TextAlign MiddleLeft
TabIndex 0
label2 Text Discount percent:
TextAlign MiddleLeft
label3 Text Discount amount:
TextAlign MiddleLeft
label4 Text Total:
TextAlign MiddleLeft
textBox1 Name txtSubtotal
TabIndex 1
textBox2 Name txtDiscountPercent
ReadOnly True
TabStop False
C2, Slide 49
The property settings for the controls (cont.)
textBox3 Name txtDiscountAmount
ReadOnly True
TabStop False
textBox4 Name txtTotal
ReadOnly True
TabStop False
button1 Name btnCalculate
Text &Calculate
TabIndex 2
button2 Name btnExit
Text E&xit
TabIndex 3
C2, Slide 50
The Solution Explorer as a form file is being renamed
C2, Slide 51
How to rename a file, project, or solution
Right-click on it in the Solution Explorer window and select the
Rename command from the shortcut menu. Or, you can select it in
the Solution Explorer and press F2. Then, you can enter the new
name.
Be sure not to change or omit the file extension when you rename
a file. Remember too that using a three-letter prefix to indicate the
contents of the file (like frm for a form file) makes it easier to tell
what each file represents.
When you change the name of a file, Visual Studio also changes
the File Name property for the form and asks you if you want to
change all references to the file.
If you change the name of a project, you may also want to change
the name of the assembly that’s created for the project and the
name of the namespace that contains the project from the
Application tab of the Project Properties window.
C2, Slide 52
How to save a file, project, or solution
You can use the Save All button in the Standard toolbar or the
Save All command in the File menu to save all files and projects
in the solution.
You can use the Save button in the Standard toolbar or the Save
command in the File menu to save a file, project, or solution. The
files that are saved depend on what’s selected in the Solution
Explorer window.
If you try to close a solution that contains modified files, a dialog
box is displayed that asks you if you want to save those files.
C2, Slide 53
Chapter 3
C3, Slide 54
Objectives
Applied
1. Given the code for a simple application, use the skills presented in
this chapter to add the code and test the application.
2. Use indentation and blank lines to make the code easier to read.
3. Use comments to document the code for the entire form or for
portions of the code.
4. Use any of the help features to get the information that you need
for developing an application.
C3, Slide 55
Objectives (cont.)
Knowledge
1. In the context of object-oriented programming, describe these
terms: class, object, instantiation, instance, property, method,
event, and member.
2. Describe how an application responds to events.
3. Describe the use of snippets, refactoring, and annotations.
4. Describe the use of annotations in the vertical scroll bar of the
Code Editor.
5. Distinguish between a syntax (or build) error and a runtime error.
6. Distinguish between testing and debugging.
7. Explain how a data tip can help debug a runtime error.
C3, Slide 56
A form object and its ten control objects
C3, Slide 57
Class and object concepts
An object is a self-contained unit like a form or control that
combines code and data.
A class is the code that defines the characteristics of an object.
You can think of a class as a template for an object.
An object is an instance of a class, and the process of creating an
object from a class is called instantiation.
More than one object instance can be created from a single class.
C3, Slide 58
Property, method, and event concepts
Properties define the characteristics of an object and the data
associated with an object.
Methods are the operations that an object can perform.
Events are signals sent by an object to the application telling it
that something has happened that can be responded to.
Properties, methods, and events can be referred to as members of
an object.
If you instantiate two or more instances of the same class, all of
the objects have the same properties, methods, and events.
However, the values assigned to the properties can vary from one
instance to another.
C3, Slide 59
Objects and forms
When you use the Form Designer, Visual Studio automatically
generates C# code that creates a new class based on the Form
class. Then, when you run the project, a form object is instantiated
from the new class.
When you add a control to a form, Visual Studio automatically
generates C# code in the class for the form that instantiates a
control object from the appropriate class and sets the control’s
default properties.
When you move and size a control, Visual Studio automatically
sets the properties that specify the location and size of the control.
C3, Slide 60
A member list in the Code Editor window
C3, Slide 61
The syntax for referring to a member
of a class or object
ClassName.MemberName
objectName.MemberName
C3, Slide 62
Statements that refer to methods
txtMonthlyInvestment.Focus(); Uses the Focus method to
move the focus to the text box
named txtMonthlyInvestment.
this.Close(); Uses the Close method to
close the form that contains
the statement. Here, this is a
keyword that is used to refer
to the current instance of the
class.
C3, Slide 63
How to enter member names when working
in the Code Editor
1. To display a list of the available members for a class or an object,
type the class or object name followed by a period (called a dot
operator, or just dot).
2. Type one or more letters of the member name, and the Code Editor
will select the first entry in the list that matches those letters. Or,
scroll down the list to select the member you want.
3. Press the Tab or Enter key to insert the member into your code.
Note
If a member list isn’t displayed, select the ToolsOptions
command to display the Options dialog box. Then, expand the
Text Editor group, select the C# category, and check the Auto List
Members and Parameters Information boxes.
C3, Slide 64
Event: The user clicks the Exit button
C3, Slide 65
Common control events
Event Occurs when…
Click …the user clicks the control.
DoubleClick …the user double-clicks the control.
Enter …the focus is moved to the control.
Leave …the focus is moved from the control.
C3, Slide 66
How an application responds to events
Windows Forms applications work by responding to events that
occur on objects.
To indicate how an application should respond to an event, you
code an event handler, which is a special type of method that
handles the event.
To connect the event handler to the event, Visual Studio
automatically generates a statement that wires the event to the
event handler. This is known as event wiring.
An event can be an action that’s initiated by the user like the Click
event, or it can be an action initiated by program code like the
Closed event.
C3, Slide 67
The method that handles the Click event
of the Calculate button
Object Event
C3, Slide 68
How to handle the Click event of a button
1. In the Form Designer, double-click the control. This opens the Code
Editor, generates the declaration for the method that handles the
event, and places the cursor within this declaration.
2. Type the C# code between the opening brace ({) and the closing
brace (}) of the method declaration.
3. When you are finished writing code, you can return to the Form
Designer by clicking on its tab.
C3, Slide 69
The completion list that’s displayed when you enter
a letter at the beginning of a line of code
C3, Slide 70
The completion list that’s displayed
as you enter code within a statement
C3, Slide 71
The event handlers for the Invoice Total form
private void btnCalculate_Click(object sender,
EventArgs e)
{
decimal subtotal =
Convert.ToDecimal(txtSubtotal.Text);
decimal discountPercent = 0m;
if (subtotal >= 500)
{
discountPercent = .2m;
}
else if (subtotal >= 250 && subtotal < 500)
{
discountPercent = .15m;
}
else if (subtotal >= 100 && subtotal < 250)
{
discountPercent = .1m;
}
C3, Slide 72
The event handlers for the Invoice Total form (cont.)
txtDiscountPercent.Text =
discountPercent.ToString("p1");
txtDiscountAmount.Text =
discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
txtSubtotal.Focus();
}
C3, Slide 73
Coding rules
Use spaces to separate the words in each statement.
Use exact capitalization for all keywords, class names, object
names, variable names, etc.
End each statement with a semicolon.
Each block of code must be enclosed in braces ({}). That includes
the block of code that defines the body of a method.
C3, Slide 74
The Code Editor and Error List windows
with syntax errors displayed
C3, Slide 75
A method written in a readable style
private void btnCalculate_Click(object sender,
EventArgs e)
{
decimal subtotal =
Convert.ToDecimal(txtSubtotal.Text);
C3, Slide 76
A method written in a readable style (cont.)
decimal discountAmount = subtotal * discountPercent;
decimal invoiceTotal = subtotal - discountAmount;
txtDiscountPercent.Text =
discountPercent.ToString("p1");
txtDiscountAmount.Text =
discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
txtSubtotal.Focus();
}
C3, Slide 77
A method written in a less readable style
private void btnCalculate_Click(object sender, EventArgs e){
decimal subtotal=Convert.ToDecimal(txtSubtotal.Text);
decimal discountPercent=0m;
if (subtotal>=500) discountPercent=.2m;
else if (subtotal>=250&&subtotal<500) discountPercent=.15m;
else if (subtotal>=100&&subtotal<250) discountPercent=.1m;
decimal discountAmount=subtotal*discountPercent;
decimal invoiceTotal=subtotal-discountAmount;
txtDiscountPercent.Text=discountPercent.ToString("p1");
txtDiscountAmount.Text=discountAmount.ToString("c");
txtTotal.Text=invoiceTotal.ToString("c");txtSubtotal.Focus();}
C3, Slide 78
Coding recommendations
Use indentation and extra spaces to align statements and blocks of
code so they reflect the structure of the program.
Use spaces to separate the words, operators, and values in each
statement.
Use blank lines before and after groups of related statements.
Note
As you enter code in the Code Editor, Visual Studio automatically
adjusts its formatting by default.
C3, Slide 79
A method with comments
private void btnCalculate_Click(object sender, EventArgs e)
{
/***************************************
* this method calculates the total
* for an invoice depending on a
* discount that's based on the subtotal
****************************************/
C3, Slide 80
A method with comments (cont.)
else if (subtotal >= 100 && subtotal < 250)
{
discountPercent = .1m;
}
C3, Slide 81
How to code comments
Comments are used to help document what a program does and
what the code within it does.
To code a single-line comment, type // before the comment. You
can use this technique to add a comment on its own line or to add
a comment at the end of a line.
To code a delimited comment, type /* at the start of the comment
and */ at the end. You can also code asterisks to identify the lines
in the comment, but that isn’t necessary.
C3, Slide 82
The Code Editor and the Text Editor toolbar
C3, Slide 83
How to use the buttons
of the Text Editor toolbar
To display or hide the Text Editor toolbar, right-click in the
toolbar area and choose Text Editor from the shortcut menu.
To comment or uncomment several lines of code, select the lines
and click the Comment Out or Uncomment button.
During testing, you can comment out a line of code. That way,
you can test new statements without deleting the old statements.
To move quickly between lines of code, you can use the last four
buttons on the Text Editor toolbar to set and move between
bookmarks.
C3, Slide 84
How to collapse or expand regions of code
If a region of code appears in the Code Editor with a minus sign
(-) next to it, you can click the minus sign to collapse the region
so just the first line is displayed.
If a region of code appears in the Code Editor with a plus sign (+)
next to it, you can click the plus sign to expand the region so all of
its code is displayed.
C3, Slide 85
The Code Editor with enlarged text
and a highlighted variable
C3, Slide 86
The default list of Visual C# code snippets
C3, Slide 87
A code snippet after it has been inserted
C3, Slide 88
The options that are displayed
when you rename a variable
C3, Slide 89
The Preview Changes - Rename dialog box
C3, Slide 90
A scroll bar in map mode with annotations
C3, Slide 91
Online help for Visual Studio
C3, Slide 92
The form that’s displayed
when you run the Invoice Total project
C3, Slide 93
The Exception Assistant that’s displayed when a
runtime error occurs
C3, Slide 94
How to test a project
1. Test the user interface, including the appearance of the controls,
the tab order, the access keys, and the Enter and Esc keys.
2. Test valid input data.
3. Test invalid data or unexpected user actions.
C3, Slide 95
How a project looks in break mode
C3, Slide 96