Download presentation
Presentation is loading. Please wait.
Published byDortha Blair Modified over 6 years ago
1
Programming with Microsoft Visual Basic 2008 4th Edition
Chapter One An Introduction to Visual Basic 2008 Programming with Microsoft Visual Basic th Edition
2
Previewing the Splash Screen
Figure 1-1: Splash screen for the Country Charm Inn Programming with Microsoft Visual Basic 2008, Fourth Edition
3
Lesson A Objectives After studying Lesson A, you should be able to:
Start and customize Visual Studio 2008 (VS2008) or Visual Basic 2008 (VB2008) 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 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 VB 2008 VB 2008 is available as: A stand-alone product Visual Basic 2008 Express Edition Part of VS 2008 Installation file of Visual Studio 2008 (詢問 鄧福祥技師) Programming with Microsoft Visual Basic 2008, Fourth Edition
5
The Splash Screen Application (continued)
To start VS 2008 or VB 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 (也許須選擇預設的環境設定 : Visual Basic開發設定) 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)
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)
First, set Environment Options in Visual Studio: Click Tools (工具) on menu bar and then click Options (選項) to open Options dialog box 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)
expand click 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
Figure 1-4: Completed New Project dialog box
expand click Create VB2008\Chap01 Change the project name in the Name box Change the location in the Location box Figure 1-4: Completed New Project dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition
11
The Splash Screen Application (continued)
Default form Figure 1-5: Solution and Visual Basic project Programming with Microsoft Visual Basic 2008, Fourth Edition 11
12
Folder Structure Created in File Explorer
.exe執行檔 .sln方案檔 .vb表單檔 Debug folder Programming with Microsoft Visual Basic 2008, Fourth Edition
13
Managing the Windows in the IDE
IDE: Integrated Development Environment Options to show various sub-windows Click View on menu bar to select a window 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 To show an auto-hidden window Hover mouse over tab of the window Programming with Microsoft Visual Basic 2008, Fourth Edition
14
3 1 2 4 Options to show various sub-windows
Click View on menu bar, select a window Hover mouse over tab (if auto-hidden) Click Auto Hide (push-pin) toggle button Hover mouse over tab (if overlapped) 4 Programming with Microsoft Visual Basic 2008, Fourth Edition 14 14
15
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
16
Windows Form Designer Window
Design our GUI here Figure 1-7: Windows Form Designer window Programming with Microsoft Visual Basic 2008, Fourth Edition
17
Windows Form Designer Window
Click to close window Double-click to open window Figure 1-7: Windows Form Designer window Programming with Microsoft Visual Basic 2008, Fourth Edition
18
The 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 in source file Form file Contains code underlying a Windows Form object Has a file extension of .vb Programming with Microsoft Visual Basic 2008, Fourth Edition
19
The Solution Explorer Window
Figure 1-8: Solution Explorer window Programming with Microsoft Visual Basic 2008, Fourth Edition 19 19
20
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
21
The Properties Window Selected object
Figure 1-9: Properties window showing the properties of the Form1.vb file object Programming with Microsoft Visual Basic 2008, Fourth Edition
22
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
23
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.Forms.Form Programming with Microsoft Visual Basic 2008, Fourth Edition
24
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
25
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: frmCalculateTaxes Programming with Microsoft Visual Basic 2008, Fourth Edition
26
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 Form1 Programming with Microsoft Visual Basic 2008, Fourth Edition
27
The Text Property Change it to see what happens
Programming with Microsoft Visual Basic 2008, Fourth Edition 27 27
28
The StartPosition Property
Determines where form first appears on screen Programming with Microsoft Visual Basic 2008, Fourth Edition
29
Font Property Determines type, style, and size of the font used on the form Programming with Microsoft Visual Basic 2008, Fourth Edition
30
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
31
Setting and Restoring the Value of a Property
BackColor 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 31 31
32
Setting and Restoring the Value of a Property (continued)
Right-click and click Reset Figure 1-12: Status of the form in the IDE Programming with Microsoft Visual Basic 2008, Fourth Edition 32
33
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 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
34
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
35
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
36
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
37
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
38
Lesson B Objectives After studying Lesson B, you should be able to:
Add a (GUI) 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
39
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
40
The Toolbox (工具箱) Window
Class: a template from which an object is created Object: Instance with behaviors and attributes Controls: VB GUI objects that appear on a form Toolbox window (or toolbox) Contains tools for designing user interface Each tool represents class Controls are instantiated using tools from toolbox Programming with Microsoft Visual Basic 2008, Fourth Edition
41
The Toolbox Window (continued)
Rest your mouse to show the tooltip (Depends on version) Figure 1-13: Box describing the purpose of the ListBox tool Programming with Microsoft Visual Basic 2008, Fourth Edition
42
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
43
The Label Tool (continued)
Figure 1-15: Label control added to the form Programming with Microsoft Visual Basic 2008, Fourth Edition
44
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
45
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
46
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
47
The PictureBox Tool PictureBox 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 SizeMode property: Specifies if PictureBox or image itself should be resized to match other Programming with Microsoft Visual Basic 2008, Fourth Edition
48
The PictureBox Tool (continued)
After a image file is imported, change it to see what happens Figure 1-17: Task list for a picture box control Programming with Microsoft Visual Basic 2008, Fourth Edition
49
The PictureBox Tool (continued)
Click to import a image file Figure 1-18: Completed Select Resource dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition
50
The PictureBox Tool (continued)
Figure 1-19: Image shown in the picture box control Programming with Microsoft Visual Basic 2008, Fourth Edition
51
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
52
Starting and Ending an Application
Startup form: Displays when application is first started Project Designer: Specifies startup form or executable code to be run. Two ways : Right-click My Project in Solution Explorer and then click Open Or, click Project on menu and then click <project name> Properties Click Application tab, then specify startup form Programming with Microsoft Visual Basic 2008, Fourth Edition
53
Starting and Ending an Application (Project Designer window)
Figure 1-20: Application pane in the Project Designer window Programming with Microsoft Visual Basic 2008, Fourth Edition
54
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 F5 key Application started from IDE runs in debug mode Programming with Microsoft Visual Basic 2008, Fourth Edition
55
Starting and Ending an Application (continued)
Figure 1-21: Result of starting the copyright screen application Programming with Microsoft Visual Basic 2008, Fourth Edition
56
Writing Visual Basic 2008 Code (Code Editor window )
Event: User action such as double-clicking Event procedure: A set of program instructions that are processed when an 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
57
Writing Visual Basic 2008 Code (Code Editor window )
Figure 1-22: Code Editor window opened in the IDE Programming with Microsoft Visual Basic 2008, Fourth Edition
58
Writing Visual Basic 2008 Code (Code Editor window )
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
59
Writing Visual Basic 2008 Code (Components of Code Editor window )
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
60
Writing Visual Basic 2008 Code (Procedure)
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
61
Writing Visual Basic 2008 Code (Sub procedure)
Sub procedure: a block of code that performs a specific task Keywords in a sub procedure block: Sub: indicates the start of 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 Names of Event sub procedures include the object name and the event name Example: btnExit_Click Programming with Microsoft Visual Basic 2008, Fourth Edition
62
The Me.Close() Instruction
Method: Predefined VB procedure Can be called (or invoked) if needed Me.Close() method Causes current form to close If it is the only form in the application, the application terminates IntelliSense 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
63
The Me.Close() Instruction (with IntelliSense feature)
procedure header procedure footer IntelliSense pop-up list with a leading m Figure 1-25: List displayed by the IntelliSense feature Programming with Microsoft Visual Basic 2008, Fourth Edition
64
The Me.Close() Instruction (continued)
Check object and event names for the code you are writing procedure header from template Figure 1-26: Completed Click event procedure for the btnExit control procedure footer From template Programming with Microsoft Visual Basic 2008, Fourth Edition
65
Lesson B Summary To add a control to a form, use tool from toolbox
Use Label control for text that user cannot edit Use PictureBox 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
66
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
67
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
68
Using the Timer Tool Splash screens typically close automatically
Do not usually contain Exit button Use Timer control to remove 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
69
Using the Timer Tool (continued)
Tick event: Occurs each time an interval has elapsed (The interval is set by Interval property) 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
70
Using the Timer Tool (component tray)
Figure 1-29: Timer control placed in the component tray Programming with Microsoft Visual Basic 2008, Fourth Edition
71
Setting the FormBorderStyle Property
FormBorderStyle property of a form 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 FixedSingle: Fixed, thin border line None: No border Programming with Microsoft Visual Basic 2008, Fourth Edition
72
The MinimizeBox, MaximizeBox, and ControlBox Properties
MinimizeBox property: Determines whether Minimize button appears on form’s title bar MaximizeBox property: Determines whether Maximize button appears on form’s title bar ControlBox 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
73
The MinimizeBox, MaximizeBox, and ControlBox Properties (continued)
Figure 1-31: Completed splash screen Programming with Microsoft Visual Basic 2008, Fourth Edition
74
Printing Your Code You can print a copy of code entered in Code Editor window Helps you understand and 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
75
Printing Your Code (continued)
Figure 1-32: Print dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition
76
Lesson C Summary Timer control: Processes code at specified intervals
Use form’s MinimizeBox and MaximizeBox properties to enable or disable Minimize and Maximize buttons on form Form’s ControlBox property: Determines whether icon and control buttons will appear in title bar Form’s BorderStyle 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.