Computer Science 140
C# Programming
Introduction to CS 140
Introducing .NET and Visual Studio.NET
Today's Plan

Go over the syllabus

Cover .NET Basics




Just what is a "platform"?
Evolution of platform architecture
Windows and .NET architecture
Building a .NET application


A quick intro to using Visual Studio .NET
In-class Exercise 1
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Syllabus I

Class Web Site


csjava.occ.cccd.edu/~gilberts/CS140F02
Meeting


Class is in Computing Center 102
3.5 hours lecture, 1.5 hours lab
Lecture from 7:15 - 8:45 p.m.
 Lab from 9:00 - 9:50 p.m.

Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Syllabus II

Instructor


Stephen Gilbert
Preferred communication, email:



[email protected],
[email protected]
Phone: (714) 432-0202 ext 21173
In person office hours:
Monday 6-7, Wednesday 3-4
 Tuesday/Thursday 1:30-2:30

Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Syllabus III

Course description



Is this course for you?
Goals & Outcomes
Course requirements






Reading & workload
Exams
Quizzes
In-class programming assignment
Programming assignments
Grades
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Syllabus IV

Course resources

Textbook


Deitel & Deitel, C#, How to Program, 1st ed.
Software
Operating system: NT, 2000, XP (Pro)
 Visual Studio .NET (aka VS7)
 The .NET framework


Class Schedule
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Syllabus V

Class policies






Late work
Absences
Academic honesty
Disruptive behavior
Disabilities
Changes to the syllabus
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Computing Platforms I

The most basic "platform" is the hardware





Suppose we have a particular hardware platform




Memory
A CPU (fetch,decode,execute,store)
Input and output devices
Secondary storage and other peripherals
How do we display output and read input?
Store files on disk?
Load and start programs running?
We need some minimal software : BIOS (firmware)
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Computing Platforms II

BIOS provides minimal platform to develop software




Interface to BIOS is machine language
Ability to print to screen, send data to ports
Read/write disks by accessing controller ports
Really need "control program" to sit on top of BIOS




This is the role of the operating system
Provides protocol for input and output (command-line)
Provides scheduling/submission to CPU
Provides abstraction layer on top of peripheral devices
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Computing Platforms III

Operating system provides basis for first "real" platform:
Traditional Computing Architecture
Applications
Operating System
Hardware / BIOS
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Computing Platforms IV

Let's use MS DOS as example of traditional platform

Application layer includes system software (languages)

Sometimes application layer went through O/S




You write: ch = getchar(); // read a char in C
C runtime looks for available char (internally buffered)
None available? Calls operating system to read a "block"
Sometime application layer went around O/S


You write: putchar('A');
Most MS-DOS compilers ignore BIOS/OS, write to HW
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Computing Platforms V

Problems with MS-DOS (traditional) architecture

Problems for the user




Command line is hard to learn
Programs tied to the hardware
Programs don't work together
The Programmer's Problems



Variety of different hardware (printers, etc.) to support
Excessive UI code to write (60-70%)
No "UI" abstraction equivalent to file system abstraction
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
The Windows Platform


Designed to solve problems with traditional platform
Common User Interface



Queued Input Model





Familiarity for the user
Availability for the programmer
Cooperation between programs
Sharing of hardware devices
Device Independence
Multitasking
Interprocess Communication
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Windows Architecture

Device Layer

System Layer




OS
Input
Interface
Output
Application
O/S
Device
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Win
GDI
Hardware
Page ‹#›
Windows Architecture II

OS Components




Contained in KERNELXXX.DLL/EXE
Memory manager
Dynamic loading, linking, scheduling
Input Components


Device-driver layer
The Hardware [System] Queue


Hardware interrupts
Timers, keyboard, mouse
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Windows Architecture III

User Interface Components [USERXXX.DLL/EXE]

Windows



Window classes, instance, & child windows
Menus & dialogs
Output Components - GDI [GDIXXX.DLL/EXE]





Graphics Device Interface
The Device Context
Coordinate transformation
Drawing primitives
Device drivers
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Writing Windows Programs

Application Programming Interfaces (API)


Win16 - Windows 3.1 [Segmented Memory]
Win32 - Windows 95/98/NT/2000/XP [Flat Memory]
Differences between 9X & NT families
 Unsupported functions [Security, etc.]
 Unicode vs ASCII


