102
Teaching an Introductory Programming Class Using Visual Basic .NET

Teaching an Introductory Programming Class Using Visual Basic.NET

Embed Size (px)

Citation preview

Page 1: Teaching an Introductory Programming Class Using Visual Basic.NET

Teaching an Introductory Programming Class

Using Visual Basic .NET

Page 2: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 2

We are...Anita C. MillspaughProfessor, Computer Information SystemsMt. San Antonio college

Julia Case BradleyProfessor Emeritus, Computer Information SystemsMt. San Antonio College

Bradley/Millspaugh texts:VB 4, VB 5, VB 6, Advanced VB, Javaand the new VB .NET

Page 3: Teaching an Introductory Programming Class Using Visual Basic.NET

Slide 3Spring 2002

You…

Teach Visual Basic 6?

Are planning to teach Visual Basic .NET?

Have taught an OOP language

such as Java, SmallTalk, or C++?

Page 4: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 4

Our Goals for Today

Help you transition from VB 6 to VB .NET

Look at the differences between Visual Basic 6: object based; event driven Visual Basic .NET: object oriented

Introduce the new Visual Studio IDE (integrated development environment)

Show the changes to the language and the new features

Page 5: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 5

This is an introductory programming course

Main topics: Program planning Programming logic Variables, constants, calculations, decisions,

loops, data files Language syntax Coding standards Testing and debugging

The VB .NET course still covers these topics, with OOP concepts embedded throughout.

Our Approach

Page 6: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 6

What is the .NET Framework?

Platform for building and running applications

Designed for cross-platform compatibility

Common Language Runtime (CLR)

Share methods and objects from different languages

Many languages to choose from — VB, C#, COBOL, Fortran, J# …

Page 7: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 7

A new mind set

New keywords:InheritsOverridesOverloads

Terminology introduced in Chapter 1

Concepts covered in Chapter 6

The Biggest Change in VB .NET — OOP

Page 8: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 8

The Second Biggest Change — Web Forms

Programming for the Web vs. Windows

New set of controls

Client side vs. server side

Web page layout

Very simple projects presented in Chapter 9

Page 9: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 9

The Third Biggest Change — Data File Handling

Database uses ADO .NET (Chapter 10)

No Data control

“Simple” program is more difficult

Binding is more robust and likely to be used by “real programmers”

Must write code for navigation

Simple data files use streams (Chapter 11)

Page 10: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 10

Chapter 1 — IntroductionTopics Covered

Objects, Methods, and Properties

The Visual Studio IDE

Planning a Project

Creating a Project

Labels and Buttons

Code

Syntax and Logic Errors

Help

Page 11: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 11

Chapter 1 — Changes New IDE

Page 12: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 12

Chapter 1 (continued)

TerminologyVS IDESolution ExplorerForm DesignerEditor window

Tabbed display is the default for the IDE

AutoHide toolbox; can be “pinned” open

Page 13: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 13

Chapter 1 (continued)

PushPin

Locked Control

TabbedWindow

Page 14: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 14

Chapter 1 (continued)

A solution can contain multiple projects

Forms, classes, and code modules have a .vb extension

The IDE automatically creates a folder for the project

Page 15: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 15

Form Designer

Similar to VB 6

Double-Click a control in the toolbox: New default-sized control Appears on top of a selected control Top-left of the form if no control is selected

Locked controls have a dark border when selected

Command Buttons are now Buttons Use “btn” for prefix No more Caption property — now it is Text

Chapter 1 (continued)

Page 16: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 16

The Editor window (formerly the Code window)

Lots more IntelliSense help; can be confusing

Declarations section replaces General Declarations

Class List and Method List

- Selecting the object and event is somewhat different

Collapsible Regions in code

Chapter 1 (continued)

Page 17: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 17

Chapter 1 (continued)

CollapsedRegion

CollapsedProcedure

ClassList

Method ListTabs

Page 18: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 18

