chapter 01

36
1-1 lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh

Upload: terry-yoast

Post on 26-May-2015

1.151 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Chapter 01

1-1

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

Page 2: Chapter 01

Introduction to Visual Basic.NET 2005

Chapter11

McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Page 3: Chapter 01

1-3

Chapter Objectives (1 of 2)

Describe the process of visual program design and development

Explain the term object-oriented programmingExplain the concepts of classes, objects, properties,

methods, and eventsList and describe the three steps for writing a Visual

Basic projectDescribe the various files that make up a Visual Basic

project

Page 4: Chapter 01

1-4

Chapter Objectives (2 of 2)

Identify the elements in the Visual Studio environment

Define design time, run time, and debug timeWrite, run, save, print, and modify your first Visual

Basic projectIdentify syntax errors, run-time errors, and logic

errorsUse Auto Correct to correct syntax errorsLook up Visual Basic topic in Help

Page 5: Chapter 01

1-5

Writing Windows Applications with VB(1 of 2)

Windows Graphical User (GUI) InterfaceDefines how elements look and function

Page 6: Chapter 01

1-6

Writing Windows Applications with VB(2 of 2)

Elements are called controls and are added using a toolbox

Windows are called forms

Page 7: Chapter 01

1-7

Programming Languages-Procedural, Event Driven and Object Oriented

Procedural—Cobol, Fortran, BasicProgram specifies exact sequence of all operations

Event Driven (VB 6.0 and previous)Contain some elements of Object oriented programming but

not all

Object Oriented Programming (OOP) (VB .NET)User controls sequence

Click eventDouble Click eventChange event

Page 8: Chapter 01

1-8

The Object Model (1 of 2)

In VB you will work with objects, which have properties, methods, and events. Each object is based on a class.

Objects equate to NounsForms are windowsControls are components contained inside a form

Properties equate to AdjectivesColor or size of a Form

Methods are like VerbsTypical methods include Close, Show and Clear

Page 9: Chapter 01

1-9

Object Model (2 of 2)

Events occur when the user takes actionUser clicks a button, User moves a form

Classes are templates used to create a new objectClasses contain the definition of all available properties,

methods, and eventsEach new object created is based on a class

Creating three new buttons makes each button a instance of the Button class

Page 10: Chapter 01

1-10

Object Model Analogy

Class = automobileProperties = make, model, color, yearObject = each individual car

Object is also an Instance of the automobile class

Methods = start, stop, speedup, slowdownEvents = car arrives, car crashes

Page 11: Chapter 01

1-11

Visual Studio .NET

Included in Visual Studio .NET 2005Visual Basic (can also be purchased separately)Visual C++C# (C sharp)J# (J sharp).NET 2.0 Framework

Visual Studio .NET EditionsStandardProfessionalEnterprise DeveloperEnterprise Architect

Page 12: Chapter 01

1-12

Writing Visual Basic Projects

There is a three-step process when writing a Visual Basic application—you set up the user interface, define the properties and then create the code

Planning

1. Design the User Interface

2. Plan the Properties

3. Plan the Basic Code; follow the language syntax rules; use pseudocode (English expression or comment describing action) then you move on to

Programming (and use the same three step process)1. Define the User Interface 2. Set the properties3. Write the Basic code

Page 13: Chapter 01

1-13

VB Application Files

One Solution File—think ofone solution file equals one project .sln

Solution User Options File .suo

Form Files .vb

Resource File for the Form .resx

Project Files .vbproj

Project User Options File .vbproj.user

Application configuration File .app.config

Once a project is run several more files are created by the system. The only file that is opened directly is the solution file.

Page 14: Chapter 01

1-14

Visual Studio Environment

The Visual Studio environment is where you create and test your projects-in Visual Studio it is called an

Integrated Development Environment (IDE) consists of various tools including:

Form DesignerEditor for entering codeCompilerDebuggerObject BrowserHelp facility

Page 15: Chapter 01

1-15

Default Environment Settings

Visual Studio 2005 provides a new option that allows the programmer to select the default profile for the IDE

Page 16: Chapter 01

1-16

The IDE Initial Screen

The Visual Studio IDE with the Start Page open, as it first appears in Windows XP, without an open project.

Page 17: Chapter 01

1-17

IDE Main Window

ToolbarsDocument WindowForm DesignerSolution Explorer Window Properties WindowToolbox Help

Page 18: Chapter 01

1-18

ToolBox

You can scroll to view more controls To sort the tools in the toolbox:

•Right-click the toolbox and select•Sort Items Alphabetically from the context menu (shortcut menu).

Page 19: Chapter 01

1-19

Modes

Design Time--used when designing the user interface and writing code

Run Time--used when testing and running a projectBreak Time--if/when receiving a run-time error or

pause error

“Look at the Title Bar”

Page 20: Chapter 01

1-20

