57
An Introduction to Visual Basic .NET Chapter Microsoft Visual Basic .NET: Reloaded 1

An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

Embed Size (px)

Citation preview

Page 1: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

An Introduction to Visual Basic .NET

Chapter Microsoft Visual Basic .NET: Reloaded

1

Page 2: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

2Microsoft Visual Basic .NET: Reloaded

Objectives

• Explain the history of programming languages

• Define the terminology used in object-oriented programming languages

• Create a Visual Basic .NET Windows-based application

• Manage the windows in the IDE

• Set the properties of an object

Page 3: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

3Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Add a control to a form

• Use the Label and Button tools

• Enter code in the Code Editor window

• Save a solution

• Start and end an application

Page 4: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

4Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Print a project’s code

• Close a solution

• Open an existing solution

Page 5: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

5Microsoft Visual Basic .NET: Reloaded

Programmers

• Programmers

• People who write programs

• Programs

• Directions given to a computer accomplish a given task

Page 6: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

6Microsoft Visual Basic .NET: Reloaded

A Brief History of Programming Languages

• Machine Languages• 0 (for on switch)

• 1 (for off switch)

• Tedious and prone to error

• Assembly Languages• More advanced than machine languages

• Mnemonics (memory aids)

• Examples: MUL b1, ax

• Require an assembler

Page 7: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

7Microsoft Visual Basic .NET: Reloaded

A Brief History of Programming Languages (continued)

• High Level Languages• Require Interpreter or compiler

• Interpreter translates high-level into machine code line-by-line while program is running

• Compiler translates entire program before program has run

• English like syntax

• Allow for object-oriented programming• Programmer focuses on using objects to achieve

the program’s goal

Page 8: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

8Microsoft Visual Basic .NET: Reloaded

OOP Terminology

• Object

• Anything that can be seen touched, or used

• Has Attributes - also called Properties

• Characteristics that describe the object

• Has Behaviors - also called Methods

• Operations the object is capable of performing

• Class

• Pattern or blueprint used to create an object

Page 9: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

9Microsoft Visual Basic .NET: Reloaded

OOP Terminology (continued)

• Encapsulation• Combination of attributes and behaviors that

describe object created by class

• Abstraction• Hiding internal details of object from user

• Inheritance• Ability to create one class from another

• Polymorphism• Same instruction carried out differently

depending on the object giving the instruction

Page 10: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

10Microsoft Visual Basic .NET: Reloaded

Illustration of OOP Terms

Page 11: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

11Microsoft Visual Basic .NET: Reloaded

Visual Studio .NET

• IDE

• Integrated Development Environment

• Contains all the features needed to create, run, and test programs

• Editor for entering program instructions

• Compiler for running and testing program

• Allows creation of Web-based and Windows-based applications

Page 12: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

12Microsoft Visual Basic .NET: Reloaded

Visual Studio .NET (continued)

• Solution .sln

• Container for project and files for application

• Project .vbproj

• Container for storing files associated with a specific piece of the application

• Files

• Individual files necessary to store information about the application

• Include forms and class information

Page 13: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

13Microsoft Visual Basic .NET: Reloaded

Illustration of solution, project, and file

Page 14: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

14Microsoft Visual Basic .NET: Reloaded

Starting Microsoft Visual Studio .NET

Page 15: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

15Microsoft Visual Basic .NET: Reloaded

Visual Studio .NET 2003 startup screen

Page 16: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

16Microsoft Visual Basic .NET: Reloaded

Purpose of windows in the IDE

Page 17: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

17Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 18: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

18Microsoft Visual Basic .NET: Reloaded

Creating A Visual Basic .Net Windows-Based Application

Page 19: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

19Microsoft Visual Basic .NET: Reloaded

Creating A Visual Basic .Net Windows-Based Application (continued)

Page 20: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

20Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 21: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

21Microsoft Visual Basic .NET: Reloaded

The Dynamic Help Window

Page 22: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

22Microsoft Visual Basic .NET: Reloaded

The Windows Form Designer Window• Windows form object (form)

• Foundation for the user interface

Page 23: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

23Microsoft Visual Basic .NET: Reloaded

Solution Explorer Window

• Contains: • Projects

• References• Addresses of memory cells

• Namespace – block of internal memory cells• Contains code that defines a group of related

classes

• Source files• Contains program instructions – called code• Examples: Assembly information, Forms

Page 24: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

24Microsoft Visual Basic .NET: Reloaded

The Solution Explorer Window (continued)

Page 25: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

25Microsoft Visual Basic .NET: Reloaded

The Properties Window• Object box (contains name of selected object)

• Properties list has 2 columns• Left column displays property names• Right column displays Settings box

• Contains current value for each property

Page 26: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

26Microsoft Visual Basic .NET: Reloaded

Properties of a Windows Form Object• Select object to display properties

• Dot member access operator• Separates words in property name

