51
Chapter 1: An Introduction to Visual Basic .NET Programming with Microsoft Visual Basic .NET, Second Edition

Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

  • View
    274

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Chapter 1: An Introduction to Visual Basic .NET

Programming with Microsoft Visual Basic .NET, Second Edition

Page 2: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 2

Creating a Windows-Based Application in Visual Basic .NET

Lesson A Objectives• Start and customize Visual Studio.NET

• Create a Visual Studio .NET solution

• Add a Visual Basic .NET project to a solution

• Set the properties of an object

• Restore a property to its default setting

Page 3: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 3

Creating a Windows-Based Application in Visual Basic .NET Lesson A Objectives (continued)

• Save a solution, project, and form

• Close a solution

• Open an existing solution

Page 4: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 4

Starting and Customizing Visual Studio .NET

• Visual Studio .NET is Microsoft’s newest integrated development environment (IDE)– Includes programming languages such as Visual

Basic .NET, C++ .NET, and C# .NET• A Windows-based application has a Windows

user interface and runs on a desktop computer• A user interface is what you see and interact with

when using an application• A Web-based application has a Web user

interface and runs on a server

Page 5: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 5

Starting and Customizing Visual Studio .NET (continued)

• Windows in the Visual Studio .NET IDE:– Start Page– Solution Explorer– Server Explorer– Toolbox– Class View– Dynamic Help

Page 6: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 6

Starting and Customizing Visual Studio .NET (continued)

Figure 1-3: Projects pane in the Start Page window

Page 7: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 7

Creating the Copyright Screen Application

• Applications created in Visual Studio .NET are composed of solutions, projects, and files

• A solution is a container that stores the projects and files for an entire application

• A project is a container that stores files associated with only a specific piece of the solution

• To create an application:– Create a blank Visual Studio .NET solution– Add one or more projects to the solution

Page 8: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 8

Creating the Copyright Screen Application (continued)

Figure 1-6: New Project dialog box used to create a blank solution

Page 9: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 9

Creating the Copyright Screen Application (continued)

Figure 1-9: New Visual Basic .NET project added to the solution

Page 10: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 10

The Windows Form Designer Window

• Windows Form Designer window is where you create (or design) the graphical user interface (GUI) for your project

• You create the user interface by adding other objects (such as buttons and text boxes) to a Windows Form object (or form)

Page 11: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 11

The Windows Form Designer Window (continued)

Figure 1-11: Windows Form Designer window

Page 12: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 12

The Solution Explorer Window

• The Solution Explorer window displays:– A list of projects contained in the current solution– Items contained in each project

• The Solution Explorer window shows that the Copyright Solution contains one project named Copyright Project

• Within the Copyright Project is:– A References folder– Two files: AssemblyInfo.vb and Form1.vb

Page 13: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 13

The Solution Explorer Window (continued)

Figure 1-12: Solution Explorer window

Page 14: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 14

The Properties Window

• Each object has a set of attributes, called properties, that determine its appearance and behavior

• The Properties window exposes the object’s properties to the programmer

• The Properties window includes an Object box and a Properties list

• The Properties window can be used to change a property of an object

Page 15: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 15

The Properties Window (continued)

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

Page 16: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 16

Properties of the Windows Form Object

• The Windows Form object has a set of properties

• Properties of the Windows Form object will appear in the Properties window when you select the Windows Form object in the designer window

Page 17: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 17

Properties of the Windows Form Object (continued)

• Important properties of the Windows Form object:– Name property– Text property– StartPosition property– Size property– BackgroundImage property

Page 18: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 18

Saving a Solution

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

window, and then click the Save button on the Standard toolbar

Page 19: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 19

Closing the Current Solution

• You close a solution using the Close Solution option on the File menu

• When you close a solution, all projects and files contained in the solution also are closed

• If unsaved changes were made to the solution, project, or form, a dialog box opens and prompts you to save the appropriate files

Page 20: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 20

Opening an Existing Solution

• To open an existing solution:– Click File on the menu bar and then click Open

