Transcript Slide 1

Visual Studio.NET
Part 2 dbg --- Using the VS.NET
Integrated Development Environment
Visual Studio.NET
• A development “shell” for creating applications
–
–
–
–
Visual designer
Context editor with Intellisense for typing code
Programming language compilers
Run/Debug environment
2
Visual Studio.NET
• We will use Visual Studio.NET and the C#
programming language to develop Graphical
User Interface (GUI) applications.
• Notice that GUIs look very different from the
console applications you made in CIS202.
3
Installing Visual Studio.NET 2005
• If you have Windows XP with Service Pack 2,
Windows Vista, or a Macintosh Computer
running Windows XP via Bootcamp or Parallels,
you can install Visual Studio V2005.
• Obtain 2 CDROMs from School of Math &
Science office (2nd floor, Madison Avenue end of
Science Center).
• Follow the provided instructions.
4
Starting Visual Studio.NET
1. Start Visual Studio.NET from a Desktop
shortcut.
-or-
2.
, Programs:
5
Creating a New Project
6
Select a Language
7
Select a Template
8
Assign Path and Meaningful Name
9
The Visual Studio IDE
10
Adjust the View in the IDE
• 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. Double-click a window’s title bar to return the
window to its default location.
• To hide a docked window, click its Auto Hide
button. The window will be displayed as a tab at
the edge of the screen. You can redisplay it by
placing the mouse pointer over the tab.
• To return all windows to their original Visual
Studio positions, choose Window, Reset Window
Layout in the menu.
11
Setting Default Location for Projects
• Choose Tools, Options
in the menu.
• Click the + in front of
Projects and Solutions.
• Click General under
Projects and Solutions.
• Click the Browse button
next to Visual Studio
projects location: and
choose the folder you
created for this course.
Make a folder on drive c: rather
than in My Documents!
12
Visual Studio Solutions
• A solution is comparable to a
workspace in C++ V6.
• The first project created in the
solution gives its name to the
solution and thus to the
namespace as well.
• At times, it may be advisable to
change the name of either the
project or the namespace.
13
The Designer View
• The IDE displays a
design-time view of the
visible portion, if any, of
the object being
designed.
• A Designer view of a
form object appears by
default in a new project.
• The form will be a
window at run time.
14
The Code View
• The IDE provides an
editor for building the
actual code for the class
or classes that make up
your new project.
• Pre-existing (library)
code is not visible here.
• Choose View Code icon
in Solution Explorer or
click Code tab in
Designer Window.
15
Adjusting Properties at Design Time
• The IDE provides a
convenient window for
assigning values to object
properties at design time.
• The Properties Window
displays all available
properties for any object that
is selected within the IDE.
• Use F4 or View, Properties
Window from menu, if the
Properties Window is not
visible.
16
Some Form Properties
• A number of form properties can
be changed at design time.
• Dimensions of the form can be
adjusted by dragging the
selection handles or by
assigning values to the Size
Property in the property window.
• The screen position of the
window can be adjusted by
choosing values for the
StartPosition property.
17
More Form Properties
• Every form should have a Text property that
reflects the role of the form in the project; the
text typed as the Text Property appears in the
Titlebar of the window.
• The form icon can also be changed by assigning
a new icon file to the Icon property; later, we will
learn how to create a custom icon.
 FormWithChangedProperties
18
Compiling and Running
• Choose Build, Build
Solution from menu to
compile without running.
• 3 ways to run (and build, if
necessary) an application:
– Choose Debug, Start
Debugging from menu
– Press F5
– Click the Debug tool in the
toolbar
19
The Compilation Process
Visual Studio .NET
.NET Framework
Integrated
Development
Environment
C#
compiler
Common
Language
Runtime
1
2
3
Solution
Assembly
Project
Intermediate Language (IL)
Source files
Class references
20
The Window Control Box
• When the Windows Application project runs, the
form appears as a Window object.
• The project remains running until the window is
closed.
• The window can be closed by clicking the
in
the Control Box
in the Titlebar.
21
The Window Control Box
• The Control Box also contains
icons for minimizing the
window to a taskbar item and
maximizing the window to full
screen size.
• The Maximize icon becomes a
Restore Down icon if the
window is maximized.
• All, some, or none of these
icons may be displayed by
setting the ControlBox,
MinimizeBox, and/or
MaximizeBox properties to
true or false.
Maximize
Minimize
Restore Down
Close
 ControlBox