All windows programs use the API, but not all equal



API is C-centric, some things are difficult in VB
Programs are monolithic, can't be part VB, part C
MS .NET is "unified API" on top of Win32
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
.NET Blueprint (Microsoft's Vision)
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
.NET (From our perspective)

An execution environment




A class library


Called the CLR
Converts MSIL to
machine code
Responsible for
security, etc.
Common to VB, C#
"managed" C++, etc.
Translators or languages
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
What are .NET Programs?

.NET programs can be written in any language



All .NET languages use the same class library




C#, VB.NET, and Managed C++ come with VS
Next version will also have J#
Don't use standard VB or C++ library
.NET Framework Class Library replaces built-in library
Means you always write Console.Writeln("Hi");
Called the .NET Foundation Class Library (FCL)

Rich set of features -- similar to Java Class Libraries
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
The Framework Class Libraries
System.Web
System.Windows.Forms
Services
Description
UI
HtmlControls
Discovery
WebControls
Design
ComponentModel
System.Drawing
Protocols
Caching
Security
Drawing2D
Printing
Configuration
SessionState
Imaging
Text
System.Data
System.Xml
SqlClient
OleDb
XSL
Serialization
SqlTypes
Common
XPath
Schema
System
Collections
IO
Security
Configuration
Net
ServiceProcess
Diagnostics
Reflection
Text
Remoting
Globalization
Resources
Threading
Serialization
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Runtime
InteropServices
Page ‹#›
MSIL and Common Type System

All .NET languages produce "virtual" machine language

Called MSIL or Microsoft Intermediate Language



Similar to Java bytecode targets the Java Virtual Machine
MSIL is converted to machine code when loaded
All .NET languages use a Common Type System (CTS)




Language-specific types are mapped to .NET types
Value types : primitives and structs (records)
Reference types : objects, pointers, interfaces
How cross-language interoperability is achieved
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
How Translation Works in .NET
VB
C#
Script
Assembly:
MSIL + Metadata
Install time
Code Gen
...
Development
Deployment
Common
Language
Runtime
Stephen Gilbert © 2002-2005
JIT
Code Gen
Native
Code
CS 140 - C# Programming
Page ‹#›
Result is Managed by CLR

CLR (Common Language Runtime) is heart of .NET


ALL .NET Code is Managed
.NET cannot run unmanaged code or use legacy library



You cannot write: cout << "Hi"; for instance
You can, currently, run both .NET and unmanaged code
What does CLR do?




Provides compilation and class loading
Handles security, range-checking, exceptions
Provides garbage collection
Thread support, class library interfaces, COM support
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
The Parts of the CLR
Base Class Library Support
Thread Support
COM Marshaler
Type Checker
Exception Manager
Security Engine
Debug Engine
IL to Native
Compilers
Code
Manager
Garbage
Collector
Class Loader
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
What is Visual Studio .NET?

Visual Studio .NET


Integrated Development Environment (IDE)
Program in a variety of .NET languages


C#, VB.NET, Managed C++
Tools to edit and manipulate several file types
Source code editor
 Debugger
 Compiler
 Visual GUI builder
 Resource editor

Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
The Start Page

The Start Page


Links on left side
Getting Started screen
Links to recent projects
 The Open Project button
 The New Project button


The What's New screen

Updates for Visual Studio
Code samples
 New tools
 Tutorials and Tips

Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Start-Page Links II

The Online Community screen


The Headlines screen


New articles, white-papers, etc.
The Search Online screen


Newsgroups, user groups, chats, components
Search screen for MSDN library
The Downloads screen

Updates and code samples
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Start-Page Links III

The XML Web Services screen

Information about Web services (Chapter 21)


The Web Hosting screen


Reusable services available over the Internet
Programmers can post software for public use
The My Profile screen

Allows Visual Studio .NET customization
Keyboard preferences
 Window layout preferences

Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Our First App

Let's build the program from Section 2.6 (p.44)




Form to hold other controls
Label to display text
PictureBox to display
a picture
No code needed to create
this program
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 1 : Create the Project

Click New Project button or use File > New > Project
Project type
Project name
Click to change
project location
Project location
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
The New Projects Dialog

New project dialog

C# .NET project


C# .NET solution


Group of related files, images, and documentations
Group of projects creating one or a group of applications
Windows Applications

GUI applications that run under the Windows OS
Microsoft Word
 Microsoft Internet Explorer

Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Name and Location

Make the project a C# Windows Application

Name it ASimpleProject

Store it on your
local folder (U:\)
Select project location
Click to set project location
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
IDE Tour I
menu
title bar
tabs
menu bar
active tab
Solution
Explorer
Form
(windows
application)
Stephen Gilbert © 2002-2005
Properties
window
CS 140 - C# Programming
Page ‹#›
IDE Tour II - Form & Tabs

IDE after a new project


"Design Mode" supports form-based visual design for all
.NET languages. (Like Visual Basic)
The form
Grey rectangle in window
 Represents the project’s main window
 Part of the GUI or Graphical User Interface


Tabs
One tab appears for each open document
 Used to save space in the IDE

Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
IDE Tour III - Menus & Toolbar

The menu bar

Commands for developing and executing programs




Create new project using File > New > Project
Certain menu options only appear in specific IDE modes
Each menu is summarized in Fig. 2.5 (page 38)
The toolbar : common commands


Note down arrows for additional commands
Display tool tips by
hovering mouse
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
IDE Tour IV - Solution Explorer

A "Solution" is .NET-speak for multi-project, project


Project can have multiple files, all same language
Solution Explorer is the "file manager" for solution
Use it to navigate through projects/files
 Expand/collapse like Windows Explorer



Can also double click on the file name to expand/collapse
Solution Explorer toolbar
Refresh icon reloads
files in the solution
 Display All Files icon
shows hidden files

Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
IDE Tour VI - Properties Window

Manipulate the properties of
a form or control

Each control has its
own set of properties




Size, color, text, or position
Left column property, right value
Users alter controls visually
without writing code
component selection dropdown
list shows what control is being
altered and controls available
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 2 : Change the Text Property

Let's change our form’s title bar



Use the Properties window
Locate the Text property
Set the value to: A Simple Program
Name and type of object
Selected property
Property value
Property description
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 3 : Resize the Form

Click and drag one of the forms size handles


Enabled handles are white, disabled ones are gray
The grid in the background will not appear in the solution
Title bar
grid
Disabled sizing handle
Mouse pointer over
a sizing handle
Enabled sizing handle
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 4 : Change the Background

Determined by the BackColor property


Dropdown arrow is used to set the color
Change it to Yellow
on Custom tab
Custom palette
Down
arrow
Current color
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Adding a Component
toolbox group

Want to add a Label control to the form



Controls can be dragged to the
controls
form from the Toolbox
Also added to the form by double clicking
The Toolbox contains reusable controls




Controls customize the form
Visual programming allows
‘drag and drop’ of controls
Pointer icon deselects current control
No tool tips, each icon is labeled
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
scroll arrow
Page ‹#›
More Toolbox Info

Toolbox is organized into groups

Toolbox can be hidden on left side of IDE


Mouse over it to expand it, move mouse to hid
The pin icon is used disable auto hide
close button
toggle auto hide
mouse over window name
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 5 : Add a Label

Open the Toolbox

Drag and drop the Label
control on the form

Set the label’s text


Use the Text property
Position by dragging, or
use menu command
Format >
Center In Form > Horizontal
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 6 : Change Font

Set the label’s font size



The Font property
changes label’s text
Select font from
standard Windows
font selection
dialog
Make the text
24 Point Sans Serif
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 7 : Align Label on Form

Use the TextAlign property to align the label


Align it Top-Center by clicking on the appropriate bump
Doesn't seem to resize, does it?
Text alignment option
Top-center alignment option
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 8 : Add the PictureBox

Drag PictureBox control to the form


Used to display pictures
Insert an image



Use Image property
Click ellipsis, pick file
of type .gif, .jpeg, or .png
Resize box to fit the
entire image
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Step 9 : Save, Run, & Quit

Save the project


Run the project


Use Toolbar Save All or
File > Save All from Menu
Use Toolbar Start (VCR), or
Click Debug > Start or
Press the F5 key
To stop, just close the window
or press Stop button in
Debug toolbar
Stephen Gilbert © 2002-2005
CS 140 - C# Programming
Page ‹#›
Descargar

Computer Science 140 C# Programming