Presentation is loading. Please wait.

Presentation is loading. Please wait.

McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Introduction to Visual Basic.NET 2008 Chapter 1.

Similar presentations


Presentation on theme: "McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Introduction to Visual Basic.NET 2008 Chapter 1."— Presentation transcript:

1 McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Introduction to Visual Basic.NET 2008 Chapter 1

2 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-2 Chapter Objectives (1 of 2) Describe the process of visual program design and development. Explain the term object-oriented programming. Explain the concepts of classes, objects, properties, methods, and events. List and describe the three steps for writing a Visual Basic project. Describe the various files that make up a Visual Basic project.

3 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-3 Chapter Objectives (2 of 2) Identify the elements in the Visual Studio environment. Define design time, run time, and debug time. Write, run, save, print, and modify your first Visual Basic project. Identify syntax errors, run-time errors, and logic errors. Use Auto Correct to correct syntax errors. Look up Visual Basic topics in Help.

4 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-4 Writing Windows Applications with VB (1 of 2) Windows Graphical User (GUI) Interface –Defines how elements look and function Text boxes Check box Buttons Picture box Radio buttons Label Message box

5 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-5 Writing Windows Applications with VB (2 of 2) Elements are called controls and are added using a toolbox. Windows are called forms.

6 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-6 Programming Languages—Procedural, Event Driven, and Object Oriented Procedural—Cobol, Fortran, Basic Program specifies exact sequence of all operations. Event-Driven Programming(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 event – Double Click event – Change event

7 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-7 Chapter 1: Introduction to Visual Basic 2005 Programming 7 Lego Building Blocks

8 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-8 The Object Model (1 of 2) In VB, you will work with objects that have properties, methods, and events. Each object is based on a class. Objects equate to Nouns. –Forms are windows. –Controls are components contained inside a form. Properties equate to Adjectives. –Color or size of a Form Methods are like Verbs. –Typical methods include Close, Show and Clear

9 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-9 Object Model (2 of 2) Events occur when the user takes action. –User clicks a button, User moves a form Classes are templates used to create a new object. –Classes contain the definition of all available properties, methods, and events. –Each new object created is based on a class. Creating three new buttons makes each button a instance of the Button class.

10 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-10 Object Model Analogy Class = automobile Properties of automobile class= make, model, color, engine, year Object = Each individual auto is an object. –Object is also an Instance of the automobile class. Methods = start, stop, speedup, slowdown Events of automobile class = Arrive, Crash

11 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-11 Visual Studio.NET Included in Visual Studio.NET 2008 Visual Basic (can also be purchased separately) Visual C++ C# (C sharp) J# (J sharp) F# (F sharp).NET 3.5 Framework Visual Studio.NET Editions Express Standard Professional Team System

12 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 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 Design the User Interface. Plan the Properties. 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) Define the User Interface. Set the properties. Write the Basic code.

13 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-13 Chapter 2: Program and Graphical User Interface Design 13 5 D’s of Programming DEFINE the problem –OIP DESIGN a solution –User Interface –OEA DEVELOP/DEBUG the solution –Enter code and test DEPLOY the solution

14 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-14 VB Application Files One Solution File—think of one solution file equals one projectHelloWorld.sln Solution User Options File HelloWorld.suo Form Files HelloForm.vb Resource File for the FormHelloForm.resx Form DesignerHelloForm.Designer.vb Project User Options FileHelloWorld.vbproj.user Once a project is run, several more files are created by the system. The only file that is opened directly is the solution file.

15 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-15 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) consisting of various tools including: Form Designer Editor for entering and modifying code Compiler Debugger Object Browser Help Facility

16 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-16 Default Environment Settings Visual Studio 2008 provides a new option that allows the programmer to select the default profile for the IDE.

17 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-17 The IDE Initial Screen The Visual Studio IDE with the Start Page open, as it first appears in Windows XP, without an open project

18 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-18 IDE Main Window Toolbars Document Window Form Designer Solution Explorer Window Properties Window Toolbox Help Document window Properties window Solution Explorer

19 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-19 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).

20 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-20 Modes Design Time — used when designing the user interface and writing code Run Time — used when testing and running a project Break Time — if/when receiving a run-time error or pause error "Look at the Title Bar"

