57
Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

Embed Size (px)

Citation preview

Page 1: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

Chapter 1: An Introduction to Visual Basic 2005

Programming with Microsoft Visual Basic 2005, Third Edition

Page 2: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

2Programming with Microsoft Visual Basic 2005, Third Edition

An Introduction to Visual Basic 2005 Lesson A Objectives

• Start and customize Visual Studio 2005 or Visual Basic 2005 Express Edition

• Create a Visual Basic 2005 Windows-based application

• Manage the windows in the IDE

• Set the properties of an object

• Restore a property to its default setting

Page 3: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

3Programming with Microsoft Visual Basic 2005, Third Edition

An Introduction to Visual Basic 2005 Lesson A Objectives (continued)

• Save a solution

• Close a solution

• Open an existing solution

Page 4: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

4Programming with Microsoft Visual Basic 2005, Third Edition

Starting Visual Studio 2005 or Visual Basic 2005 Express Edition

• Click Start and point to All Programs • Select one of two environments

– Microsoft Visual Studio 2005 – Microsoft Visual Basic 2005 Express Edition

• Click Window on the menu bar• Click Reset Window Layout • Click Yes at prompt to restore default layout

Page 5: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

5Programming with Microsoft Visual Basic 2005, Third Edition

Starting Visual Studio 2005 or Visual Basic 2005 Express Edition

(continued)

Figure 1-2: Microsoft Visual Basic 2005 startup screen

Page 6: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

6Programming with Microsoft Visual Basic 2005, Third Edition

Starting Visual Studio 2005 or Visual Basic 2005 Express Edition

(continued)

Figure 1-4: Purpose of the windows included in the IDE

Page 7: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

7Programming with Microsoft Visual Basic 2005, Third Edition

Creating the Copyright Screen Application

• Applications comprise solutions, projects, and files

• Solution: container storing projects and files

• Project: contains files for a part of the solution

• Template – Pattern used to create solutions and projects– Includes standard folders and files

• Windows presented are from Visual Studio 2005

Page 8: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

8Programming with Microsoft Visual Basic 2005, Third Edition

Creating the Copyright Screen Application (continued)

Figure 1-5: Illustration of a solution, project, and file

Page 9: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

9Programming with Microsoft Visual Basic 2005, Third Edition

Creating the Copyright Screen Application (continued)

Figure 1-8: Solution and Visual Basic project created in Visual Studio 2005

Page 10: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

10Programming with Microsoft Visual Basic 2005, Third Edition

Managing the Windows in the IDE

• Options to show a window– Click View on the menu bar, select a window – Hover the mouse over the tab (if auto-hidden)

• Options to close a window– Use the Close button on the title bar

• How to minimize a window to a tab– Use Auto Hide (pushpin) button on the title bar

Page 11: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

11Programming with Microsoft Visual Basic 2005, Third Edition

The Windows Form Designer Window

• Graphical user interface (GUI) – Visual representation of the application

• Windows Form Designer window– Area in which you create (or design) the GUI

• Windows Form object (or form)– Foundation for the user interface

• To provide user interaction, add objects to Form – Examples of interaction objects: buttons, text boxes

Page 12: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

12Programming with Microsoft Visual Basic 2005, Third Edition

The Windows Form Designer Window (continued)

Figure 1-11: Windows Form Designer window

Page 13: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

13Programming with Microsoft Visual Basic 2005, Third Edition

The Solution Explorer Window

• Solution Explorer window– Displays list of projects contained in current solution– Displays the items contained in each project

• Source file: a file containing program code

• Code: the program instructions

• Form file – Contains code underlying a Windows Form object– Has a file extension of .vb

Page 14: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

14Programming with Microsoft Visual Basic 2005, Third Edition

The Properties Window

• Properties: attributes of an object

• Properties window: exposes object’s properties

• Object box: contains name of selected object

• Properties list: helps identify and access properties

• Settings box: shows current values of properties

Page 15: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

15Programming with Microsoft Visual Basic 2005, Third Edition

The Properties Window (continued)

Figure 1-13: Properties window showing the properties of the Form1.vb file object

Page 16: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

