Vous êtes sur la page 1sur 6

Dim A, B, C As Double

A=2
B=3
C = (A+B) + (A-B)
Textbox1.Text = C

To clear listbox:
Listbox1.Items.Clear()

Print "hello" in textbox:


Textbox1.Text = "hello"

String data type:


sequence of characters that is treated as a single item
variable for text AND numbers

How to declare:
Dim str As String
str = "Steve Martin"
or str = "814-111-2345"

How to print data using text or list:


Textbox1. Text = str
Listbox1.Items.Add (str)

How to read data using a textbox:


Input using a string variable
reads data from txtInput
data is saved in string variable
use string variable to set the text of txtOutput
Ex. Dim str As String
str = txtInput. Text
txtOutput.Text = str
OR
txtOutput.Text = txtInput.Text

Numbers entered in a textbox (123)


VB treats all data in the textbox as a string variable

You need to convert the data to a numeric value before doing any calculations

1. CDbl- convert to double


2. Cint- convert to integer
Dim str As String
str = txtInput.Text <-- declared
Dim A As Double <--- read data
A = CDbl(str) + 3
txtOutput.Text = A

*Cstr --> convert to string


(str (123)) --> "123"

txtOutput.Text = A
VB adds Cstr for you
txtOutput.Text = Cstr (A)

String Concatenation
Use &
str1 & str2
Ex. Dim str1, str2, str3 As String
str1 = txt1.Text
str2 = txt2.Text
str3 = str1 & str2
txt3.Text = str3

Lengths- counts the number of characters


Dim S As String
S = "hello"
txtbox.Text = S.ToUpper --> HELLO
OR
S = "UPJ"
txtbox.Text = S.ToLower --> upj

Trim- removes all spaces from front and back of string value
Ex. S = " Hello World "
txtbox.Text = S.Trim --> "Hello World"

Substring- get a part of string values


S = "Hello World"
S.Substring (m [beginning position], n [# of characters to extract])
S.Substring (0, 2) --> from position #0, extract 2 letters

Ex. S = "Steve Martin"


txtbox.Text = substring (0, 5) --> "Steve"
Or
txtbox.Text = substring (6, 6) --> "Martin"

Index of (S2) --> checks if "S2" exists in S1 and returns position of S2


Ex. S1 = "Hello"
1. S1.Indexof ("e") --> returns 1
2. S1.Index of ("l") --> returns 2
3. S1. Indexof ("ne") --> returns 0
If there is no match, it returns -1
Ex. S1.Indexof ("x")

S1 = "Steve Martin"
"Michael Goode"
Dim sp As Integer
name = txtName.Text
sp = name.Indexof (" ")
Dim len As Integer
len = name.lengths
LL= len - (sp + 1) --> the # of characters in last name
name.substring ((sp + 1), LL)
Empty string ("")
txtbox.Text = " "
OR
txtbox.Clear ( )

How to read data from a file (.txt)


Understand "directory path"
C:\Windows\System32\hello.txt

C:\Users\sim.PITT\mydocuments\hello.txt
**If the application file and the text file are placed in the same directory, you don't need to use
the "directory path"

Length- # of characters in a string variable


To Upper (Lower)
Trim (m, n)
Substring (0, 0)
Indexof

Reading data from a text file


1. directory path to the file
2. file name

Filename in Window
In windows, file name has 2 parts
filename. filetype ---> mp3, doc

(:|users|sim.PITT| |hello.txt)
**When executable and data are located in the same folder, "directory path" is not
required
Just use the file name
put the data file (hello.txt) in bin/Debug

How to read data using VB:


1. Declare variable for file reading Dim Sr As IO.streamreader
2. Open the file.Sr = 10.file.OpenText ("hello.txt")
3. Read data (line by line) Str = Sr.Readline filename
4. Close the file using Sr.Close( )

**Use CDbl or CInt when you read numbers stored in a text file
Ex. a = CDbl ("message, 0, txt6)

Message Dialog Box- A message box is a special dialog box used to display a piece of
information to the user. The user cannot type anything in the dialog box.

Input Dialog Box- Displays a prompt in a dialog box, waits for the user to input text or click a
button, and then returns a string containing the contents of the text box

Creating a zone (virtual table) in a listbox:


Dim zone As String
zone = "{0, 5} {1, 3}
1st = columb number
2nd = max number of characers

zone = "{0, 10} {1, 10} {2, 10}"


lstbox.Items.Add (next line)
String.Format (zone, "a", "b", "c")
**Must add 3 items --> empty: " "

Font Style
1. monospace- each character uses the same space <Lucida Console>
2. proportional- one character uses more space <Courier New>
**Must use monospace font with zone

Format functions for $, %, and numbers


Format.Currency (1000) --> $1,000.00
Adds $ , .00

FormatPercent (0.5) --> 50.00%


Adds .00, % and convert to percentage value

FormatNumber (3 [data to be displayed], 2 [# of decimal places])


lstbox.Items.Add (Format...)

Ex. Dim zone As String


zone = "{0, 10} {1, 10}"
listbox1.Items.Add (String. Format (zone, FormatCurrency (1000), FormatPercent(.01)))
zone = "{0, 10: C} {1, 10:P}"
listbox1.Items.Add(String.Format (zone, 10, 0.1))

left and right justification:


negative # = left
positive # = right

Rational and Logical Operations


rational (a < 5) true or false
logical (Not (b = 5))

Ex. Not (("B" = "b") OR ("Big" < "big")) --> False

If-Block
If condition Then
action 1
Else,
action 2
End If

Ex. If (a < 5) Then


print "hi"
End If

Es. If (a < 5) Then


print "hi" --> lstbox.Items.Add ("hi")
Else
print "bye"
End If

If condition Then
action 1
Else if condition 2 # of conditions: n-1
action 2 # of actions: n
Else if condition 3
action 3
Else
action n

Ex. Letter Grading


If score >= 90 Then
print A
Else If score >= 80 Then
print B
Else If score >= 70
print C
Else
print F
End If

Nested If Block- an if block inside another if block


If condition Then
If (a < 5) Then
print A
End If
Else
action 2
End If

Ex. Credit Score


If credit-score > 500 Then
If age > 30 Then
print "10%"
End If
Else
print "15% interest"
End If

Equivalent If Block without Nested If Block


If (credit-score >500) And (age > 30) Then
print "10%"
Else
print "15%"
End If

Ex. Dim tip, amount As Double


tip = CDbl (txtinput.Text) * .15
If tip < 1 Then
tip = 1
Else If
print tip
End If

Select Case Block


Select Input
case1
print "red"
case 2
print "blue"
case 3
print "yellow
case else
print "orange
End Select

Vous aimerez peut-être aussi