Solution– Select the appropriate solution file in the Open

Solution dialog box

• Only one solution can be open in the IDE at any one time

Page 21: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 21

Exiting Visual Studio .NET

• You exit an application using either:– The Close button on the application window’s title

bar– The Exit option on the File menu

Page 22: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 22

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

• Set the properties of a project

Page 23: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 23

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 24: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 24

The Toolbox Window

• The Toolbox window, or toolbox, contains the tools and other components you use when designing a user interface

• The contents of the toolbox vary depending on the designer in use

• Tools in the toolbox allow you to create objects, called controls, that can be displayed on a form

Page 25: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 25

The Toolbox Window (continued)

• The Windows Form tab– Contains the tools you use when designing your

user interface– Contains 47 basic tools– You can add new tools or delete existing tools

• By default, the tools on the Windows Form tab are listed in order by their estimated frequency of use, with the most used tools listed first

Page 26: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 26

Using the Label Tool

• You use the Label tool to create a label control

• The purpose of a label control is to display text that the user is not allowed to edit while the application is running

• Assigning meaningful names to the controls on a form will help you keep track of the various controls in the user interface

Page 27: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 27

Using the Label Tool (continued)

• Used to display data that users cannot change

Property Description

Name Identifies the object in code

Text Displays information

AutoSize Determines if the object resizes itself to display all Text

Location Sets the X and Y coordinates for where label is displayed

Font Characteristics of the displayed Text

Page 28: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 28

Changing the Property for More Than One Control at a Time

• You can use the Font property to change the appearance of many of the objects in your user interface

• The Font property allows you to change the type, style, and size of the font used to display the text in the object

• A font is the general shape of the characters in the text

Page 29: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 29

Changing the Property for More Than One Control at a Time

(continued)• Font sizes are typically measured in points, with

one point equaling 1/72 of an inch

• One reason for changing a font is to bring attention to a specific part of the screen

• You can change the font size for both controls at the same time by clicking one control and then pressing and holding down the Control key as you click the other control in the form

Page 30: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 30

Changing the Property for More Than One Control at a Time

(continued)• You can use the Control+Click method to select

as many controls as you want

• To cancel the selection of one of the selected controls, press and hold down the Control key as you click the control

• To cancel the selection of all of the selected controls, release the Control key, then click the form or an unselected control on the form

Page 31: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 31

Using the Format Menu

• Align option aligns two or more controls by their left, right, top, or bottom borders

• Make Same Size option makes two or more controls the same width and/or height

• Center in Form option centers one or more controls either horizontally or vertically on the form

Page 32: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 32

Using the PictureBox Tool

• You can include a logo by displaying the logo in a picture box control, which you create using the PictureBox tool

Page 33: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 33

Using the Button Tool

• Every Windows application should give the user a way to exit the program

– Most Windows applications provide either an Exit option on a File menu or an Exit button for this purpose

• In Windows applications, a button control performs an immediate action when clicked

• You create a button using the Button tool in the toolbox

Page 34: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 34

Starting and Ending an Application

• You can start an application by:

– Clicking Debug on the menu bar, and then clicking Start

– Or, pressing the F5 key on your keyboard

Page 35: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 35

Starting and Ending an Application (continued)

• You can stop the copyright screen application by:– Clicking the Close button on the form’s title bar– Or, clicking the designer window to make it the

active window, then clicking Debug on the menu bar, and then clicking Stop Debugging

– Or, pressing Shift+F5

Page 36: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 36

Writing Visual Basic .NET Code

• Users actions—such as clicking, double-clicking, and scrolling—are called events

• The set of Visual Basic .NET code that tells an object how to respond to an event is called an event procedure

• A class definition is a block of code that specifies (or defines) the attributes and behaviors of an object

Page 37: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 37

Writing Visual Basic .NET Code (continued)

• The Class Name list box lists the names of the objects included in the user interface

• The Method Name list box lists the events to which the selected object is capable of responding

• Code Editor provides you with a code template for every event procedure

• First line in the Code template is the procedure header