Chapter 1 (continued)

Complications:

Changing the name of the form requires the project’s startup object to be changed

PrintForm is no longer supported

IDE Print does not offer Form Image or Form Text

No edit-and-continue execution

– Must restart (recompile) if edits are made during break time

Page 19: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 19

Chapter 1 (continued)

Form and controls no longer have Caption property

Forms, buttons, labels all use the Text property

The Alignment property becomes TextAlign

The maximum length of identifiers is 16,383

All new Help — MSDN

Redesigned Object Browser

Me.Close()

Page 20: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 20

Chapter 2 — More Controls Topics Covered

Check Boxes

Radio Buttons

Group Boxes

Picture Boxes

Designing an Interface

Setting the Tab Order

Setting up keyboard access keys (shortcuts)

Page 21: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 21

Chapter 2 — Changes

No more Image control, Line control, or Shape control

Use PictureBox for graphics; Label for lines

OptionButton becomes RadioButton

Frame becomes GroupBox

AcceptButton and CancelButton are properties of the form

Set the form’s StartPosition property; no more Form Layout Window

Page 22: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 22

Chapter 2 (continued) PictureBox controls

Use Image property to hold a graphic

Set SizeMode property to StretchImage —replaces Stretch property of Images

CheckBox controls

Value property becomes the Checked property (Boolean)

It’s event is CheckChanged

Great new method for setting tab order

Page 23: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 23

Chapter 2 (continued)View / Tab Order

Page 24: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 24

New component tray holds non-visible controls

To add ToolTips, add a ToolTip control to the component tray

New ToolTip on ToolTip1 property is added to the form and all controls

Text boxes have a Clear methodtxtName.Clear()txtName.Text = “”

Focus method replaces SetFocus

txtName.Focus()

Chapter 2 (continued)

Page 25: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 25

Many colors available through the Color class

lblMessage.ForeColor = Color.Blue

Color.Red

Color.Yellow

Color.Aquamarine

Color.Bisque

Color.Chocolate

Color.Cadetblue

… and dozens more

Chapter 2 (continued)

Page 26: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 26

Chapter 3 — Variables,Constants and Calculations

Topics Covered

Variables, constants, calculations

Data types

Converting text input to numeric

Formatting output

Error Handling (Exceptions)

Message Boxes

Page 27: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 27

Chapter 3 — Changes

Strongly Typed Data

Option Strict On

– Enforces strong data typing

Data Types

Short (old Integer)

Integer (old Long)

Decimal (Currency is gone)

Object ( Variant is gone)

Date (replaces DateTime)

Page 28: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 28

Chapter 3 (continued) Initialize a variable at declaration

Dim intMax As Integer = 100IDim decRate As Decimal = 0.08D

Declare multiple variables at onceDim intCount, intNumber As Integer

Convert all input to correct data type(Do not use Val function)

decSale = CDec(txtSale.Text)

CDec and CInt — Can parse: $ , () + -

CInt rounds to nearest EVEN number

Page 29: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 29

Chapter 3 (continued) New assignment Operators += –= *= /= &=

decTotal += decSale

Structured Exception HandlingTry

(Code that could cause an exception)Catch [excName As excType]

(Code to handle the exception)Finally

(Code to execute in any case)End Try

We can catch input errors before covering If statements!

Page 30: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 30

Chapter 3 (continued)

MessageBox class

MessageBox.Show(Arguments)

Several choices for argument list

Introduces concept of Overloaded functions

Multiple signatures to choose from

MessageBox.Show(TextMessage)MessageBox.Show(TextMessage, TitlebarText)MessageBox.Show(TextMessage, TitlebarText, _

MessageBoxButtons)MessageBox.Show(TextMessage, TitlebarText, _

MessageBoxButtons, MessageBoxIcon)

Page 31: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 31

Chapter 3 (continued)

MessageBox Icons and buttons

Enumeration of constants

Page 32: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 32

