74
Special Lecture: Visual Basic Asst.Prof.Dr.Supakit Nootyaskool Faculty of Information Technology KMITL Ref: Diane Zak, Programming with Microsoft® Visual Basic 2010 5 th , Cengage Learning

Special Lecture: Visual Basic

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Special Lecture: Visual Basic

Special Lecture: Visual Basic

Asst.Prof.Dr.Supakit Nootyaskool

Faculty of Information Technology

KMITLRef: Diane Zak, Programming with Microsoft® Visual Basic 2010 5th , Cengage Learning

Page 2: Special Lecture: Visual Basic

1. What is Visual Basic

Page 3: Special Lecture: Visual Basic

1.1 History of VB from CLI to GUI

• 1964, Dartmouth BASIC • Generate of Beginer’s All-purpose Symbolic Instruction Code

(BASIC).

• 1979, Microsoft• Presented Basic-C for MS-DOS

• 1991, Microsoft• Presented the first version of Visual Basic

• 2006, Microsoft• Introduced VB.NET (VB 6.0)

Page 4: Special Lecture: Visual Basic

1.2 Difference between VBA and VB.NET

• VB.NET• a modern language

• having the same capabilities as C#

• Support 64bit and modern instruction set.

• Support object-oriented approach

• VBA• Similar to Visual Basic 6,

• obsoleted since about 1997

• Support 32bit

https://www.freelancinggig.com/blog/2019/01/21/what-is-the-difference-between-visual-basic-net-and-vba/

Page 5: Special Lecture: Visual Basic

2. Variable & Calculation

Page 6: Special Lecture: Visual Basic

2.1 Variables

Page 7: Special Lecture: Visual Basic

2.2 Variables

Page 8: Special Lecture: Visual Basic

2.3 Rule for naming variables

1. The first character in the variable name uses only an English-letter or an underscore symbol.

2. The variable name consists of letters, numbers or underscores symbol. VB does not accept punctuation characters, white space, special characters.

3. The length of character name has a hundred character but the possible way and good for memorizing should declare not over 32 characters.

4. Some words are reserved, we cannot use it, such as “Sub”, “Double”, “integer”, etc.

Page 9: Special Lecture: Visual Basic

2.4 Variable Declaration

• Recommend to use is in form “xxxYYYYYYY” for the variable name,• xxx is three characters of the variable type

• YYY is the process name.

Example

Dim intHour as integer

Dim dblRatio as double

Dim strName as string

Page 10: Special Lecture: Visual Basic

2.5 Arithmetic operators

Page 11: Special Lecture: Visual Basic

2.6 Arithmetic assignment operators

VB uses arithmetic assignment operators similar to C language, as

+= addition assignment

-= subtraction assignment

*= multiplication assignment

/= division assignment

Example

a +=10 similar to a = a+10

Page 12: Special Lecture: Visual Basic

3. Application Development1

Page 13: Special Lecture: Visual Basic

3.1 Button: Hello world

Page 14: Special Lecture: Visual Basic

3.2 Convert Radius to Area

https://www.wikihow.com/Calculate-the-Area-of-a-Circle

Page 15: Special Lecture: Visual Basic

Topic

• Conditions (if-else, select case)• Debugging• Global and local variable• Textbox component• Label component• String to value conversion• Dev: Feet to Meter• Dev: Radius to Area with value limitation• Dev: Word length• Dev: Floating point with precision limited

Page 16: Special Lecture: Visual Basic

4. Conditions

Page 17: Special Lecture: Visual Basic

4.1 Comparison Operators

Comparison Operator Operation

= equal to

> greater than

>= greater than or equal to

< less than

<= less than or equal to

<> not equal to

Page 18: Special Lecture: Visual Basic

4.2 IF Condition

Example 1:If a > 10 Then

a = a+2End if

Example 2:If a > 10 Then

a = a+2Else

a = a-2End if

Page 19: Special Lecture: Visual Basic

4.3 Textbox component

Page 20: Special Lecture: Visual Basic

number = val(string)Function converts a string to a numberExample:

Result = val(“15.3”) + 7

outval = convert.METHOD(inVal)The Convert class is converter from an input value to outpvalue by METHODExample:

Result = convert.ToDecmial (“15.3”)

4.4 String to value conversion

Page 21: Special Lecture: Visual Basic