Page 38: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 38

Writing Visual Basic .NET Code (continued)

• Last line in the Code template is the procedure footer

• A keyword is a word that has a special meaning in a programming language

• The Sub keyword refers to a block of code that performs a specific task

• The Private keyword indicates that the procedure can be used only within the class in which it is defined

Page 39: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 39

Writing Visual Basic .NET Code (continued)

• Parameters represent information that is passed to the procedure when it is invoked

• A method is a predefined Visual Basic .NET procedure that you can call (or invoke) when needed

• When you call the Me.Close method, Visual Basic .NET terminates the current application

Page 40: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 40

Writing Visual Basic .NET Code (continued)

Private Sub uiExitButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles uiExitButton.Click

Private Sub Indicates the start of a code unit that can only be used in this class

uiExitButton_Click The name of the procedures is the object name and event name connected with an underscore

sender As Object Identifies the object creating the event

e As System.EventArgs

Identifies specific information about the event

Handles Identifies the object and event that this procedure will handle

The uiExitButton’s Click event procedure

Page 41: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 41

Writing Visual Basic .NET Code (continued)

Private Sub uiExitButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles uiExitButton.Click

Me.Close()

End Sub

The event procedure

Page 42: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 42

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

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

• Prevent the user from sizing a form

• Print the project’s code

Page 43: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 43

Using the Timer Tool

• Splash screens typically use a timer control to automatically remove the splash screen after a set period of time

• You create a timer control using the Timer tool in the toolbox

• You can use a timer control to process code at regular time intervals

• You set the timer control’s Interval property to the length of the desired time interval, in milliseconds

Page 44: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 44

Using the Timer Tool (continued)

• You also set its Enabled property to True

• The Enabled property determines whether an object can respond to an event

• You then enter the code you want processed into the control’s Tick event procedure

• The Tick event procedure tells the computer what to do after each time interval has elapsed

Page 45: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 45

Using the Timer Tool (continued)

Figure 1-35: Timer control placed in the component tray

Page 46: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 46

Setting the FormBorderStyle Property

• The FormBorderStyle property determines the border style of a Windows Form object

FromBorderStyle setting

Description

Fixed3D Fixed, three-dimensional

FixedDialog Fixed, thick dialog-style

FixedSingle Fixed, thin line

FixedToolWindow Fixed, tool window style

None No border

Sizable Sizable, normal style (default)

SizableToolWindow Sizable, tool window style

Page 47: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 47

The MinimizeBox, MaximizeBox, and ControlBox Properties

• You can use a Windows Form object’s MinimizeBox property to disable the Minimize button

• You can use the MaximizeBox property to disable the Maximize button

• Splash screens typically do not contain a title bar

• You can remove the title bar by setting the Windows Forms object’s ControlBox property to False, and then removing the text from its Text property

Page 48: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 48

Printing Your Code

• You always should print a copy of the code entered in the Code Editor window, because the printout will help you understand and maintain the application in the future

• To print the code, the Code editor window must be the active, or current, window

Page 49: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 49

Summary

• To create a blank solution, click File on the menu bar, point to New, and then click Blank Solution

• You can add a new project to the current solution by clicking File on the menu bar, pointing to Add Project, and then clicking New Project

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

• To open an existing solution, click File on the menu bar, and then click Open Solution

Page 50: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 50

Summary (continued)

• To add a control to a form, click the appropriate tool in the toolbox, but do not release the mouse button, drag the mouse pointer to the form, and release the mouse button

• To select multiple controls, click one control, then Control+click each of the other controls

• One way to open the Code Editor window is to right-click anywhere on the form (except the form’s title bar), and then click View Code on the context menu

Page 51: Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition

Programming with Microsoft Visual Basic .NET, Second Edition 51

Summary (continued)

• Use the Timer tool to perform code at specified intervals of time

• Set the form’s FormBorderStyle property to control the border style of the form

• To print the Visual Basic .NET code, open the Code Editor window, click File on the menu bar, click Print, and then click the OK button in the Print dialog box