Microsoft Visual Basic: Reloaded Chapter One An Introduction to Visual Basic

Transcription

Microsoft Visual Basic: Reloaded Chapter One An Introduction to Visual Basic
Microsoft Visual Basic:
Reloaded
Chapter One
An Introduction to Visual Basic
2
Overview
■
■
■
■
■
■
■
■
■
■
■
What Is a Computer?
Machine, Assembly and High-Level Languages
Visual Basic & Other High-Level Languages
Object Technology
The Internet and the World Wide Web
Introduction to Microsoft .NET
Language and Syntax
Visual Studio IDE
Solutions and Projects
Creating a New Project
Examples
 2009 Pearson Education, Inc. All rights reserved.
3
What Is a Computer?
■ A computer is a device that can perform
calculations and make logical decisions much
faster than humans can.
■ Computers process data, using sets of
instructions called computer programs.
– These programs guide computers through orderly sets of
actions that are specified by people known as computer
programmers.
 2009 Pearson Education, Inc. All rights reserved.
4
What Is a Computer?
■ A computer is composed of various devices
known as hardware:
–
–
–
–
–
–
–
–
keyboard
screen
mouse
hard drives
memory
DVD drives
printer
processing units
■ The programs that run on a computer are referred to
as software.
 2009 Pearson Education, Inc. All rights reserved.
5
Computer Components
Computers can be thought of as being divided
into six units:
■ Input unit: This “receiving” section of the
computer obtains information from input devices.
– Input can come from devices such as the keyboard and
the mouse.
■ Output unit: This “shipping” section of the
computer takes information that the computer has
processed and places it on various output devices.
– Output can be displayed on screens, printed on paper,
played on audio/video devices, and transmitted over the
Internet.
 2009 Pearson Education, Inc. All rights reserved.
6
Computer Components
■ Memory unit: This “warehouse” section of the
computer stores data while an application is
running. Its information is immediately available for
processing.
– To be executed, computer programs must be in memory.
– The memory unit retains information until it can be sent to
output devices.
– Often, the memory unit is called either memory or primary
memory.
– Random-access memory (RAM) is an example of primary
memory.
– Primary memory is usually volatile, which means that it is
erased when the machine is powered off.
 2009 Pearson Education, Inc. All rights reserved.
7
Computer Components
■ Central processing unit (CPU): The CPU supervises
the operation of the other sections.
■ Arithmetic and logic unit (ALU): The ALU (a part of the
CPU) performs calculations and makes decisions.
■ Secondary storage unit: This unit is the “warehousing”
section of the computer.
– Devices such as hard drives, CD-ROM drives, DVD drives,
and USB memory sticks are secondary storage units.
– These normally hold programs or data that other units are not
actively being used.
– The computer can retrieve this information when it is needed
later in time. Secondary storage is nonvolatile.
– Information in secondary storage takes much longer to access
than information in primary memory.
 2009 Pearson Education, Inc. All rights reserved.
8
Machine, Assembly and High-Level Languages
■ Programmers write instructions in programming
languages. Some of these are directly
understandable by computers, and others require
intermediate translation steps.
■ Computer languages that are in use today can be
divided into three general types:
– machine languages
– assembly languages
– high-level languages
 2009 Pearson Education, Inc. All rights reserved.
9
Machine, Assembly and High-Level Languages
■ A computer can directly understand only its own
machine language.
■ As the “natural language” of a particular computer,
machine language is defined by the computer’s
hardware design. Machine languages are machine
dependent.
■ Machine languages generally consist of streams of
numbers (ultimately reduced to 1s and 0s in the
binary number system).
 2009 Pearson Education, Inc. All rights reserved.
10
Machine, Assembly and High-Level Languages
■ The following section of a machine-language
program demonstrates the incomprehensibility
of machine language to humans:
+1300042774
+1400593419
+1200274027
■ Machine-language programming proved to be
slow and error prone.
 2009 Pearson Education, Inc. All rights reserved.