16Programming with Microsoft Visual Basic 2005, Third Edition

Properties of the Windows Form Object

• Displaying properties of Windows Form object– Select Windows Form object in designer window

• Two ways to organize properties– Press Categorized or Alphabetical button

• Class definition: specifies attributes and behaviors

• Namespace: code defining a group of classes

• Dot member access operator: specifies hierarchy– Example: System.Windows.Forms.Form

Page 17: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

17Programming with Microsoft Visual Basic 2005, Third Edition

Properties of the Windows Form Object (continued)

• Name property: refers to the object in code

• Hungarian notation naming convention– First three characters represent the type of object – Remaining characters represent object’s purpose– Name is entered in camel case (for camel’s hump)– Example: frmCalculateTaxes

• New naming convention– Indicate the form purpose and class– Form names are entered using Pascal case– Example: CopyrightForm

Page 18: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

18Programming with Microsoft Visual Basic 2005, Third Edition

Properties of the Windows Form Object (continued)

• Two naming conventions for Windows applications– Name should appear in application window’s title bar– The title bar should display an appropriate name

• Text property – Controls the caption in the form’s title bar– Also appears on the application’s taskbar button– The default caption is Form1

• StartPosition property– Determines where the form first appears on screen

Page 19: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

19Programming with Microsoft Visual Basic 2005, Third Edition

Properties of the Windows Form Object (continued)

• Size property– Controls the height and width of the object– Measured in pixels by default

• Pixel: unit picture element of a screen image

• Set the value of a property using the settings box

• How to restore the default value of a property– Right-click the property name and select Reset

• BackColor property: sets the form background color

Page 20: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

20Programming with Microsoft Visual Basic 2005, Third Edition

Saving a Solution

• Save a solution at regular intervals; e.g., 10 minutes

• Options for saving a solution– Select File on the menu bar, then Save All– Click the Save All button on the Standard toolbar– Select solution’s name in Solution Explorer window– Click the Save button on the Standard toolbar

• How to determine which file will be saved– Hover the mouse on the Save button

Page 21: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

21Programming with Microsoft Visual Basic 2005, Third Edition

Closing the Current Solution

• Select File on menu bar, then Close Solution

• Impact of closing the current solution– All projects and files contained within are closed

• Unsaved changes trigger a warning prompt – You are given another chance to save your work

Page 22: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

22Programming with Microsoft Visual Basic 2005, Third Edition

Opening an Existing Solution

• Options to open an existing solution– Go to File Open Solution for dialog box– Select the appropriate solution file – Double-click solution file on hard drive (.sln extension)

• Only one solution can be open at any one time– Currently open solution closes before another opens

Page 23: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

23Programming with Microsoft Visual Basic 2005, Third Edition

Exiting Visual Studio 2005 or Visual Basic 2005 Express Edition

• Options to exit an application– Use Close button on application window’s title bar– Select File on the menu bar, then Exit

Page 24: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

24Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson A

• To create a solution, select Tools on the menu bar, then Options, and then Projects and Solution

• Form’s Text property displays a caption in the title bar

• Property values are set in the Properties window of a selected object

• To save a solution, select File on the menu bar, then Save All

• To open an existing solution, select File on the menu bar, then Open, then Project/Solution

Page 25: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

25Programming with Microsoft Visual Basic 2005, Third Edition

Working with ControlsLesson B Objectives

• Add a control to a form

• Set the properties of a label, picture box, and button control

• Select multiple controls

• Center controls on the form

• Open the Project Designer window

Page 26: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

26Programming with Microsoft Visual Basic 2005, Third Edition

Working with ControlsLesson B Objectives (continued)

• Start and end an application

• Enter code in the Code Editor window

• Terminate an application using the Me.Close method

Page 27: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

27Programming with Microsoft Visual Basic 2005, Third Edition

The Toolbox Window

• Class: pattern from which an object is created

• Object: instance with behaviors and attributes

• Toolbox window (or toolbox)

– Contains tools for designing a user interface

– Each tool represents a class

– Controls are instantiated using tools from toolbox

• Controls: GUI objects that appear on a form

Page 28: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

28Programming with Microsoft Visual Basic 2005, Third Edition