Writing Your First Visual Basic ProjectSetting Up the Project

1

2

3

Hello World Project

Page 21: Chapter 01

1-21

Planning the Project

Design the user interfaceSet up the form

Resize the formPlace a label and a button

control on the form using the

toolboxLock the Controls in place

After the user interface is

designed, the next step is to

set the properties

Page 22: Chapter 01

1-22

Setting Properties

Label 1Name messageLabelText leave blank

Button 1Name pushButtonText Push Me

Button 2Name exitButtonText Exit

FormName helloFormText Hello World by

your name

Page 23: Chapter 01

1-23

Setting the Form Properties

The default startup object is Form1

The name of the form should always be changed to adhere to naming rules

The properties window shows the files properties

Page 24: Chapter 01

1-24

Writing the Code

While the project is running the user can perform actions

Each action by the user causes an event to occurWrite code for the events you care about; the

events you want to respond to with codeCode is written as event proceduresVB will ignore events for which you do not write

codeVB will automatically name event procedures as

the object name, an underscore(_) and the name of the event

Page 25: Chapter 01

1-25

More on Writing the Code

When writing the code for your first project you will use the following:Remark StatementAssignment StatementEnding a ProgramEditor Window

Page 26: Chapter 01

1-26

'Display the Hello World message.

Remark Statement

Also known as Comment, used for documentation; every procedure should begin with a remark statement providing explanation

Non-executableAutomatically colored Green in EditorBegins with an apostrophe ( ' )

On a separate line from executable codeAt the right end of a line of executable code

Page 27: Chapter 01

1-27

messageLabel.Text=" Hello World "

Assignment Statement

Assigns a value to a property or variableOperates from right to left- the value appearing on the

right side of the equal sign is assigned to the property named on the left of the equal sign

Enclose text strings in quotation marks (" ")

Page 28: Chapter 01

1-28

Ending a Program

Methods always have parentheses (this will help you distinguish them from Properties which never have parentheses)

To execute a method of an object you write:

Object.Method()Current Form may be referenced as Me

Me.Close( )

Page 29: Chapter 01

1-29

Editor Window

Declarations SectionClass listMethod list

Page 30: Chapter 01

1-30

Run, Save, Modify, Print, Test, Debug and Execute

Run ProjectDebug Menu, StartStart (F5)

Start Without Debugging (CTRL+F5)

Save Project - File Menu, Save AllModify Project if neededPrint the CodeCorrect any Errors and Rerun

When you start executing your program, the first step is called compiling, which means that the VB statements are converted to Microsoft Intermediate Language (MSIL). Your goal is to have no errors during the compile process: a clean compile.

"Help is always available from the Help Menu or by pressing F1."

Page 31: Chapter 01

1-31

Print the Code

File Menu, PrintPrints complete code listingUses arrow symbol to denote line continuation

Page 32: Chapter 01

1-32

Finding and Fixing Errors

Syntax ErrorsBreaks VB’s rules for punctuation, format or spellingSmart editor finds most syntax errors, compiler finds the restThe editor identifies a syntax error with a squiggly blue line

and you can point to an error to pop up the error message.You can display the Error List window and line numbers in the

source code to help locate the error lines.Run-Time Errors

Statements that fail to execute such as impossible arithmetic operations

Logic ErrorsProject runs but produces incorrect results

Page 33: Chapter 01

1-33

Naming Rules and Conventions

Have a set of standards and always follow themNo spaces, punctuation marks or reserved wordsUse camel casing

ExamplesmessageLabelexitButtondataEntryFormpaymentAmountTextBox

Page 34: Chapter 01

1-34

Recommended Naming Conventions for VB Objects

Object Class Example

Form dataEntryForm

Button exitButton

Label totalLabel

TextBox paymentAmountTextbox

Radio button boldRadiobutton

CheckBox printSummaryCheckBox

Horizontal Scroll Bar rateHorizontalScrollBar

Vertical Scroll Bar temperatureVerticalScrollBar

PictureBox landscapePictureBox

ComboBox bookListComboBox

ListBox ingredientsListBox

SoundPlayer introPageSoundPlayer

Page 35: Chapter 01

1-35

Visual Studio Help Additional Info (1 of 2)

Visual Studio has an extensive Help facility,Filter MSDN help to display VB topics onlyRun MSDN from hard drive, CD or WebYou can access MSDN on the Web at

http://msdn.microsoft.com

The Help system display is greatly changed and improved in Visual Studio 2005. You view the Help topics in a separate window from the VS IDE, so you can have both windows open at the same time.

Page 36: Chapter 01

1-36

Visual Studio Help Additional Info (2 of 2)

When you choose How Do I, Search, Contents, Index, or Help Favorites from the Help menu, a new window opens on top of the IDE window. You can switch from one window to the other, or resize the windows to view both on the screen if your screen is large enough.