11
Machine, Assembly and High-Level Languages
■ Programmers began using English-like
abbreviations to represent the computer’s basic
operations.
■ These abbreviations formed the basis of
assembly languages.
LOAD
ADD
STORE
BASEPAY
OVERPAY
GROSSPAY
 2009 Pearson Education, Inc. All rights reserved.
12
Machine, Assembly and High-Level Languages
■ Translator programs called assemblers
convert assembly-language programs to
machine language.
■ Although it is clearer to humans, computers
cannot understand assembly-language code
until it is translated into machine language.
■ Assembly languages still require many
instructions to accomplish even the simplest
tasks.
 2009 Pearson Education, Inc. All rights reserved.
13
Machine, Assembly and High-Level Languages
■ To speed up the programming process,
high-level languages were developed.
■ Programs called compilers convert high-levellanguage programs into machine language.
■ High-level languages look almost like everyday
English and contain common mathematical
notations.
grossPay = basePay + overTimePay
■ Visual Basic is one of the world’s most popular
high-level programming languages.
 2009 Pearson Education, Inc. All rights reserved.
14
Visual Basic
■ Visual Basic evolved from BASIC (Beginner’s
All-purpose Symbolic Instruction Code), which
was developed as a language for writing simple
programs quickly and easily.
■ When Bill Gates founded Microsoft Corporation
in the 1970s, he implemented BASIC on several
early personal computers.
 2009 Pearson Education, Inc. All rights reserved.
15
Visual Basic
■ In the late 1980s and the early 1990s, Microsoft
developed the Microsoft® Windows® graphical
user interface (GUI).
■ Visual Basic was introduced by Microsoft in 1991
to make programming Windows applications
easier.
 2009 Pearson Education, Inc. All rights reserved.
16
Visual Basic
■ Visual Basic is a so-called object-oriented,
event-driven visual programming language.
■ Programs are created with the use of a software
tool called an Integrated Development
Environment (IDE). (Visual Studio)
■ The latest versions of Visual Basic are fully object
oriented and respond to user-initiated events such
as mouse clicks, keystrokes and timers.
■ In Visual Studio, it is convenient to make programs
by dragging and dropping predefined objects like
buttons and textboxes.
 2009 Pearson Education, Inc. All rights reserved.
17
Visual Basic
■ Microsoft introduced its .NET strategy in 2000.
■ The .NET platform allows applications to be
distributed to a variety of devices.
■ Software components created in different .NET
programming languages can communicate with
one another.
■ In 2000, Microsoft announced C#. The C#
programming language was designed
specifically for the .NET platform. Like Visual
Basic, C# is object oriented and has access to
.NET’s library.
 2009 Pearson Education, Inc. All rights reserved.
18
Object Technology
■ Object technology is a packaging scheme for
creating meaningful software units.
■ Almost any noun can be reasonably represented
as a software object.
■ Objects:
– have properties such as color, size and weight
– perform actions such as moving, sleeping or drawing
■ A class specifies the general format of its
objects, and the properties and actions available
to an object depend on its class.
 2009 Pearson Education, Inc. All rights reserved.
19
Object Technology
■ Procedural programming languages focused on
actions (verbs) rather than objects (nouns).
■ Object-oriented languages more naturally reflects the
way in which we perceive the world.
■ This has resulted in significant productivity gains.
– Properly designed classes can be reused on future
projects
– Using libraries of classes reduces the amount of effort
required to implement new systems
– The production of software is more understandable
because it is better organized and has fewer maintenance
requirements.
 2009 Pearson Education, Inc. All rights reserved.
20
Object Technology
■ Instead of worrying about the details of how
objects are implemented, you can focus on the
behaviors and interactions of objects.
■ Visual Basic is one of the world’s most widely
used object-oriented languages.
 2009 Pearson Education, Inc. All rights reserved.
21
The Internet and the World Wide Web
■ In the late 1960s, ARPA rolled out plans to
network the main computer systems of ARPAfunded universities and research institutions.
■ This became known as the ARPAnet, the
grandparent of today’s Internet.
■ Its main benefit proved to be the capability for
quick and easy communication via what came to
be known as electronic mail (e-mail).
 2009 Pearson Education, Inc. All rights reserved.