21 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-21 Writing Your First Visual Basic Project Setting Up the Project 1 2 3 Hello World Project

22 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-22 Your First VB Program Create a program that will allow the user to press a button (Push ME) and display a message (Hello World) on the screen. When the user presses the Exit button, the program will terminate. 1-22

23 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-23 Chapter 3: Program Design and Coding 23 Define - OIP Form OUTPUTHello World message INPUTNone PROCESSDisplay message Exit program

24 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-24 Planning the Project Design the user interface. Set up the form. – Resize the form. – Place a label and a button control on the form using the toolbox. – Lock the Controls in place. After the user interface is designed, the next step is to set the properties.

25 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-25 Setting Properties Label 1 Name messageLabel Textleave blank Button 1 NamepushButton TextPush Me Button 2 NameexitButton TextExit Form NamehelloForm TextHello World by your name

26 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-26 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

27 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-27 Chapter 3: Program Design and Coding 27 Design - OEA Form OBJECTEVENTTASK(S) Exit buttonClickExit program Push Me buttonClickDisplay Message

28 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-28 Writing the Code While the project is running, the user can perform actions. Each action by the user causes an event to occur. Write code for the events you care about; the events you want to respond to with code. Code is written as event procedures. VB will ignore events for which you do not write code. VB will automatically name event procedures as the object name, an underscore(_) and the name of the event.

29 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-29 More on Writing the Code When writing the code for your first project, you will use the following: Remark Statement Assignment Statement Ending a Program Editor Window

30 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-30 Remark Statement Also known as Comment, used for documentation; every procedure should begin with a remark statement providing explanation. Non-executable Automatically colored Green in Editor Begins with an apostrophe ( ' ) On a separate line from executable code At the right end of a line of executable code 'Display the Hello World message.

31 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-31 Assignment Statement Assigns a value to a property or variable Operates 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 (" ") messageLabel.Text=" Hello World "

32 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-32 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( )

33 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-33 Editor Window Declarations Section Class list Method list

34 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-34 Run, Save, Modify, Print, Test, Debug, and Execute Run Project Open Debug Menu, Start Debugging. Start Debugging button on the toolbar. Press F5, the Start Debugging command. Save Project — File Menu, Save All. Modify Project if needed. Print the Code. Correct 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."

35 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-35 Print the Code File Menu, Print Prints complete code listing Uses arrow symbol to denote line continuation

36 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-36 Finding and Fixing Errors Syntax Errors Breaks VB’s rules for punctuation, format, or spelling Smart editor finds most syntax errors, compiler finds the rest. The 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 Errors Project runs, but produces incorrect results.

37 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-37 Naming Rules and Conventions Have a set of standards and always follow them. No spaces, punctuation marks, or reserved words Use camel casing. Examples – messageLabel – exitButton – dataEntryForm – paymentAmountTextBox

38 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-38 Recommended Naming Conventions for VB Objects Object ClassExampleHungarian Notation FormdataEntryForm frmDataEntry ButtonexitButton btnExit LabeltotalLabel lblTotal TextBoxpaymentAmountTextbox txtPaymentAmount Radio buttonboldRadiobutton rdbBoldd CheckBoxprintSummaryCheckBox chkPrintSummary Horizontal Scroll BarrateHorizontalScrollBar hsbRate Vertical Scroll BartemperatureVerticalScrollBar vsbTemperature PictureBoxlandscapePictureBox picLandscape ComboBoxbookListComboBox cboBookList ListBoxingredientsListBox lstIngredients SoundPlayerintroPageSoundPlayer sndIntroPagesound

39 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-39 Visual Studio Help Additional Info (1 of 2) Visual Studio has an extensive Help facility. Filter MSDN help to display VB topics only. Run MSDN from hard drive, CD, or Web. You can access MSDN on the Web at http://msdn.microsoft.com http://msdn.microsoft.com The Help system display is greatly changed and improved in Visual Studio 2008. You view the Help topics in a separate window from the VS IDE, so you can have both windows opened at the same time.

40 McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 1-40 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. Index Results window Main Document window Visual Studio Help Additional Info (2 of 2)


Download ppt "McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Introduction to Visual Basic.NET 2008 Chapter 1."

Similar presentations


Ads by Google