Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter One An Introduction to Visual Basic 2008
Previewing the Splash Screen Figure 1 -1: Splash screen for the Country Charm Inn Programming with Microsoft Visual Basic 2008, Fourth Edition 2
Lesson A Objectives After studying Lesson A, you should be able to: • Start and customize Visual Studio 2008 or Visual Basic 2008 Express Edition • Create a Visual Basic 2008 Windows application • Manage the windows in the IDE • Set the properties of an object • Restore a property to its default setting Programming with Microsoft Visual Basic 2008, Fourth Edition 3
Lesson A Objectives (continued) • Save a solution • Close a solution • Open an existing solution Programming with Microsoft Visual Basic 2008, Fourth Edition 4
The Splash Screen Application • In this chapter, you create a splash screen using Visual Basic 2008 • Visual Basic 2008 is available as: – A stand-alone product • Visual Basic 2008 Express Edition – Part of Visual Studio 2008 Programming with Microsoft Visual Basic 2008, Fourth Edition 5
The Splash Screen Application (continued) • To start Visual Studio 2008 or Visual Basic 2008 Express Edition: – Click Start and point to All Programs – Select one of two environments: • Microsoft Visual Studio 2008 • Microsoft Visual Basic 2008 Express Edition – – Click Window on the menu bar Click Reset Window Layout Click Yes at prompt to restore default layout Click the Maximize button if necessary Programming with Microsoft Visual Basic 2008, Fourth Edition 6
The Splash Screen Application (continued) • User interface: The part of program that user sees and interacts with • Windows applications are composed of solutions, projects, and files • Solution: Container that stores project and files for an entire application • Project: Container that stores files associated with the project • Template: Pattern used to create solutions and projects Programming with Microsoft Visual Basic 2008, Fourth Edition 7
The Splash Screen Application (continued) • Creating a Windows application – First, set options in Visual Studio: • Click Tools on menu bar and then click Options • Expand Projects and Solutions, click General, and then check these options: – Always show Error List if build finishes with errors – Always show solution – Save new projects when created • Uncheck Show Output window when build starts • Click OK to close Options dialog Programming with Microsoft Visual Basic 2008, Fourth Edition 8
The Splash Screen Application (continued) Figure 1 -3: Options dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition 9
The Splash Screen Application (continued) • Creating a Windows application (continued) – Click File menu, and then click New Project – Expand Visual Basic node, and then click Windows – Click Windows Form Application in the installed templates section of the Templates list – Change the project name in the Name box – Click Browse button to select the folder where solution will be stored – Click OK Programming with Microsoft Visual Basic 2008, Fourth Edition 10
The Splash Screen Application (continued) Figure 1 -4: Completed New Project dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition 11
The Splash Screen Application (continued) Figure 1 -5: Solution and Visual Basic project Programming with Microsoft Visual Basic 2008, Fourth Edition 12
Managing the Windows in the IDE • IDE: Integrated Development Environment • Options to show a window – Click View on menu bar, select a window – Hover mouse over tab (if auto-hidden) • Options to close a window – Use Close button on title bar • To minimize window to tab: – Click Auto Hide (push-pin) toggle button on title bar Programming with Microsoft Visual Basic 2008, Fourth Edition 13
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 the form – Examples of interaction objects: buttons, text boxes Programming with Microsoft Visual Basic 2008, Fourth Edition 14
The Windows Form Designer Window (continued) Figure 1 -7: Windows Form Designer window Programming with Microsoft Visual Basic 2008, Fourth Edition 15
The Solution Explorer Window • Solution Explorer window – Displays list of projects contained in current solution – Displays the items contained in each project • Source file: File containing program instructions • Code: Program instructions • Form file – Contains code underlying a Windows Form object – Has a file extension of. vb Programming with Microsoft Visual Basic 2008, Fourth Edition 16
The Solution Explorer Window (continued) Figure 1 -8: Solution Explorer window Programming with Microsoft Visual Basic 2008, Fourth Edition 17
The Properties Window • Properties: Attributes of object that determine its appearance and behavior • Properties window: Lists object’s properties • Object box: Contains name of selected object • Properties list: Helps identify and access properties • Settings box: Shows current values of properties Programming with Microsoft Visual Basic 2008, Fourth Edition 18
The Properties Window (continued) Figure 1 -9: Properties window showing the properties of the Form 1. vb file object Programming with Microsoft Visual Basic 2008, Fourth Edition 19
The Properties Window (continued) Figure 1 -10: Form file’s name shown in the designer, Solution Explorer, and Properties windows Programming with Microsoft Visual Basic 2008, Fourth Edition 20
Properties of a Windows Form • To display properties of Windows Form object: – Select the Form object in designer window • Two ways to organize properties: – Categorized – Alphabetical • Class definition: Specifies attributes and behaviors • Namespace: Code defining group of classes • Dot member access operator: Specifies hierarchy – Example: System. Windows. Form Programming with Microsoft Visual Basic 2008, Fourth Edition 21
Properties of a Windows Form (continued) Figure 1 -11: Properties window showing a partial listing of the form’s properties Programming with Microsoft Visual Basic 2008, Fourth Edition 22
Name Property • Name property: Refers to object in code • Form’s name must begin with letter – Can contain only letters, numbers, and underscore • Hungarian notation: Naming convention – – First three characters represent type of object Remaining characters represent object’s purpose Name is entered in camel case (for camel’s hump) Example: frm. Calculate. Taxes Programming with Microsoft Visual Basic 2008, Fourth Edition 23
The Text Property • Application name should appear in window’s title bar – The title bar should display an appropriate name • Text property – Controls caption in form’s title bar – Also appears on application’s taskbar button – Default caption is Form 1 Programming with Microsoft Visual Basic 2008, Fourth Edition 24
The Start. Position Property • Determines where form first appears on screen Programming with Microsoft Visual Basic 2008, Fourth Edition 25
Font Property • Determines type, style, and size of the font used on the form Programming with Microsoft Visual Basic 2008, Fourth Edition 26
Size Property • Controls the height and width of the object • Measured in pixels by default • Pixel: Unit picture element of screen image Programming with Microsoft Visual Basic 2008, Fourth Edition 27
Setting and Restoring the Value of a Property • Back. Color property: Sets form background color • Set the value of a property using settings box in Properties window • To restore default value of a property: – Right-click property name and click Reset Programming with Microsoft Visual Basic 2008, Fourth Edition 28
Setting and Restoring the Value of a Property (continued) Figure 1 -12: Status of the form in the IDE Programming with Microsoft Visual Basic 2008, Fourth Edition 29
Saving a Solution • Save solution at regular intervals – For example, every 10 minutes • Options for saving solution include: – Click File on menu bar and then click Save All – Click Save All button on Standard toolbar – Select solution’s name in Solution Explorer window and then click Save button on Standard toolbar Programming with Microsoft Visual Basic 2008, Fourth Edition 30
Saving a Solution (continued) • To determine which file will be saved: – Any tab on designer tab with asterisk indicates unsaved file – Hover mouse on Save button Programming with Microsoft Visual Basic 2008, Fourth Edition 31
Closing the Current Solution • Click File on menu bar and then click Close Solution • Impact of closing current solution: – All projects and files contained within are closed • Unsaved changes trigger warning prompt – You are given another chance to save your work Programming with Microsoft Visual Basic 2008, Fourth Edition 32
Opening an Existing Solution • Options to open an existing solution – Click File on menu bar and then click Open Solution – Select appropriate solution file – Or, 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 Programming with Microsoft Visual Basic 2008, Fourth Edition 33
Exiting Visual Studio 2008 or Visual Basic 2008 Express Edition • Options to exit from Visual Studio: – Use Close button on application window’s title bar – Click File on the menu bar and then click Exit Programming with Microsoft Visual Basic 2008, Fourth Edition 34
Lesson A Summary • Start Visual Studio from Start button • To create solution, click File on menu bar and then click New Project • Property values are set in Properties window of selected object • Form’s Text property displays caption in title bar • To save solution, click File on menu bar and then click Save All • To open existing solution, click File on the menu bar, click Open, and then click Project/Solution Programming with Microsoft Visual Basic 2008, Fourth Edition 35
Lesson B Objectives After studying Lesson B, you should be able to: • 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 Programming with Microsoft Visual Basic 2008, Fourth Edition 36
Lesson B Objectives (continued) • Start and end an application • Enter code in the Code Editor window • Terminate an application using the Me. Close() instruction • Run the project’s executable file Programming with Microsoft Visual Basic 2008, Fourth Edition 37
The Toolbox Window • Class: Pattern from which object is created • Object: Instance with behaviors and attributes • Toolbox window (or toolbox) – Contains tools for designing user interface – Each tool represents class – Controls are instantiated using tools from toolbox • Controls: GUI objects that appear on a form Programming with Microsoft Visual Basic 2008, Fourth Edition 38
The Toolbox Window (continued) Figure 1 -13: Box describing the purpose of the List. Box tool Programming with Microsoft Visual Basic 2008, Fourth Edition 39
The Label Tool • Label tool: Used to create label control • Label control: Displays text that cannot be edited at runtime • Name property: Unique name for control • Text property – Specifies value that appears in label control • Location property – Specifies location of control on form – Includes X property and Y property Programming with Microsoft Visual Basic 2008, Fourth Edition 40
The Label Tool (continued) Figure 1 -15: Label control added to the form Programming with Microsoft Visual Basic 2008, Fourth Edition 41
Changing the Property For More Than One Control At a Time • To simultaneously modify similar controls: – Click one control and then press and hold Ctrl key – Click other similar control(s) in form – Set property value in Properties window • Some properties are common to many controls, including: – Font property: Determines font type, style, size of the font Programming with Microsoft Visual Basic 2008, Fourth Edition 42
Changing the Property For More Than One Control At a Time (continued) Figure 1 -16: Label controls selected on the form Programming with Microsoft Visual Basic 2008, Fourth Edition 43
Using the Format Menu • Format menu: Options to manipulate multiple selected controls – Align: Aligns selected controls by left, right, top, or bottom borders – Make Same Size: Makes width and/or height of selected controls uniform – Center in Form: Centers one or more controls • Select as first control the one whose size or location you want to match – Called reference control Programming with Microsoft Visual Basic 2008, Fourth Edition 44
The Picture. Box Tool • • • Picture. Box tool: Creates picture box control Picture box control: Displays image on form Task box: Lists tasks associated with picture box Use task box to import image into project Project’s resource file contains imported images Size. Mode property: Specifies if Picture. Box or image itself should be resized to match other Programming with Microsoft Visual Basic 2008, Fourth Edition 45
The Picture. Box Tool (continued) Figure 1 -17: Task list for a picture box control Programming with Microsoft Visual Basic 2008, Fourth Edition 46
The Picture. Box Tool (continued) Figure 1 -18: Completed Select Resource dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition 47
The Picture. Box Tool (continued) Figure 1 -19: Image shown in the picture box control Programming with Microsoft Visual Basic 2008, Fourth Edition 48
The Button Tool • Button tool: Used to create button control • Button control: Used to perform action when clicked • Common means of exiting a Windows application – Exit option on File menu – Exit button Programming with Microsoft Visual Basic 2008, Fourth Edition 49
Starting and Ending an Application • Startup form: Displays when application is first started • Project Designer: Specifies startup form or executable code to be run – Right-click My Project in Solution Explorer and then click Open – Or, click Project on menu and then click <project name> Properties • Application tab: Specify startup form Programming with Microsoft Visual Basic 2008, Fourth Edition 50
Starting and Ending an Application (continued) Figure 1 -20: Application pane in the Project Designer window Programming with Microsoft Visual Basic 2008, Fourth Edition 51
Starting and Ending an Application (continued) • Executable file: File that can be run outside of the IDE – File name ends with. exe • Two ways to start application in IDE: – Click Debug on menu bar and then click Start Debugging – Press F 5 key • Application started from IDE runs in debug mode Programming with Microsoft Visual Basic 2008, Fourth Edition 52
Starting and Ending an Application (continued) Figure 1 -21: Result of starting the copyright screen application Programming with Microsoft Visual Basic 2008, Fourth Edition 53
Writing Visual Basic 2008 Code • Event: User action such as double-clicking • Event procedure: Set of program instructions that are processed when event occurs • Code Editor window – Provides templates for creating event procedures – Procedure header: First line in code template – Procedure footer: Last line in code template • To open Code Editor window: – Right-click form and then click View Code Programming with Microsoft Visual Basic 2008, Fourth Edition 54
Writing Visual Basic 2008 Code (continued) Figure 1 -22: Code Editor window opened in the IDE Programming with Microsoft Visual Basic 2008, Fourth Edition 55
Writing Visual Basic 2008 Code (continued) • You can collapse or expand a region of code in the Code Editor window – Click minus box to collapse code – Click plus box to expand code Figure 1 -23: Code collapsed in the Code Editor window Programming with Microsoft Visual Basic 2008, Fourth Edition 56
Writing Visual Basic 2008 Code (continued) • Class Name list box: Lists names of objects (controls) on form • Method Name list box: Lists appropriate events for selected object • To select a control’s event in code editor window: – Select class name first and then select method name • Syntax: Rules of programming language Programming with Microsoft Visual Basic 2008, Fourth Edition 57
Writing Visual Basic 2008 Code (continued) • Procedure header: First line in code template for event procedure • Procedure footer: Last line in code template for event procedure • Keyword: Predefined word in programming language that has special meaning – Are color-coded in IDE Programming with Microsoft Visual Basic 2008, Fourth Edition 58
Writing Visual Basic 2008 Code (continued) • Sub procedure: a block of code that performs a specific task • Keywords in a sub procedure block: – Sub: starts a sub procedure – Private: restricts the use of the procedure to the form where it appears – End Sub: indicates the end of a sub procedure • Event sub procedure names include the object name and the event name – Example: btn. Exit_Click Programming with Microsoft Visual Basic 2008, Fourth Edition 59
The Me. Close() Instruction • Method: Predefined Visual Basic procedure – Can be called (or invoked) when needed • Me. Close() method: Causes current form to close – If it is only form in application, the application terminates • Intelli. Sense feature: Provides appropriate selections as pop-up lists – Type first few letters of choice – Then use arrow keys to select an item in list Programming with Microsoft Visual Basic 2008, Fourth Edition 60
The Me. Close() Instruction (continued) Figure 1 -25: List displayed by the Intelli. Sense feature Programming with Microsoft Visual Basic 2008, Fourth Edition 61
The Me. Close() Instruction (continued) Figure 1 -26: Completed Click event procedure for the btn. Exit control Programming with Microsoft Visual Basic 2008, Fourth Edition 62
Lesson B Summary • • • To add a control to a form, use tool from toolbox Use Label control for text that user cannot edit Use Picture. Box control to display image Use Button control to cause action to occur Project Designer window: Used to change name of startup form and/or executable file • To start and stop an application in IDE, use the Debug menu choice Programming with Microsoft Visual Basic 2008, Fourth Edition 63
Lesson B Summary (continued) • Code Editor window: Used to display, write, and edit procedures • Me. Close() method: Closes the current form at runtime • To run an executable file from IDE, use Debug menu • To run an executable file outside IDE, browse to. exe file and double-click it Programming with Microsoft Visual Basic 2008, Fourth Edition 64
Lesson C Objectives After studying Lesson C, you should be able to: • 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 • Prevent the user from sizing a form • Print the project’s code Programming with Microsoft Visual Basic 2008, Fourth Edition 65
Using the Timer Tool • Splash screens typically close automatically – Do not usually contain Exit button – Timer control removes 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 • Enabled property: Allows timer to run if set to True Programming with Microsoft Visual Basic 2008, Fourth Edition 66
Using the Timer Tool (continued) • Tick event: – Occurs each time an interval has elapsed – Condition: Timer must be running (enabled) • Add code into Tick event procedure – Code will be run when Tick event occurs • Component tray – Special area below form design area in IDE – Stores controls not visible in user interface at runtime Programming with Microsoft Visual Basic 2008, Fourth Edition 67
Using the Timer Tool (continued) Figure 1 -29: Timer control placed in the component tray Programming with Microsoft Visual Basic 2008, Fourth Edition 68
Setting the Form. Border. Style Property • Form. Border. Style property – Determines the border style of a form • Some settings and their descriptions – Sizable: Allows user to change form’s size by dragging its borders at runtime – Fixed. Single: Fixed, thin border line – None: No border Programming with Microsoft Visual Basic 2008, Fourth Edition 69
The Minimize. Box, Maximize. Box, and Control. Box Properties • Minimize. Box property: Determines whether Minimize button appears on form’s title bar • Maximize. Box property: Determines whether Maximize button appears on form’s title bar • Control. Box property: Determines whether title bar elements (icons/buttons) appear on form • Splash screens typically do not contain title bar – To remove title bar, delete contents of form’s Text property Programming with Microsoft Visual Basic 2008, Fourth Edition 70
The Minimize. Box, Maximize. Box, and Control. Box Properties (continued) Figure 1 -31: Completed splash screen Programming with Microsoft Visual Basic 2008, Fourth Edition 71
Printing Your Code • You can print a copy of code entered in Code Editor window – Helps you understand maintain application • To print code: – Code editor window must be active (current) window • Print options: – Hide collapsed regions – Include line numbers Programming with Microsoft Visual Basic 2008, Fourth Edition 72
Printing Your Code (continued) Figure 1 -32: Print dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition 73
Lesson C Summary • Timer control: Processes code at specified intervals • Use form’s Minimize. Box and Maximize. Box properties to enable or disable Minimize and Maximize buttons on form • Form’s Control. Box property: Determines whether icon and control buttons will appear in title bar • Form’s Border. Style property: Determines if form will be sizable at runtime • Print Visual Basic code with Code Editor window open Programming with Microsoft Visual Basic 2008, Fourth Edition 74
- Slides: 74