special lecture: visual basic

Post on 19-Feb-2022

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

1. What is 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)

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/

2. Variable & Calculation

2.1 Variables

2.2 Variables

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.

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

2.5 Arithmetic operators

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

3. Application Development1

3.1 Button: Hello world

3.2 Convert Radius to Area

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

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

4. Conditions

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

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

4.3 Textbox component

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

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”, “ ”)

4.5 Test: String to value

4.6 Test error: convert str to double!!

4.6 Dev: Grade Display

4.7 Dev: Grade display

btnDisplay

lbGrade

tbScore

4.8 Debugging and step running

3 Enter tbScore

4.9 Select Case

Select case

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

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

4.10 Dev: Grade_v2

4.11 Dev: Feet to Meter

5. Loops

5.1 Loops

• Visual basic has three loop types

While Until

For

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

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

5.4 Loop Count Up & Debugging

btnWhileLoop

btnUntilLoop

• Open immediate windows• CTRL+G

5.5 Immediate windows

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)

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()

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

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

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

5.10 Listbox

ListBox

Manual Item

add/remove

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

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)

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

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

6. Array

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

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

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

6.4 One-dimension array

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

“Changmai”,

“Chonburi”,

“Rayong”}

Bangkok Changmai Chonburi Rayong

strC

ity(

0)

label.text = strCity(0)

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

‘//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

6.7 Test For Each

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

6.9 Dev: Average value calculator

lbValue

tbInputbtnAdd

btnAvg

6.9 Dev: Average value calculator

6.9 Dev: Sort value in the listbox

7. Multi-dimensional array

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

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

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

tb00 tb01

tb10 tb11

btnCalc

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.

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.

1.Create new form

2. Select picture component and placement

FormBorderStyle = noneStartPosition = CenterScreen

ControlBox = false

7.6 Toolbox: Timer3.Select timer

component

4. Set 5000ms, Enable

5. Set property of the project

6. Choose the splash form to be

the startup

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

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

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

Where is the bug from this program?

Quiz5

top related