Chapter 4 — Decisions and Conditions Topics Covered

Conditions / Relational Operators

Block Ifs

Compound Conditions

Validation

Message Boxes that return the user response

Debugging

Page 33: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 33

Chapter 4 — Changes

Block IF unchanged

Conditions — always compare like data types

If CInt(txtInput.Text) > 10I Then

String ToUpper and ToLower methods (replace UCase and LCase functions)

If txtInput.Text.ToUpper = “YES”

Page 34: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 34

Chapter 4 (continued) MessageBox.Show method returns an object of

type DialogResult

Dim dgrResponse As DialogResult

dgrResponse = MessageBox.Show(”Shall we?", _ "The Question", MessageBoxButtons.YesNo, _ MessageBoxIcon.Question)

If dgrResponse = DialogResult.Yes ThenMessageBox.Show("You said Yes!")

ElseMessageBox.Show("You said No!")

End If

Page 35: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 35

Chapter 4 (continued) Replace vbCrLf, vbCr, vbLf with constants in the

ControlChars class

ControlChars.NewLine

Dim strMultilineMessage As String

strMultilineMessage = “Line 1” & _ ControlChars.NewLine & “Line 2”

Page 36: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 36

Chapter 4 (continued) Calling a procedure requires parentheses

VB 6 Way:ClearControls

or Call ClearControls()

VB .NET Way:[Call] ClearControls()

Calling an event procedure requires two argumentsPrivate Sub btnExit_Click(ByVal sender _ As System.Object, ByVal e _ As System.EventArgs) Handles btnExit.Click

Call the event procedure: [Call] btnExit_Click(sender, e)

Page 37: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 37

Chapter 4 (continued) Debug.WriteLine

(Replaces Debug.Print)

Debug.WriteLine(“I got this far”)

Clear the Output Window:

Right-click / Clear All

New Autos window — shows current value of variables and objects within approx. 6 statements

Immediate window only valid at run time

Keyboard shortcut for Step Into — F11

Page 38: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 38

Chapter 5 — Menus, Sub Procedures, and

Sub Functions Topics Covered

Creating Menus and Menu Items

Creating ContextMenus

Displaying Common Dialog Boxes

Writing General Procedures

Page 39: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 39

Chapter 5 — Changes Create a menu by adding a MainMenu control to the

component tray

Great new Menu Designer replaces the Menu Editor

Page 40: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 40

Chapter 5 (continued)

For Context menus, add a ContextMenu control

A ContextMenu uses the same Menu Designer as a MainMenu

Attach a context menu by setting the ContextMenu property of a control or the form

Page 41: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 41

Chapter 5 (continued)

New ColorDialog and FontDialog controls

Added to the component tray

Use the control’s Show method to display

Can set and retrieve properties of the control

With dlgColor.Color = frmMain.BackColor

.ShowDialog().frmMain.BackColor = .Color

End With

Page 42: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 42

Chapter 5 (continued)

The FontDialog control 'Change the font for the total label

With dlgFont.Font = lblTotal.Font.ShowDialog()lblTotal.Font = .Font

End With

Page 43: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 43

Chapter 5 (continued) Arguments are passed ByVal (by default)

The Editor adds ByVal if you leave it out

New Return statement

Can still set function name to value Better to use the Return statement

Private Function CalculateSum( _ByVal intNum1 As Integer, _ByVal intNum2 As Integer) As Integer'Calculate the sum of the numbers

Return intNum1 + intNum2End Function

Page 44: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 44

Chapter 6 — OOP Topics Covered

Object-oriented terminology

Multitier applications

Class vs. Object

Visibility — Public, Private, Protected

Constructors

Instantiation

Inheritance

Page 45: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 45

Chapter 6 — Changes Namespaces

Organize class libraries Provide unique naming region Can create your own namespace

Specifying namespaceslblMessage.Font = New System.Drawing.Font( _

"Arial", 12) or

Imports System.Drawing.Font...lblMessage.Font = New Font("Arial", 12)

Page 46: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 46

Chapter 6 (continued)

Classes vs. Objects Use New keyword to instantiate For exception handling, declare the

name at the module level and instantiate in a Try/Catch block

Encapsulation Classes hide implementation details Only the Public properties and methods

can be seen by other classes

Page 47: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 47

Chapter 6 (continued) Inheritance

New class based on existing class

Page 48: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 48

Chapter 6 (continued) Inheritance (continued)

All public properties and methods are inherited

Visual inheritance — can inherit a form New keywords to support inheritance

– Inherits– Overrides– OverridablePublic Class StudentBookSale

Inherits BookSale

Page 49: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 49

Chapter 6 (continued) Inheritance (continued)

In base class

Public Overridable Function _ ExtendedPrice() As Decimal

In derived class

Overrides Function ExtendedPrice() _

As Decimal

Page 50: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 50

Chapter 6 (continued) Polymorphism

Consistent naming, so that many classes can have the identically-named methods that perform an action appropriate for the object

– Example: The Add method for collections, lists, arrays, database

Identically named methods in a class, with different implementations based on the arguments

– Keyword: Overloads – Example: MessageBox.Show method, which has

many different argument lists (signatures)

Page 51: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 51

Chapter 6 (continued) New Format for Property Procedures

Private mstrLastName As String

Public Property LastName As StringGet LastName = mstrLastName

End GetSet (ByVal Value As String)mstrLastName = Value

End SetEnd Property

Page 52: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 52

Chapter 6 (continued)

ReadOnly and WriteOnly keywords for properties

Private mdecTotalPay As Decimal

ReadOnly Property SalesTotal() As DecimalGet

TotalPay = mdecTotalPayEnd Get

End Property

Page 53: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 53

Chapter 6 (continued)

Shared members — one variable forfor all instances of the class

Often used for totals and counts

'SalesTotal property Private Shared mdecSalesTotal As Decimal

'SalesCount property

Private Shared mintSalesCount As Integer

Page 54: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 54

Chapter 6 (continued) Constructors — New() method

Replaces ClassInitialize procedure Constructors are not inherited

– Must write your own New() procedure– First statement should be

MyBase.New() Constructors can be overloaded

Parameterized Constructors

mBookSale = New BookSale(txtTitle.Text, _

CInt(txtQuantity.Text), _

CDec(txtPrice.Text))

Page 55: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 55

Chapter 6 (continued)

Visibility Private Public Protected

– Accessible only inside the class or a derived class

Garbage Collection is automatic Don’t set objects to Nothing in code

Page 56: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 56

Chapter 7 — Lists, Loops, and Printing

Topics Covered

List Boxes and Combo Boxes Items Collection Methods and Properties of Lists Do Loops For/Next Loops Printing and Print Preview

Page 57: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 57

Chapter 7 — Changes

The DropDownStyle property of ComboBoxes replaces the Style property

No List property or ItemData property Lists have an Items collection

Items.Add(value) Items.Insert(position) Items.Remove(text string) Items.RemoveAt(index) Items.Clear() Items.Count

SelectedIndex property of control

Page 58: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 58

Chapter 7 (continued)lstSchools.Items.Add("Harvard")

cboMajors.Items.Insert(1, cboMajors.Text)

intTotalItems = lstItem.Items.Count

strSelectedFlavor = _ lstFlavor.Items(lstFlavor.SelectedIndex)

cboCoffee.Items.RemoveAt( _cboCoffee.SelectedIndex)

cboCoffee.Items.Remove(cboCoffee.Text)

Page 59: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 59

Chapter 7 (continued) New events

SelectedIndexChanged– Event to use when the user makes a selection– Click event no longer supported for lists

TextChanged– One event for each keystroke

Enter and Leave replace GotFocus and LostFocus events

FindString and FindStringExact methods Not covered in this text

SelectAll method For text boxes or text portion of a combo box

Page 60: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 60

Chapter 7 (continued) Printing is completely changed

No more Printer.Print, Form.Print, PictureBox.Print All text printing is performed with a DrawString method

on a Graphics object Add a PrintDocument control to the component tray

Write all printing logic in the control’s PrintPage event procedure

The control fires the event once for each page to print

The program responds in the PrintPage event procedure

Page 61: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 61

Chapter 7 (continued) Begin a printing operation:

Private Sub btnPrint_Click(ByVal _sender As System.Object, ByVal _e As System.EventArgs) _Handles btnPrint.Click'Print output on the printer

'Start the print processprtDocument.Print()

End Sub

Page 62: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 62

Chapter 7 (continued) Set up print page in the control’s PrintPage event:

Private Sub prtDocument_PrintPage( _ByVal sender As Object, ByVal e As _System.Drawing.Printing.PrintPageEventArgs)_Handles prtDocument.PrintPage

'Set up actual output to print

End Sub

Page 63: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 63

Chapter 7 (continued) Use the DrawString method in the control’s PrintPage event:

'Declarations at the top of the procedure

Dim fntPrintFont As New Font("Arial", 12)

'More declarations here

'Print a line

e.Graphics.DrawString(strPrintLine, _ fntPrintFont, Brushes.Black, _

sngPrintX, sngPrintY)

Page 64: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 64

Chapter 7 (continued) Graphics objects

Must define fonts and brushes to create text Set up print lines with the DrawString method Print Preview

Add a PrintPreviewDialog control to the form Assign the PrintDocument control to the

PrintPreviewDialog’s Document property The same PrintPage event procedure is used to set

up the print output

ppdPrintPreview.Document = prtDocument

ppdPrintPreview.ShowDialog()

Page 65: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 65

Chapter 7 (continued)

The Good News: Do Loops and For / Next Loops are unchanged

Page 66: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 66

Chapter 8 — Arrays Topics Covered

Select Case structure Shared events Arrays Structures Table lookup Combining list controls and arrays Multi-dimensional arrays

Page 67: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 67

Chapter 8 — Changes

Shared events (no control arrays)

Private Sub radBlue_CheckedChanged( _ByVal sender As System.Object, _

ByVal e As System.EventArgs) _ Handles radBlue.CheckedChanged,

radBlack.CheckedChanged, _ radRed.CheckedChanged, _

radWhite.CheckedChanged, _radYellow.CheckedChanged

Page 68: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 68

Chapter 8 (continued) In shared event procedure:

'Save the name of the selected buttonDim radSelected As RadioButtonradSelected = CType(sender, RadioButton)Select Case radSelected.Name

Case "radBlue"mcolorNew = Color.Blue

Case "radBlack"mcolorNew = Color.Black

Case "radRed"'… rest of code

End Select

Page 69: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 69

Chapter 8 (continued)

All arrays are zero based No Dim Array(LowerBound To UpperBound)

Declare upper subscript unlessinitializing values at declaration

Dim mstrProduct(99) As String

Dim mintValue() As Integer _= {1, 5, 12, 18, 20}

Page 70: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 70

Chapter 8 (continued)

Structures replace User-defined Types

Public Structure ProductDim strDescription As StringDim strID As StringDim intQuantity As IntegerDim decPrice As Decimal

End Structure

Page 71: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 71

Chapter 9 — Web Forms Topics Covered

Client vs. Server Running Web Forms in a browser Web controls Event structure of Web programs Layouts Validator Controls

Page 72: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 72

Chapter 9 — All New

Must install IIS before installing VS .NET Properties, methods, and events are

different from Windows Forms Examples:

– Name property is ID– Form_Load is Page_Load

Web Controls differ from Windows controls even when they have the same name

Page 73: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 73

Chapter 9 (continued) A Web project is created in Inetpub\wwwroot

Page 74: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 74

Chapter 9 (continued) Validator Controls run on the client side, eliminating

traffic to the server

Page 75: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 75

Chapter 9 (continued)

Web projects are not easy to transfer from one computer or folder to another location

Page 76: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 76

Chapter 10 — Database Topics Covered

ADO.NET XML Datasets in Windows and Web projects Record navigation Parameterized queries Updating a dataset

Page 77: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 77

Chapter 10 — Changes

No Data control Disconnected datasets with an adapter Two adapters available

SQL Server OleDb (Works well with Access files)

Must write navigation code Data is transferred in XML code

Generated automatically

Page 78: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 78

Chapter 10 (continued)

Specific file OleDbConnection object

Handles data transfer and provides data for dataset. Uses SQL to specify data to retrieve or update.

Actual data. Can contain multiple tables and relationships.

Data displays on the form in bound controls

Page 79: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 79

Chapter 10 (continued) Use the Server Explorer

Manage connections View table structure Drag tables and/or fields

to the form to set up connection and data adapter

Page 80: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 80

Chapter 10 (continued) A Configure Data Adapter wizard steps through creating

the dataset The dataset can be based on an SQL Select statement

or a stored procedure The wizard presents a Query Builder for building SQL

Select statements Similar to the Query Designer in Access Creates the Select statement Can also generate the Insert, Delete, and Update

SQL statements for updating the dataset You can preview the data from the file

Page 81: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 81

Chapter 10 (continued)

Bind the data to individual controls with the DataBindings properties

At Form_Load or Page_Load call the Fill method to generate the dataset

DataAdapterName.Fill(DataSetName) Dataset is disconnected Windows Forms — Bound controls are filled automatically Web Forms — Must execute the BindData method

Bound controls are filled when BindData executes Web Forms are static — Only current record is transferred

Page 82: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 82

Chapter 10 (continued) All new Datagrid controls

One for Windows forms; another for Web forms Navigation requires code

No MoveNext, MovePrevious methods Me.BindingContext(DsBooks1, “Books”).Position += 1

Microsoft recommends one dataset per table

Save changes back to the original data source by executing the dataset’s Update method The automatically-generated SQL statements execute

for any changed rows

– Insert, Delete, Update

Page 83: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 83

Chapter 11 — Data Files Topics Covered

Use streams to store and retrieve data Save and restore values in a list Use serialization to save the values of an object

Page 84: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 84

Chapter 11 — Changes

Data file handling is completely new Streams transfer data

StreamReader StreamWriter

Imports System.IO

Dim datPhone As StreamWriter

datPhone = New StreamWriter(strFilePath)

datPhone.WriteLine(txtName.Text)

datPhone.WriteLine(txtPhone.Text)

datPhone.Close()

Page 85: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 85

Chapter 11 (continued)

StreamReaderImports System.IO

Dim datPhone As StreamReader

datPhone = New StreamReader(strFilePath)

If datPhone.Peek <> -1 Then

lblName.Text = datPhone.ReadLine()

lblPhone.Text = datPhone.ReadLine()

End If

datPhone.Close()

Page 86: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 86

Chapter 11 (continued) OpenFileDialog

Display common dialog Allow user to browse for file

Dim dgrResult As DialogResult

dlgOpen.InitialDirectory = Application.StartupPathdgrResult = dlgOpen.ShowDialog() If dgrResult <> DialogResult.Cancel Then

'User didn't click the Cancel buttondatPhone = New StreamReader(dlgOpen.FileName)btnNext.Enabled = TruebtnNext_Click(sender, e)

End If

Page 87: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 87

Chapter 11 (continued) Serialization

Save an object to a file The object’s class must be declared as serializable In the program that creates the object:Dim datBooks As FileStream _

= New FileStream("Books", FileMode.Create)

Dim BookFormatter As BinaryFormatter _

= New BinaryFormatter()

BookFormatter.Serialize(datBooks, mBookSale)

datBooks.Close()

Page 88: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 88

Chapter 11 (continued) Deserialization — Restore an object from a file

Create a FileStream object in the Open mode Declare a Formatter object Use the Formatter's Deserialize method, converting

the input to the desired object type Transfer the fields from the object to the screen Close the stream

FileStream object has Create and Open modes

Page 89: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 89

Chapter 12 — Graphics Topics Covered

Draw shapes, lines, and filled shapes Instantiate Brush and Pen objects Simple animation Timer component Scroll bars Creating pie charts

Page 90: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 90

Chapter 12 — All New

Graphics use GDI+ for greater device independence Based on System.Drawing namespace Graphics object may be on a form or a control Pens draw lines; Brushes draw filled shapes Measurement for graphics is pixels

The Scale function is gone

Page 91: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 91

Chapter 12 (continued)

Create graphics in the form’s Paint event procedure Declare a Graphics object using the PaintEventArgs

Private Sub frmDraw_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _

Handles MyBase.Paint

'Create a graphics objectDim gr As Graphics = e.Graphics

Page 92: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 92

Chapter 12 (continued) Use a Pen to draw a line or outlined shape Use a Brush for a filled shape

Dim gr As Graphics = e.GraphicsDim penRed As New Pen(Color.Red)Dim brushBlue As New SolidBrush(Color.Blue)

'Draw a red rectanglegr.DrawRectangle(penRed, 10, 10, 30, 30)

'Draw a blue filled circlegr.FillEllipse(brushBlue, 100, 100, 50, 50)

Page 93: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 93

Chapter 12 (continued)

The SetBounds method replaces Move

SetBounds(intX, intY, intWidth, intHeight)

picPlane.SetBounds(intX, intY, _planeWidth, planeHeight)

Page 94: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 94

Chapter 12 (continued)

PictureBox controls hold many more graphic formats Can display an animated GIF file

Timer component appears in the component tray Enabled = False by default — must set to True The Tick event fires when the Interval occurs

Scroll bar changes ValueChanged replaces Change event Minimum property replaces Min Maximum property replaces Max

Page 95: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 95

Chapter 13 — Additional Topics Topics Covered

Multiple Document Interfaces (MDI) Windows Common Controls

Image Lists Toolbars Status bars Calendar

Crystal Reports

Page 96: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 96

Chapter 13 — Changes

An MDI program can have Multiple parent forms Multiple child forms Forms that operate independently

You no longer have to use a different form type as a parent Set parent’s IsMdiContainer property to True For child form, set MdiParent property in code

Dim frmChildOne As New frmChild()

frmChildOne.MdiParent = Me

Page 97: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 97

Chapter 13 (continued) To create a Window menu, set a menu’s MdiList

property to True The Arrange methods are replaced with Layout

methods

Me.LayoutMdi(MdiLayout.TileHorizontal)

Me.LayoutMdi(MdiLayout.TileVertical)

Me.LayoutMdi(MdiLayout.Cascade)

Page 98: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 98

Chapter 13 (continued)

New controls added to the chapter Image list status bar toolbar

Image List You can add and remove images after attaching

the list to a toolbar New collection editor makes adding and removing

images easier

Page 99: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 99

Chapter 13 (continued)

Toolbar Single ButtonClick event for all buttons on toolbar EventArgs argument holds index of clicked button

Select Case tlbParent.Buttons.IndexOf(e.Button)Case 0

mnuDisplayChildOne_Click(sender, e)Case 1

mnuDisplayChildTwo_Click(sender, e)End Select

Page 100: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 100

Chapter 13 (continued)

Crystal Reports are back and are more powerful No code is required to write a report You design a template and add a

CrystalReportViewer object to the form

Page 101: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 101

Questions ???

Page 102: Teaching an Introductory Programming Class Using Visual Basic.NET

Spring 2002 Slide 102

Thank You

We appreciate your being here with us.

Have fun. It’s a great product and should be fun to teach!!!