str = format(string, “TYPE”)The format function converts a string-value to a string as a defined TYPE (currency, fixed, standard, percentExample:

piVal = format(“3.1415”, “ ”)expVal = format(“2.7182”, “ ”)

electronVolt = format(“1.6e-19”, “ ”)lightSpeed = format(“186000”, “ ”)

Page 22: Special Lecture: Visual Basic

4.5 Test: String to value

Page 23: Special Lecture: Visual Basic

4.6 Test error: convert str to double!!

Page 24: Special Lecture: Visual Basic

4.6 Dev: Grade Display

Page 25: Special Lecture: Visual Basic

4.7 Dev: Grade display

btnDisplay

lbGrade

tbScore

Page 26: Special Lecture: Visual Basic

4.8 Debugging and step running

3 Enter tbScore

Page 27: Special Lecture: Visual Basic

4.9 Select Case

Select case

Not match in the list of case, it runs on this path

Page 28: Special Lecture: Visual Basic

4.9 Select Case--Example code--data = 10x = 1Select case data

case 1x = x+1

case 2 to 5x = x+2

case elsex = x+3

End select

Page 29: Special Lecture: Visual Basic

4.10 Dev: Grade_v2

Page 30: Special Lecture: Visual Basic

4.11 Dev: Feet to Meter

Page 31: Special Lecture: Visual Basic

5. Loops

Page 32: Special Lecture: Visual Basic

5.1 Loops

• Visual basic has three loop types

While Until

For

Page 33: Special Lecture: Visual Basic

5.2 While/Until LoopCondition in while loop rans the statement when the condition is “True”

Condition in until loop rans the statement when the condition is “False”

Pretest loop

‘Example 1

a = 0

While a < 10

Debug.writeln(a)

a=a+1

End while

‘Example 2

a = 0

Do Until a >= 10

Debug.writeln(a)

a=a+1Loop

1. Start

2. Condition

3. Change

Page 34: Special Lecture: Visual Basic

5.3 While/Until LoopCondition checking in this loop when is “True” the program runs the statement in the loop.

Posttest loop

‘Example 1

a = 0

Loop

debug.writeln(a)

a=a+1

Do While a < 10

‘Example 2

a = 0

Loop

debug.writeln(a)

a=a+1

Do Until a>=10

1. Start

2. Condition

3. Change

Page 35: Special Lecture: Visual Basic

5.4 Loop Count Up & Debugging

btnWhileLoop

btnUntilLoop

• Open immediate windows• CTRL+G

Page 36: Special Lecture: Visual Basic

5.5 Immediate windows

Page 37: Special Lecture: Visual Basic

5.6 Delay time with Sleep function• The loop runs very quickly which you cannot make

a delay time with a large loop, by the reason related to a high computation time generating.

• The thread object has a function managing delay time.

System.Threading.Thread.Sleep(1000)

Page 38: Special Lecture: Visual Basic
Page 39: Special Lecture: Visual Basic

5.7 Refresh display• Object in the dialog or form does not immediately

refresh after giving an update data. The user can manual the update with the refresh command.

Object.refresh()Me.refresh()

Page 40: Special Lecture: Visual Basic

5.8 For ... Loop

For a As Integer = 0 to 9

debug.writeline(a)

Next

Dim b As Integer

For b = 0 to 9

debug.writeline(b)Next

For c As Integer = 9 to 0 Step -1

debug.writeline(c)

Next

Declaration variable

inside loop

Declaration variable

outside loop

Count down

Page 41: Special Lecture: Visual Basic

5.9 Counter 1 to 30 Sec

1. Place a button (btnStart) and a label (lbCount) to an application form.

2. Write a code below place to click event

btnStart.Enabled = False

Dim count As Integer

For count = 1 To 30

lbCount.Text = count

Me.Refresh()

System.Threading.Thread.Sleep(1000)

Next

btnStart.Enabled = True

Disable during

operation

Page 42: Special Lecture: Visual Basic

5.10 Listbox component

• List box is data collector and display.

• Each Item in the listbox is controlled by• Items.Add(item) Add an Item

• Items.Clear() Clear all Item

• Items.Insert(index, item) Insert and Items

• Items.Count() Number of items

• Items.RemoveAt(index) Remove an item at index• Items.Contain(item) Check Item is contained

Page 43: Special Lecture: Visual Basic

5.10 Listbox

ListBox

Manual Item

add/remove

Page 44: Special Lecture: Visual Basic

5.11 Concatenating strings

• Concatenating string uses operation “&” to connect between strings

• Example

dim salary as currency = 15000

dim suffix as string = “baht”

lbOutput.txt = “remain:” & salary & suffix

Ampersand

Page 45: Special Lecture: Visual Basic

5.12 InputBox Function• The InputBox function is an input dialog box containing

input message, ok and cancel button.

Example

strinput = InputBox(“msg tells user”, “a title”)

strinput = InputBox(“Keyword?”, “Confirmation”)

InputBox(description, title text)

Page 46: Special Lecture: Visual Basic

5.13 ControlChars.Newline• C language uses “\n” to set a new line.

• VB uses “ControlChars.NewLine”.

dim strInput as string = InputBox(“Keyword?”, “Confirmation”)

lbOutput = strInput & ControlChars.NewLine & strInput

Page 47: Special Lecture: Visual Basic

5.14 Lower case / Upper case text

Example

Dim txtIn as String = “HeLLo”

Dim txtOut as String

Debug.writline(txtIn.ToUpper)

Debug.writline(txtIn.ToLower)

String.ToUpperString.ToLower

Page 48: Special Lecture: Visual Basic

6. Array

Page 49: Special Lecture: Visual Basic

6.1 Array Variable

• Single variable collects a single data and also not relationship to other variable.• Dim v as integer

• Array variable is a special variable collecting many data by the same data type.

• Example

Dim height(3) as integer

Dim varname(size) as datatype

Page 50: Special Lecture: Visual Basic

6.2 Array and Indexing

• Index number at the first element is 0.

• Define variable with assignment

Dim height as integer() = {11, 22, 33}

lbOut.text = height(0) + height(2)

height(3) = height(0) + height(2)

11 22 33

Index: 0 1 2

Page 51: Special Lecture: Visual Basic

6.3 Methods for Array

• VB uses object array having many method (function) for management. There are methods

Methods Description

Array.Length Get a number element

Array.GetUpperBound(0) Get a max positon index

Page 52: Special Lecture: Visual Basic

6.4 One-dimension array

Dim strCity as string() = {“Bangkok”,

“Changmai”,

“Chonburi”,

“Rayong”}

Bangkok Changmai Chonburi Rayong

strC

ity(

0)

label.text = strCity(0)

Page 53: Special Lecture: Visual Basic

6.5 Two techniques for traveling in Array1) FOR, WHILE, DO-While commands