The Toolbox Window (continued)

Figure 1-17: Box describing the purpose of the ListBox tool

Page 29: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

29Programming with Microsoft Visual Basic 2005, Third Edition

Using the Label Tool

• Label tool: used to create a label control

• Label control

– Displays text that cannot be edited at runtime

• Text property– Determines value that appears in the label control

• Location property– Determines location of the label control on the form– Includes the X property and the Y property

Page 30: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

30Programming with Microsoft Visual Basic 2005, Third Edition

Using the Label Tool (continued)

Figure 1-19: Label control added to the form

Page 31: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

31Programming with Microsoft Visual Basic 2005, Third Edition

Changing the Property for More Than One Control at a Time

• How to simultaneously modify similar controls

– Click one control, then press and hold the Ctrl key

– Click the other similar control(s) in the form

– Set the property value in the Properties window

• Font property: determines font type, style, size

• Font: the general shape of characters in text

• Point: a measure of font size (1/72 of an inch)

Page 32: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

32Programming with Microsoft Visual Basic 2005, Third Edition

Changing the Property for More Than One Control at a Time

(continued)

Figure 1-20: Label controls selected on the form

Page 33: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

33Programming with Microsoft Visual Basic 2005, Third Edition

Using the Format Menu

• Format menu: options to manipulate controls

– Align: aligns by left, right, top, or bottom borders

– Make Same Size: makes width and/or height uniform

– Center in Form option: centers one or more controls

Page 34: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

34Programming with Microsoft Visual Basic 2005, Third Edition

Using the PictureBox Tool

• PictureBox tool: creates a picture box control

• Picture box control: displays an image on a form

• Task box: lists tasks associated with a picture box

• Use the task box to import an image into a project

• Project’s resource file contains imported images

Page 35: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

35Programming with Microsoft Visual Basic 2005, Third Edition

Using the PictureBox Tool (continued)

Figure 1-23: Task list for a picture box control

Page 36: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

36Programming with Microsoft Visual Basic 2005, Third Edition

Using the PictureBox Tool (continued)

Figure 1-24: Completed Select Resource dialog box

Page 37: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

37Programming with Microsoft Visual Basic 2005, Third Edition

Using the Button Tool

• Common means of exiting a Windows application

– Exit option on a File menu

– Exit button

• Button tool: used to create a button control

• Button control: reacts to a click event

Page 38: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

38Programming with Microsoft Visual Basic 2005, Third Edition

Using the Button Tool (continued)

Figure 1-26: Button control added to the form

Page 39: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

39Programming with Microsoft Visual Basic 2005, Third Edition

Starting and Ending an Application

• Startup form: displays when application is started

• Project Designer: specifies startup form/executable

• Executable file: can be run outside of the IDE

• Two ways to start an application in the IDE

– Select Debug on menu bar, then Start Debugging

– Press the F5 key on the keyboard

• Application started in IDE runs in debug mode

Page 40: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

40Programming with Microsoft Visual Basic 2005, Third Edition

Starting and Ending an Application (continued)

Figure 1-28: Completed Copyright Project Designer window

Page 41: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

41Programming with Microsoft Visual Basic 2005, Third Edition

Starting and Ending an Application (continued)

Figure 1-29: Result of starting the copyright screen application

Page 42: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

42Programming with Microsoft Visual Basic 2005, Third Edition

Writing Visual Basic 2005 Code

• Event: a user action such as double-clicking

• Event procedure: instructions handling an event

• Code Editor window– Provides templates for creating event procedures

– Procedure header: first line in the code template

– Procedure footer: last line in the code template

• Class Name list box: lists the names of objects

• Method Name list box: lists appropriate events

Page 43: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

43Programming with Microsoft Visual Basic 2005, Third Edition

Writing Visual Basic 2005 Code (continued)

Figure 1-32: xExitButton’s Click event procedure shown in the Code Editor window

Page 44: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

44Programming with Microsoft Visual Basic 2005, Third Edition

Writing Visual Basic 2005 Code (continued)

• Sub procedure: code block performing a task

• Keyword: predefined word in a language

– Sub: starts a sub procedure