• Example: System.Windows.Form.Form

Page 27: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

27Microsoft Visual Basic .NET: Reloaded

Properties of a Windows Form Object (continued)

Page 28: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

28Microsoft Visual Basic .NET: Reloaded

The Toolbox Window• Toolbox contains tools and other components

used in creating application• Tools are called controls• Controls are displayed on the form

Page 29: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

29Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 30: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

30Microsoft Visual Basic .NET: Reloaded

The Label Tool• Use tool to instantiate a Label object

• Object displays text that user is not allowed to edit

Page 31: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

31Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 32: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

32Microsoft Visual Basic .NET: Reloaded

The Button Tool• Button Tool instantiates a Button control

• Button Control used to perform immediate action when clicked

Page 33: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

33Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 34: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

34Microsoft Visual Basic .NET: Reloaded

The Code Editor Window

• Events

• Actions such as clicking, double-clicking, and scrolling

• Event Procedures

• Set of Visual Basic instructions (code)

• Tells an object how to respond to an event

• Contained in the Code Editor Window

Page 35: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

35Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 36: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

36Microsoft Visual Basic .NET: Reloaded

The Code Editor Window (continued)

Page 37: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

37Microsoft Visual Basic .NET: Reloaded

The Code Editor Window (continued)

• Class Definition• Block of code that specifies attributes and

behaviors of an object

• Inherits keyword• Inherits System.Windows.Forms.Form

• Allows derived class to inherit attributes and behaviors of base class

• Windows Form Designer generated code section• Contains code generated by the designer

Page 38: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

38Microsoft Visual Basic .NET: Reloaded

The Code Editor Window (continued)

Page 39: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

39Microsoft Visual Basic .NET: Reloaded

Code Editor Window (continued)

• Class name list box

• Lists names of objects on user interface

• Method name list box

• Lists events selected object responds to

• Procedure

• Block of code that responds to an event

Page 40: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

40Microsoft Visual Basic .NET: Reloaded

The Code Editor Window (continued)

Page 41: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

41Microsoft Visual Basic .NET: Reloaded

Code Keyword Examples

• Public class frmCommission• Public indicates class can be used by code

defined outside the class

• Private Sub btnExit_Click…• Sub is abbreviation for sub procedure

• Block of code that performs a specific task

• Private indicates procedure can be used only in class in which defined

• btnExit is name of object on user interface

• _Click is name of the event (Click)

Page 42: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

42Microsoft Visual Basic .NET: Reloaded

The Me.Close Method

• Instructs computer to terminate current application

• Me refers to the current form

• .Close refers to an intrinsic method

• Part of built-in VB .NET language

• Code is performed using sequential processing, also known as sequence structure

Page 43: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

43Microsoft Visual Basic .NET: Reloaded

The Me.Close Method (continued)

Page 44: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

44Microsoft Visual Basic .NET: Reloaded

Saving a Solution

Page 45: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

45Microsoft Visual Basic .NET: Reloaded

Starting and Ending an Application

Page 46: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

46Microsoft Visual Basic .NET: Reloaded

Setting the startup object

Page 47: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

47Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 48: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

48Microsoft Visual Basic .NET: Reloaded

Starting an Application

Page 49: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

49Microsoft Visual Basic .NET: Reloaded

Printing Your Code

Page 50: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

50Microsoft Visual Basic .NET: Reloaded

Closing the Current Solution

Page 51: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

51Microsoft Visual Basic .NET: Reloaded

Opening an Existing Solution

Page 52: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

52Microsoft Visual Basic .NET: Reloaded

Programming Example – State Capitals

• Application displays the capital of the state in a label when a button with the state’s name is clicked

• Create buttons with labels for the following states : Alabama, Alaska, Arizona, and Arkansas

• Create an exit button to close the application

Page 53: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

53Microsoft Visual Basic .NET: Reloaded

User Interface

Page 54: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

54Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings

Page 55: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

55Microsoft Visual Basic .NET: Reloaded

Code

Page 56: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

56Microsoft Visual Basic .NET: Reloaded

Summary

• Programming languages have evolved from machine languages to high-level program languages

• Object Oriented Programming includes:

• Objects – things with attributes and behaviors

• Attributes are the characteristics of an object

• Behaviors are actions that an object performs

• Integrated Development Environment (IDE)

• Used to manage windows, set properties, and write code for an application

Page 57: An Introduction to Visual Basic.NET Chapter Microsoft Visual Basic.NET: Reloaded 1

57Microsoft Visual Basic .NET: Reloaded

Summary (continued)• Forms are the user interface of an application

• Contain controls such as Buttons and Labels• Have code to perform specific tasks

• Code contained in Code Editor Window

• Additional Tips:• Saving your Solution (do often)

• Saves all files associated with the application• When starting an application

• You must specify a startup form• Print your code

• For ease of understanding and future reference