Docsity

Introduction to Visual Basic: A Beginner's Guide to Creating Windows Applications, Study notes of Computer Science

Toggle sidebar
bg1
1
More on Hierarchy Charts
Focus on the organization of the program, not the processing.
Shows “building blocks”, not their use
Modules may have sub-modules, just as in the calculate
stamps example
May refine on the go, as more detail emerges
Unit 3a
Intro to Visual Basic
1
Class
average
Calculate
average Display averageCompute sum
and number of grades
Get grade
Example
Average of two numbers
There may be more detail, e.g., a module that adds, one that
divides by two, etc.
Show meaningful processes. If the problem is “compute
payroll”, it is not OK to have one module “compute payroll”.
May not be good detail to read one character at a time in a
“Get input” module
Unit 3a
Intro to Visual Basic
2
Average
program
Display averageCompute average
of two inputs
Get Input
Get first
number Get second
number
Unit 3a
Intro to Visual Basic
CS 106
Computing Fundamentals II
Intro to Visual Basic
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Introduction to Visual Basic: A Beginner's Guide to Creating Windows Applications and more Study notes Computer Science in PDF only on Docsity!

More on Hierarchy Charts

 Focus on the organization of the program, not the processing.

 Shows “building blocks”, not their use

 Modules may have sub-modules , just as in the calculate

stamps example

 May refine on the go, as more detail emerges

1 Unit 3a Intro to Visual Basic average^ Class Get grade and number of gradesCompute sum Calculate average Display average

Example

 Average of two numbers

 There may be more detail, e.g., a module that adds, one that

divides by two, etc.

 Show meaningful processes. If the problem is “compute

payroll”, it is not OK to have one module “compute payroll”.

 May not be good detail to read one character at a time in a

“Get input” module

2 Unit 3a Intro to Visual Basic Average program Get Input Compute average of two inputs Display average Get first number Get second number Unit 3a Intro to Visual Basic CS 106 Computing Fundamentals II Intro to Visual Basic

Visual Basic

4 Unit 3a Intro to Visual Basic

 A relatively simple but general-purpose language used to create

Windows applications.

 Full programming power

 Graphical User Interface (GUI) for every program.

 Sequence of instructions that execute is controlled by events triggered by activities on the GUI.  This is called Event Driven Programming

 Integrated Development Environment (IDE)

 Interface design, Coding, Testing & Debugging Visual Basic Introduction

A Sample VB Program GUI

5 Unit 3a Intro to Visual Basic

Note: In a “non-visual” programming language ( e.g. C),

it will take hundreds of lines of code to generate such a GUI. Visual Basic Introduction

Visual Basic’s History

Unit 3a (^6) Intro to Visual Basic

 BASIC

 Developed at Dartmouth in early 1960s, to provide non-science students access to computers  Beginner's All-purpose Symbolic Instruction Code  No GUI -- everything done at the command line  Much like the “Toy C Program” from last lecture

 Visual Basic

 Created by Microsoft in 1991  Visual Basic 2005 is similar to original VB, but more powerful History

Visual Basic Startup Window

10 Unit 3a Intro to Visual Basic Getting Started – New Project

Start a New Project

11 Unit 3a Intro to Visual Basic Getting Started – New Project Step 1: Select New Project from File menu

New Project Dialog Box

Unit 3a (^12) Intro to Visual Basic Getting Started – New Project Step 2: select windows app. Step 3: Give your project a name (e.g., homework2); Don’t put spaces in the name. Step 4: Click OK.

Save All …

13 Unit 3a Intro to Visual Basic Getting Started – New Project Step 5: Save your project using the Save All option from the File menu. Very Very important to do this!

Save Project Dialog Box

14 Unit 3a Intro to Visual Basic Getting Started – New Project Step 6: Choose where to store your project. Use the Browse… button to find your CS106 folder and your homework subfolder. Step 7: Checkmark this box if it is not checkmarked. Step 8: Click the Save button.

What Did VB Just Do?

Unit 3a (^15) Intro to Visual Basic Getting Started – New Project CS106 subfolder you created for homework 2 Solution folder created by VB Contents of solution folder Project folder created by VB Contents of project folder

Form Designer Window

19 Unit 3a Intro to Visual Basic

 The main window in which the Form is designed.

 The Form is the main interface of the project

 Provides controls to program execution  Facilitates input and output Moving Around in the IDE

Toolbox

20 Unit 3a Intro to Visual Basic  Contains a complete list of controls that can be used.  Frequently used controls:

 Button

 Label

 ListBox

 TextBox

Moving Around in the IDE

Auto Hide

 Hides Toolbox when not in use

 Vertical push pin indicates disabled auto hide

 Click the push pin to make it horizontal and enable auto hide.

Unit 3a Intro to Visual Basic 21

Push pin

Moving Around in the IDE

Placing a Control onto the Form

22 Unit 3a Intro to Visual Basic

Three ways:

 Double-click

 Drag

 Click, Point, and Drag