– Private: restricts procedure to class of definition

– End Sub: indicates the end of a sub procedure

• Parameters: data passed to a called procedure

Page 45: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

45Programming with Microsoft Visual Basic 2005, Third Edition

Writing Visual Basic 2005 Code (continued)

• Method

– A predefined Visual Basic procedure

– Called (or invoked) when needed

• Me.Close method: causes current form to close

• IntelliSense: provides appropriate selections • Sequential processing: serial instruction execution • Sequence structure: set of sequential instructions

Page 46: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

46Programming with Microsoft Visual Basic 2005, Third Edition

Writing Visual Basic 2005 Code (continued)

Figure 1-33: The IntelliSense feature displays a list of choices

Page 47: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

47Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson B

• To add a control to a form, use a tool from the toolbox

• Some controls used: Label, PictureBox, Button

• Project Designer window: used to change the names of the startup form and/or executable file

• To start and stop an application in the IDE, use the Debug menu choice

• Code Editor window: used to display, write, and edit procedures

Page 48: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

48Programming with Microsoft Visual Basic 2005, Third Edition

Completing the Copyright ScreenLesson C Objectives

• Set the properties of a timer control

• Delete a control from the form

• Delete code from the Code Editor window

• Code the timer control’s Tick event procedure

• Remove and/or disable the form’s Minimize, Maximize, and Close buttons

Page 49: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

49Programming with Microsoft Visual Basic 2005, Third Edition

Completing the Copyright ScreenLesson C Objectives (continued)

• Prevent the user from sizing a form

• Print the project’s code

Page 50: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

50Programming with Microsoft Visual Basic 2005, Third Edition

Using the Timer Tool

• Splash screens typically close automatically

– Timer control removes the splash screen

• Timer tool: used to create a timer control

• Timer control

– Used to process code at regular time intervals

– Does not appear on the user interface at runtime

• Interval property: specified in milliseconds

Page 51: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

51Programming with Microsoft Visual Basic 2005, Third Edition

Using the Timer Tool (continued)

• Enabled property: allows timer to run if set to True

• Tick event– Occurs each time an interval has elapsed – Condition: the timer must be running (enabled)

• Add code into the Tick event procedure

• Component tray– Stores controls not visible in user interface at runtime

Page 52: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

52Programming with Microsoft Visual Basic 2005, Third Edition

Using the Timer Tool (continued)

Figure 1-40: Code entered in the xExitTimer’s Click event procedure

Page 53: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

53Programming with Microsoft Visual Basic 2005, Third Edition

Setting the FormBorderStyle Property

• FormBorderStyle property– Determines the border style of a form

• Some settings and their descriptions– Fixed3D: fixed, three-dimensional– FixedDialog: fixed, thick dialog style– FixedSingle: fixed, thin line– None: no border

Page 54: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

54Programming with Microsoft Visual Basic 2005, Third Edition

The MinimizeBox, MaximizeBox, and ControlBox Properties

• MinimizeBox property: determines whether the Minimize button appears on a form’s title bar

• MaximizeBox property: determines whether the Maximize button appears on a form’s title bar

• ControlBox property: determines whether title bar elements (icons/buttons) appear on a form

• Splash screens typically do not contain a title bar

• To remove title bar, delete content of form’s Text property

Page 55: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

55Programming with Microsoft Visual Basic 2005, Third Edition

The MinimizeBox, MaximizeBox, and ControlBox Properties

(continued)

Figure 1-42: Completed copyright screen

Page 56: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

56Programming with Microsoft Visual Basic 2005, Third Edition

Printing Your Code

• Print copy of code entered in the Code Editor window– Helps you understand and maintain the application

• Requirement for printing the code– Code editor window must be active, or current, window

• Print options– Hide collapsed regions– Include line numbers

Page 57: Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition

57Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson C

• Timer control: processes code at specified intervals

• Use the form’s MinimizeBox and MaximizeBox properties to enable or disable the Minimize and Maximize buttons on the form

• Form’s ControlBox property: determines whether icon and control buttons will appear in the title bar

• Form’s BorderStyle property: determines if the form will be sizable at runtime

• Print Visual Basic code with Code Editor open