‘Example1

Dim X(3) as integerX(0) = 10X(1) = 20X(2) = 30

For each id as integer in X Debug.writeline( X(id) )

Next

Page 54: Special Lecture: Visual Basic

‘//For Each.. Next styleDim x(3) as integer = {11,22,33}For each k as integer in x

Debug.writeline(k)Next

6.6 Two techniques for traveling in Array2) FOR EACH

Page 55: Special Lecture: Visual Basic

6.7 Test For Each

Page 56: Special Lecture: Visual Basic

6.8 Dev: Average value calculator

• Let’s you develop a program having two functional and two non-functional.• (F) Add the input value from a textbox to the listbox

• (F) Calculate average value from the value in the listbox

• (NF)All value during calculation should keep in an array.

• (NF) The value format is the floating point

Page 57: Special Lecture: Visual Basic

6.9 Dev: Average value calculator

lbValue

tbInputbtnAdd

btnAvg

Page 58: Special Lecture: Visual Basic

6.9 Dev: Average value calculator

Page 59: Special Lecture: Visual Basic

6.9 Dev: Sort value in the listbox

Page 60: Special Lecture: Visual Basic
Page 61: Special Lecture: Visual Basic

7. Multi-dimensional array

Page 62: Special Lecture: Visual Basic

7.1 Multidimensional Array declaration

• Multidimensional array variable is a special variable collecting many data by having two or more-dimension data locations.

• Example a 2-dim

Dim stack(3,4) as integer

Dim varname(dim1, dim2, .., dimn) as datatype

Page 63: Special Lecture: Visual Basic

7.2 Dev1: 2x2 Matrix• Calculate deterministic of a 2x2 matrix

Page 64: Special Lecture: Visual Basic

7.3 Dev2: Calculate |d| from a 2x2 matrix, by getting inputs from TextBox

tb00 tb01

tb10 tb11

btnCalc

Page 65: Special Lecture: Visual Basic

7.4 Dev3: Calculate |d| from a 3x3 matrix, by getting inputs from TextBox

Let’s student modify the dev2 to have a functionCalculating a 3x3 matrix.

Page 66: Special Lecture: Visual Basic

7.5 Splash Screen

• Splash screen is the first dialog showing before moving to the main program.

• There are usually property of the splash screen.• No title bar

• Automatic turn off (<5seconds)

• Show information of project

• After closing the form, it turn to display the main program.

Page 67: Special Lecture: Visual Basic

1.Create new form

2. Select picture component and placement

FormBorderStyle = noneStartPosition = CenterScreen

ControlBox = false

Page 68: Special Lecture: Visual Basic

7.6 Toolbox: Timer3.Select timer

component

4. Set 5000ms, Enable

Page 69: Special Lecture: Visual Basic

5. Set property of the project

6. Choose the splash form to be

the startup

Page 70: Special Lecture: Visual Basic

Function: .close()Usage for exit program

Function: .hide()Usage for turn off display

Function: .show()Usage for turn on display

7. Write the code at event tick of

timer1

Page 71: Special Lecture: Visual Basic

7.7 Dev4: Transpose Matrix• We had an algorithm of transpose matrix from

Internet and we are going to modify the code to VB.

Page 72: Special Lecture: Visual Basic
Page 73: Special Lecture: Visual Basic

Where is the bug from this program?

Page 74: Special Lecture: Visual Basic

Quiz5