22
The Internet and the World Wide Web
■ The protocol for communicating over the ARPAnet
became known as the Transmission Control
Protocol (TCP).
■ TCP ensured that messages, consisting of pieces
called “packets,” were properly routed from sender to
receiver, arrived intact and were assembled in the
correct order.
■ One challenge was to enable different networks to
communicate with each other.
– ARPA accomplished this by developing the Internet Protocol
(IP), which created the current architecture of the Internet.
■ The combined set of protocols is now called TCP/IP.
 2009 Pearson Education, Inc. All rights reserved.
23
The Internet and the World Wide Web
■ The World Wide Web is a collection of hardware
and software associated with the Internet.
■ In 1989, Tim Berners-Lee of CERN began to
develop a technology for sharing information via
“hyperlinked” text documents called HyperText
Markup Language (HTML).
■ He also wrote communication protocols such as
HyperText Transfer Protocol (HTTP).
■ In October 1994, Berners-Lee founded the World
Wide Web Consortium, devoted to developing
technologies for the World Wide Web.
 2009 Pearson Education, Inc. All rights reserved.
24
The Internet and the World Wide Web
■ The Internet and the web will surely be listed
among the most important creations of
humankind.
■ Today’s applications can be written to
communicate among the world’s computers.
This is the focus of Microsoft’s .NET strategy.
■ The Internet and the World Wide Web:
– make information instantly and conveniently accessible,
– and allow individuals and small businesses to achieve
worldwide exposure.
 2009 Pearson Education, Inc. All rights reserved.
25
Introduction to Microsoft .NET
■ In June 2000, Microsoft announced its .NET
initiative
– The .NET initiative permits developers to create
applications in any .NET-compatible language.
– Part of the initiative includes Microsoft’s ASP.NET.
■ The .NET strategy extends the idea of software
reuse to the Internet.
■ Visual programming enables .NET programmers
to create applications using prepackaged
graphical components.
 2009 Pearson Education, Inc. All rights reserved.
26
.NET Framework
■ The Microsoft .NET Framework:
– executes applications and web services
– contains a class library (Framework Class Library)
– and provides many other programming capabilities.
■ The .NET framework is a platform with which you
can develop software applications and libraries
called managed applications; it provides you with
the compiler and tools you need to build, debug,
and execute managed applications.
■ Prior to this approach, unmanaged applications had
to manage their own services which sometimes led
to security holes and data corruption.
 2009 Pearson Education, Inc. All rights reserved.
27
.NET Framework
■ CLR: Common Language
■ WCF: Windows Communication Foundation
■ WPF: Windows Presentation Foundation
■ WF: Windows Workflow Foundation
■ LINQ: Language Integrated Query
■ Cardspace: Manages online identities
■ AJAX: Asynchronous JavaScript and XML
■ REST: Representational State Transfer
 2009 Pearson Education, Inc. All rights reserved.
28
.NET Framework
 2009 Pearson Education, Inc. All rights reserved.
29
Spoken Languages and Syntax
■ Sprechen Sie Englisch?
■ ¿Habla ingles?
■ Você fala inglês?
■ OO-day ou-yay eak-Spay Eng-hay ish-lay?
 2009 Pearson Education, Inc. All rights reserved.
30
Examples of Programming Languages
■
■
■
■
■
C: fast, efficient code
Java: platform independence
BASIC: ease of use
Visual Basic: user interface design using forms
Perl: text manipulation
 2009 Pearson Education, Inc. All rights reserved.
31
Visual Basic.NET syntax
If x>5 Then
MessageBox.Show("I am greater than 5.")
Else
MessageBox.Show ("I am not greater than 5.")
End If
 2009 Pearson Education, Inc. All rights reserved.
32
C# Syntax
if (x>5)
{
MessageBox.Show ("I am greater than 5.");
}
else
{
MessageBox.Show ("I am not greater than 5.");
}
 2009 Pearson Education, Inc. All rights reserved.