22
Show in TaskBar
• You may not wish to have a Taskbar item
appear to represent your window.
• Setting the Show in TaskBar property to False at
design time will accomplish this.
23
Let’s Review C++ File Structure
• Before looking at the C#.NET V2005 file
structure of the project we just created, we’ll
review how C++ V6 and .NET versions of
projects look in My Computer.
24
File Structure
of a C++ V6
or .NET
Console
Application
HW777
Debug or
Release
HW777.exe *
HW777.dsw or
(.sln)
source.obj *
source.cpp
HW777.pdb
HW777.ncb
HW777.pch *
HW777.dsp
HW777.ilk
HW777.plg
vc60.pdb
HW777.opt
vc60.idb *
Note: Debug folder contains all 7 subfiles.
Release folder only contains 4 asterisked subfiles.
Structure of a C# Solution
• The IDE creates a collection of files called a
solution.
• Normally, a solution contains one project.
• A project generates an application.
• Multiple projects may be contained within one
solution.
26
C#.NET V2005 Solution Explorer Window
27
Let’s Look at C# V2005 File Structure
• Now we’ll look at how the C#.NET V2005 project
we just created looks in My Computer.
28
File Structure of C#.NET V2005 Project
29
HW999
File Structure of a C#.NET V2005
Windows Application
HW999
bin
Debug
obj
HW999.exe
Properties
frmDeesDiner.cs
frmDeesDiner.Designer.cs
HW999.csproj
HW999.sln
frmDeesDIner.resx
HW999.suo
Program.cs
Important Files
• The project file (with an extension of .csproj)
keeps track of the files that make up the project
and records various settings for the project.
• The solution file (with an extension of .sln) 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.
31
Form Files
• In C# 2005, each form is defined by two C#
files. Each file contains a partial class. The file
with the .cs extension contains the code that is
entered by the programmer; the file with the
Designer.cs extension contains the code that is
generated when the programmer works with the
Form Designer. When the project is compiled,
these two partial classes are combined into a
single class that determines the appearance and
operation of the form.
32
Other Files
• A source code file that doesn’t define a form is
usually stored in a single C# source file that
contains a single class.
• The References folder contains references to
the assemblies for the namespaces that the
application can use. These namespaces contain
the classes that the project requires. In most
cases, all the references that you need are
included when the project is created.
33
Opening a C#.NET V2005 Project
from My Computer
• To open a project from My Computer, doubleclick the solution file (ex. Homework999.sln).
• The solution file (ex. Homework999.sln), which
is a file that contains pointers to all Projects
within a Solution, is now stored outside of the
Project folder.
34
Opening a C#.NET V2005 Project
from Visual Studio.NET
35
E-mailing Assignments to Professor
• When e-mailing an assignment to me, you must
submit the circled folder and all of its contents.
You should “zip” this
folder into 1 file for easy
transmission using a
program like WinZip, 7Zip, ZipItFree, or JZip.
The course website has
links to the appropriate
download sites.
36
“Zipping” a Folder via WinZip
1. Navigate to folder in My Computer.
2. Right-click and choose WinZip from context menu.
3. Choose 2nd option, Add to Homework999.zip .
4. One file, Homework999.zip, containing the
contents of entire Homework999 folder, is created.
37
Start a New E-mail Message
1. Start http://mail.strose.edu and click New
message. Add professor e-mail address in To:
area and assignment number in Subject: area.
38
Adding .zip File as an Attachment
1. Click
in message toolbar. You will see the
Attachments dialog.
2. Browse for your .zip file.
39
Adding .zip File as an Attachment
1. After the file name
displays in Choose
a file to attach box,
click the Attach
button.
2. The .zip file will
then show as a
Current File
Attachment. Click
the Close button.
40
Send E-mail Message
• Your e-mail message should now display the
attached .zip file.
• Send the e-mail message.
41
Events
Application Lifetime and Object
Responses to Stimuli
Event-Driven Applications
• Objects can both send, and react to, special
messages or stimuli called events.
• By and large, event-driven applications run until
they are stopped by user intervention.
• Event driven applications respond to any events
that they detect.
43
Events
Event
Occurs when…
Common control events
Click
…the user clicks the control
Double-click
…the user double-clicks the control
Enter
…the focus is moved to the control
Leave
...the focus is moved from the control
Common form events
Load
Closing
…the form is loaded into memory
…the form is closing
Closed
…the form is closed
44
Reacting to a Click Event
• An object can react to the event caused by
clicking the mouse.
• Objects react to events by executing code within
a special method (function) called an event
handler.
• A specific event (clicking this object) causes the
event handler to run.
• Any instructions coded within the event handler
are then executed.
 FirstEvent
45
Associating Handlers with Events
• An event handler is a method (function) that
contains the code that will be executed when a
particular event occurs for the a specific object.
• Event handlers may be associated with objects
and their events in several ways:
– The default handler for a control (often the Click event
handler) will be added automatically to the code area,
when you double-click the control in the Designer
View.
– Or, you may double-click a particular event in the
Properties Window.
46
Associating Events via
the Property Window
• A list of all available events for
an object is available in the
Properties window if the
is
toggled.
• Double-click the appropriate
event.
• Selecting an event here causes
an event handler skeleton to be
created in the code area.
47
A New Handler Function
• Event Handler methods are
named with a combination
of the object and the event.
48
“Event Wiring”
• A separate line of code is automatically written
(in the Windows Generated Code region of the
formName.Designer.cs file) to connect the event
handler to the event. You can think of this as
“event wiring” or “automatic registration” of the
event handler method.
49
Structure of a Handler
• An event handler is a void method with two
arguments.
• The sender argument of type Object represents
the originator of the event.
50
Structure of a Handler
• The e argument of type EventArgs represents
the set of data fields, if any, that arrive with the
event.
• EventArgs fields vary with the class that
originates, or raises, the event.
51