VB Controls Example: Form Window with Four Controls 23 Unit 3a Intro to Visual Basic To select a control, click on it. Sizing handles will appear when a control is selected. Text box Button Label List box VB Controls

Positioning Controls

Unit 3a (^24) Intro to Visual Basic Proximity line VB Controls

Focus

28 Unit 3a Intro to Visual Basic

 Clicking on a control gives the focus to the control

 Example: if you click on a text box, a cursor appears in the text box, and you can type into the text box.  The text box is said to have the focus

 Clicking on a different control takes the focus from the first

control and gives it to the second

VB Controls

Properties of a Control

29 Unit 3a Intro to Visual Basic  Text  Autosize  Font  ForeColor  BackColor  ReadOnly VB Controls  Each control has a set of properties

 Set when the interface is designed

 Or set in the program source code

 Some often used properties are:

Properties Window

Unit 3a (^30) Intro to Visual Basic

Press F4 to

display the Properties window for the selected control. VB Controls Categorized View Alphabetical View

Properties Window

31 Unit 3a Intro to Visual Basic Properties (^) Settings Selected control VB Controls

Example: Setting the ForeColor

32 Unit 3a Intro to Visual Basic

  1. Select a control

2. Click on ForeColor property

(in Properties Window).

  1. Click on button at right of settings box.
  2. Click on Custom tab to obtain display shown.
  3. Click on a color. VB Controls

The Name Property

Unit 3a (^33) Intro to Visual Basic

 The VB default is to give controls very generic names like

Button1, Button2, ListBox

 But as programmers we want descriptive names like txtInputIncome to indicate a text box control that will be used to input income

 Change the name from the VB default to something descriptive

 Easier for programmer to reference control by descriptive name rather than generic name VB Controls

Button Control

 The caption on the button should indicate the effect of

clicking on the button.

 Text property determines caption.

Unit 3a Intro to Visual Basic 37 VB Controls

Add an “Access Key"

Now pressing the “ Alt + C ” key has the same effect as pressing the button. Unit 3a Intro to Visual Basic 38 VB Controls The character that follows the ampersand

Label Control

Unit 3a (^39) Intro to Visual Basic  Used to identify the contents of a text box.  Text property specifies caption.  By default, label automatically resizes to accommodate caption on one line.  When the AutoSize property is set to False, label can be resized manually. Used primarily to obtain a multi-rowed label. VB Controls

ListBox Control

40 Unit 3a Intro to Visual Basic

 Initially used to display several pieces of output.

 Later used to select from a list.

VB Controls

Steps

41 Unit 3a Intro to Visual Basic

1. Design the user interface

 Add controls to the Form Window  Set properties  Configure the appearance of the controls

2. Determine which events the controls should recognize

3. Write the event procedures for those events

Software Development in VB

Code Window

Unit 3a (^42) Intro to Visual Basic Method Name box Class Name box Page tab  Get to the code window by double clicking on a control in the form design window Software Development in VB

Start of Code for Example

46 Unit 3a Intro to Visual Basic Public Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged ... End Sub End Class VB Events  Event Procedure Outline:

Structure of Event Procedures

47 Unit 3a Intro to Visual Basic Private Sub objectName_event(...)Handles objectName.event statements End Sub

(...) is filled automatically with:

(ByVal sender As System.Object, ByVal e As System.EventArgs) VB Events Header

Header of Event Procedure

Unit 3a (^48) Intro to Visual Basic Private Sub btnRed_Click(…) Handles btnRed.Click

Name of event procedure Identifies event

can be changed. Private Sub Button_Press(…) Handles btnRed.Click VB Events

Name of event

procedure changed But name of control did not change

More Code for Example

49 Unit 3a Intro to Visual Basic Public Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst .TextChanged txtFirst .ForeColor = Color.Blue End Sub End Class VB Events

Setting Property Values

50 Unit 3a Intro to Visual Basic

Properties of a control can also be set by assignment

statements in program code:

 txtFirst.Text = “Hello World”

 txtSecond.Visible = True

 btnFirst.ForeColor = Color.White

VB Events

IntelliSense

Unit 3a (^51) Intro to Visual Basic Automatically pops up to give the programmer help in selecting properties and names. VB Events

Coding the Button Click Event (cont.) 55 Unit 3a Intro to Visual Basic VB Events Public Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub End Class Final Event Procedure Code for Example 56 Unit 3a Intro to Visual Basic Private Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.Black End Sub Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub VB Events

Handling Multiple Events

Unit 3a (^57) Intro to Visual Basic Private Sub Button_Click(...) Handles btnRed.Click, txtSecond.Leave txtFirst.ForeColor = Color.Red End Sub Event procedure can be invoked by two events. VB Events

Altering Properties of the Form

58 Unit 3a Intro to Visual Basic

 The following does not work:

frmDemo.Text = "Demonstration"

 The form is referred to by the keyword Me

Me.Text = "Demonstration"

VB Events