33
Learning a Programming Language
■ Syntax
■ Structure
■ Rules
■ Punctuation
■ Reserved Words (keywords) eg. If, Else
■ Visual Basic.NET is NOT case sensitive.
MyCase is the same as myCase and MYCASE.
■ Comment lines start with an apostrophe (')
 2009 Pearson Education, Inc. All rights reserved.
34
Visual Studio IDE
■ Visual Studio® is Microsoft’s integrated
development environment (IDE) for creating,
running and debugging applications.
■ The IDE allows you to create applications by
dragging and dropping existing building blocks
into place.
■ This technique is called visual programming.
■ It is a Rapid Application Development (RAD) tool
because it is focused on productivity.
 2009 Pearson Education, Inc. All rights reserved.
35
Visual Studio Languages
■
■
■
■
■
Visual Basic.NET
C# (pronounced C-Sharp)
J# (pronounced J-Sharp)
C++ (pronounced C plus plus)
ASP.NET (web page creation)
 2009 Pearson Education, Inc. All rights reserved.
36
Some Advantages of Visual Studio IDE
■ Greatly reduces amount of code you must write
■ Automatically colors code depending what it is
– Comments are Green
– Keywords are Blue
■ Organizes code in separate files
■ IntelliSense
– Code Snippets
■ Real-time compiler feedback
– Blue squiggle under code indicates an error
– Green squiggle under code indicates a possible error
 2009 Pearson Education, Inc. All rights reserved.
37
Solutions and Projects
■ Visual Basic organizes applications into solutions
and projects.
■ Solutions contain one or more projects.
■ A project is a group of related files, such as code
and images that might make up a program.
 2009 Pearson Education, Inc. All rights reserved.
Solutions, Projects, and Files
 2009 Pearson Education, Inc. All rights reserved.
39
Creating a New Project
■ Select either File > New Project…, which
creates a new project, or File > Open
Project…, which opens an existing project.
■ From the Start Page, under the Recent
Projects section, click the link Create:
Project… or Open: Project….
■ Click either the New Project Button or the
Open File Button.
 2009 Pearson Education, Inc. All rights reserved.
40
Working With a Project
■ The Solution Explorer window displays a list of
the files in a project and the projects in a solution.
■ The Properties window displays an object’s
attributes, such as its size, color and position. The
Properties window allows you to set object
properties visually without writing code.
■ Using visual programming, you can “drag and
drop” controls onto the Form from the Toolbox.
Properties icon
Solution Explorer icon
Object Browser icon
Toolbox icon
 2009 Pearson Education, Inc. All rights reserved.
41
Properties Window
Object’s
name
(Form1)
Object’s class
(System.Windows.Forms.Form)
Down arrow for selecting Form
or control objects
Component object box
Categorized icon
Alphabetical icon
Items that have been
changed from their default
values (by the user or by Visual
Studio) are listed in bold
Selected property
Design category
Description of
selected property
Properties
(left column)
Property values
(right column)
Figure 2.22 | Properties window displaying a Form’s properties.
 2009 Pearson Education, Inc. All rights reserved.
Form Controls
 2009 Pearson Education, Inc. All rights reserved.
43
Hello World!
Console App:
Console.WriteLine("Hello World")
Windows Form App:
MessageBox.Show("Hello World!")
 2009 Pearson Education, Inc. All rights reserved.
44
Web Browser
■ Create form
– Text: My Own Browser
– Width/Height: (640,480)
■ Create Webrowser control
– Width/Height: (607,385)
– X/Y: (12,12)
– Name: myBrowser
■ Create textbox
– Width/Height: (526,20)
– X/Y: (12,411)
– Name: txtURL
 2009 Pearson Education, Inc. All rights reserved.
45
Web Browser (cont.)
■ Create button
– Text: GO
– X/Y: (544,411)
– Name: btnGo
■ Add this line of code to the button:
myBrowser.Navigate(txtURL.Text)
 2009 Pearson Education, Inc. All rights reserved.