Lab 6b
Command
Button, Label, Textbox, Scroll Bar (Horizontal and Vertical), and list box
We know how to use the
command button, a text box, a label, an option button, a list box, and we know
how to change them properties on these to be named and look like we want them
to.
Buttons, Text Boxes, Labels, Scroll Bar,
Option Button, Check Box, List Boxes.
Label, text box, command button, list box,
check box and option button
Buttons, textboxes, labels, text fields, and captions or file names.
TextBox,
CommandButton, OptionButton, ListBox, VscrollBar, Label, CheckBox, HScrollBar,
PictureBox, Pointer
LABEL, TEXTBOX, COMBO BOX, LIST BOX, VSCROLLBAR,
HSCROLLBAR, FRAME, CHECKBOX, OPTION BOX, DRIVESCROLL BAR, COMMANDBUTTON, LINE,
DATA, FILELISTBOX, IMAGE, OLE, SHAPE, TIMER. ßnot so, these controls are in the toolbox, but we don’t know
how to use them all yet.
Forces variables to be explicitly declared.
The option explicit
tells the computer that the statement is forced to be explicitly declared.
Forces variables to be explicity declared.
The option explicit forces the variables to
be explicitly declared. It does not let you name the same variable more then
once.
Forces explicit declarations or
variable declarations
To force variable declarations if none are made.
THAT A VARIBLE WILL BE DECLARED EXPLICTLY.
Declares that these data areas
apply to all sub routines
It sets a variable as
something, such as Dim sum as Integer, sets the variable (any valid identifier)
as an integer.
Declares a variable and type, such as
integer. ßmore than this, what’s
significant about position
Each variable that has been assigned, has
that value throughout the program, not just the subroutine.
It means that it has variables outside of the main subroutine. ßthere is no such thing as a main subroutine as far as we
know. This was explained on page 145.
That
it is in the general declaration (a global variable) and available to every
event procedure.
THAT
A VARIBLE IS BEING DECLARED.ßmore than this, where
The name of the command button or other control
After Private Sub
comes the name of the item (i.e command button, list box, etc) that you are programming.
Name of the designated field and the action
that activates it.ßaction
is for event procedures only
. The name of the control
Either a command1 which is unnamed button, click or form1
which is also another unnamed form1ßI know of no occasion so far in the text where form1 occurred
on the Private Sub line, please point it out to me. There must be some justification for making this assertion!
The name of the control / command (i.e., cmdEnter_Click())
THE OBJECT OF FOCUS, SUCH AS LABEL, TEXT,
COMMAND BUTTONßabsolutely
not—these are controls, not subroutine names.
X=1
DIM x as
integer
CmdDisplay_Clickß this does none of
the above and is not a statement.
Dim message as String
(data items)
mAccessCode = txtDisplay.Text (change
values)
Select Case mAccessCode
(flow control statment)
Local
names: Text1.Text = “Hello”ßno different than the next
statement—not a declaration
Changing
values: Text1.Text = Text1.Text + 1
Flow
Control: If x > 5 Then, Print x, Else Print y, End If
Declaration Dim
A As Integer
Change
value x = a + 1
Flow
control If a > 2 Then
Print a
End If
: In the first item or the data items, an example would be a product
of <=1000, for a statement that changes the value an example would be the
product =5 * product, and for a flow control statement it is what makes it go
in the order that it does. An example might be a loop and how many times that
it loops or a do until. ß I
specifically asked for you to write three example VB statements. You have written no statements. A statement starts with a keyword.
Declaration Statement: Dim message as String
Change
Values: x
= x + 1
Flow
Control Statements: If num1 <= num2 Then
LblDisplay4.Caption
= num1
ElseIf
num1 > num2 Then
LblDisplay4.Caption = num2
IF/THEN,
IF/THEN/ELSE, DO UNTIL/LOOP ßthese are keywords. NOT example statements
An expression is a function that interprets with operators
like AND and OR.ß
Boolean expressions use the operators AND and OR, String and numeric
expressions may not.
Ex: If not
5<6 then falseßafter
the then comes a statement, this is an error.
Do: x=5:Loop while 5<6ß this is invalid, needs a
LOOP
If x=5 then trueßerror else x<5
If/Then expression -
if a certain variable is true then a certain an expression is displayed if it
is false then another expression is displayed.
While/Wend expressionßexpression not defined or illustrated!
Expression:
combination of data names with operators.
If x
> 5 Then
ElseIf Grade >= 80 Thenßtwo Ifs are not different (even if elseis), use For, Case
Do Until product > 1000
An expression is a sequence of operations
and characters where the characters are data names and logical.
Example:
DIM A As integer
X = A + 1
If X > 5 Then
Print “Yipee”
End If
Do: LoopßLoop ends the loop(this is an empty loop—repeats nothing but
never ends (no way to get out) I hope
the IDE won’t let you write this.
Print “Yipee”
While X > 5ßmissing Wend
A string that sets up a command for a specified function. Ex. ßnot an adequate
definition. Please identify what the
specified function is below in each statement, and explain what string is
involved, and define what “sets up a command” means.
Yellow = command1.caption , ßan assignment statement moving text from caption to
Yellow. A variable name does not make a
very explanatory expression
Print false, ßprint statements often have
expressions, but the constant False is not a very interesting expression
end, ßincomplete (End Sub or End
Function) No expression in this
statement
text1.text= TextßLet statement, Text, perhaps
a variable name, is not a very interesting expression
Expressions are a sequence of operators and character strings, which is a
combination of data names with operators.
The program evaluates these expressions.
Examples: 1. Dim X as Integer
X
= 1 to 100 Step 2ßtwo
statements, where is the expression?
2. If grade >= 50 Then
LblState.Caption
= “Passed”
3. Do While Product <= 1000
Product
= Product * 2
EXPRESSION- STATES THE PURPOSE OF THE
KEYWORDS OR THE STATEMENT. ß has little to do with expressions, which are formulas that
signify values. IF/THEN, IF/THEN/ELSE, FOR/NEXT.ßnot example statements.
If you true to combine two different type of values and the compiler converts them
differently than what you think. Ex: if you want to combine a + 6 the compiler will
convert both to numeric expressions and if you expect a letter it will not be what
you expect.
A wrong equation or sequence that either calculates
the wrong value or has the wrong value in memory. Either of these mistakes will produce an errored value.
The programmer has the order of operations out of
sequence according to the precedence rules.
The programmer may have used fixed values
which cannot be change by the programmer ßdo you mean he wrote 5 when he meant 3? What else has “fixed values” and what does
this say about the difference between how the programmer wrote the expression
and the computer evaluated it?
The programmer forgets the precedence in which the compiler handles
the data. For instance, incorrect use
of parenthesis or forgetting the order in which arithmetic operations are
performed.
THE
PROGRAMMER GETS CONFUSED ABOUT WHAT IS GOING ON IN THE PROGRAM DUE TO THE
PRECEDNCE OF THE CODE.
a. –2 + 7.4 INTEGER
b. 7.4 \ 5 INTEGER
c. 4 mod 2 + y SINGLE
d. 3 ^ -2 SINGLE
e. 5>x BOOLEAN
f. “+” & x STRING
g. Text1.Text + 1 INTEGER
h. (5>x) OR (y<x^10) BOOLEAN
The statement IF an
For can contain ranges.ßthe If cannot have a range
String$(17, “@”) and Format$(amount, “currency”)ßneither illustrate a range
For x = 1 to 100 Step 5 ……..
If x
>= 5000 Then ……ßthis
is NOT a range expression
The for and the case are statements that denote ranges of values, For row = 1 to 10 , Case 165 to
245
For x = 1 to 100 Step 2
If x >= 1000 Then …
SELECT CASE AND FOR/NEXT
a) Syntax error
b) Syntax error
c) Logic error
Syntax
error
)
syntax b) syntax
c) logic d)
logic e) logic
f) syntax
a) Syntax error because you didn’t use Then
with the If statement
b) Syntax error because Loop is
missing to complete the statement
c) Logic error because y = y + 1
should be in the loop (before Wend)
d)
Logic error because it would loop continuously (it should be z = z –1)
For x = 573 To 500 Step x - 5
Print x;
Next x
Dim x As Integer
For x =
573 to 500 Step –5
Print x
Next
x
Dim x As Integer
For x = 573 To 500 Step -5
Print x
Next x
For x = 573 To 500 Step x - 5
Print x;
Next x
Dim x as Integer
For x = 573 to 500 Step -5
Print x
Next x
For X = 573 To 500 Step -5
Print
X;
Rem ENDßdoesn’t hurt but then it
doesn’t help either!
Next X
(1) If pcm starts out not equal to q it will not enter
the loop loops 0 times
(2)If pcm starts out equal to q it enters the loop is
changed in the loop to a different value, we
have an infinite loop
(3) If the txtInput is changed in the program to q it
loops once
If pcm does not = q, then the loop will not
be executed.
If pcm = q, the loop will execute an infinite number
of times.UNLESS case below!
If TextInput.Text does not = q,
the loop will execute only once.
pcm is not equal to q –> zero loops
pcm is equal to q & txtInput.Text
= q –> infinite loop
pcm is equal to q & txtInput.Text not
= q –> loops 1 time
It will loop three times Once through the pcm, then through the
qfactor?? and finally into the txtinput.text.ßNOT!
pcm not equal to q, do the loop 0 times
If pcm starts out equal
to q, it gets changed in the loop to q, and you have an infinite loop.
If pcm starts out equal
to q, and textInput is not q, then it loops once.
So,
enter the loop 0 times, once, or infinite.
THE THREE CASES ARE 0 TO 1, INDEFINTE, 0ßno conditions stated!
Once
THE CASES
WOULD BE GO THROUGH ONCE AND INDEFINTELY AFTER GOING THROUGH LOOP ONCE.ßsounds like two different
answers and the computer will only do one of them.
for exercise 4.12d: assume display has the value “135”. Give an initial value for n such that
a. nothing will be printed 136
b. the loop will print exactly 8
times .000164ßtoo big, prints only 7 times
c. the loop will (try to) print an
infinite number of times. 0
a)
136 b) .0001 c) 0
n > 136
(b) n = 0.00003
(c) n = 0
nothing will be printed 200
b. the loop will print exactly 8
times .000164
c. the loop will (try to) print an
infinite number of times. 0
anything
greater than 135
b.
0.00003
c.
If n = 0
nothing will be printed A NUMBER GREATER THAN
DISPLAY(135)
b. the loop will print exactly 8 times
N=N^8ß I don’t know what value this
is.
c. the loop will (try to) print an
infinite number of times. N=0
The
caption bar on the GUI interface ßno such thing (do you mean window?)
The caption of the form
GUI interface window
(title bar of run window) (form 1 changes to Value is 385)
Value isßwhat
object is this (objects have properties, Caption is one such property)
The GUI interface window (title bar of
run window)
COMMANDBUTTON1ßcommon misunderstanding!
a) Dim x As Integer, y As Integer, Mysteryvalue as Integer
For x
= 1 to 10 Step 1
y = x ^ 2
Print y
Mysteryvalue = Mysteryvalue + y
Next x
Caption = “Value is “ & Mysteryvalue
b) The square of x.
Which is 1, 4, 9, 16, 25, 36, 49, 64, 81, 100.
The
<data item> is repeatedly incremented by y as the loop continues.
mysteryValue = 0
For x = 1 To 10 Step 1
y
= x ^ 2
Print y
mysteryValue
= mysteryValue + y
Next x
Caption = "value is " & mysteryValue
For x = 1 To 10 Step x ^ 2
y = x ^ 2
Print y
Mysteryvalue = Mysteryvalue + y
Next x
b. The value of Y
is dependant on the value x which is the result of the square of x
c. It continues to increase by one
unless otherwise specified.ßhow can it be otherwise specified? it is what it is, the question is what does it accomplish!
For x = 1 to 10 Step 1
y = x ^ 2
Print y
MysteryValue = MysteryValue + y
Next x
Caption
= “value is “ & MysteryValue
(b)
1 It
assigns the value of x-squared to y, and prints y.
4
9
16
25
36
49
64
81
100
( c ) The value will be getting larger or smaller based on “y,” because
the Text1.Text is being incremented each time through the loop by “y.”
The flow of control statements in
Basic are ALL block statements, that is, they are comprised, like subroutines,
of a header statement, an END statement, and have body statements in between. List all of the different forms of the
header statements and for each the form of the END statement.
If/Then:
End If, If/Then/Else: End If,
Select Case: End Select, While:
Wend, Do While: Loop,
For: Next.
If ……Then……<Else If……Else……>End If < > is not part of header or end
Do
Until …… Loop
Do
While …… Loop
Do ……
Loop While (Until)
While …
… Wend
For
……Next
. Select case-caseßnot end statement, <else-elseif>, <sub-endsub>,
if-endif, for-next, while-wend, do-while-loop-until??,
<let-until>ß< > indicates invalid
pairs
SELECT CASE …< CASE …> END SELECT
IF
….THEN <…. ELSE IF … ELSE …> END IF
OR can be just IF…THEN<…ELSE….>END IF
OR can be just IF…THEN if a single statementßMUST have End If
DO
UNTIL… LOOP
DO
WHILE … LOOP
PRIVATE
SUB … END SUBßNot
flow control
WHILE …
WEND
FOR…NEXT
DO …
LOOP UNTIL
DO … LOOP WHILE
If you know how to write
valid statements that achieve your purpose, you are able to read valid
statements and predict what they will accomplish. Do that for exercises 5.10, 5.11, 5.12, and 5.13, showing the
output of each below.
5.10:
Prints
& in as many rows and columns as indicated by input windows
5.11:
Prints
6 rows of “*” that is 5 columns long
5.12:
3, 5,
8, 11, 11
5.13:
5, 11,
18, 26, 30, 35, 41, 44, 48, 50, 50*
5.10
Print “&” x
number of times across and y number of times down
If you
entered 3 for x and 2 for y you would get:
&&&
&&&
5.11
i produces 2 loops
j
produces 3 loops
k
prints 4 *
This
produces 4 *’s in 6 rows
i j k Print
1 1 1 à *
2 à *
3 à *
4 à *
2 1 à *
2 à *
3 à *
4 à *
3 1 à *
2 à *
3 à *
4 à *
2 1 1 à *
2 à *
3 à *
4 à *
2 1 à *
2 à *
3 à *
4 à *
3 1 à *
2 à *
3 à *
4 à *
5.12 Outside For Loop starts at 0 increments to
4 (step 1)
Inside
loop says while c < p, go through select sequence.
First
time through, p = 0, c = 0, so it does not do while loop
So, it
increment p to 1, gets to while statement and c = 0, so it enters the while
loop.
Select
Case (p + c – 1) will equal 1 + 0 – 1 = 0, so it uses the first case, and m = 1
now m = 1, and
P = 1,
so case 1,2,3 m = m + 2 is selected and m = 3; then you fall out of select
print “ m” which is “3”, then increments c by 1, making it 1, and back up to
top for loop, make it 2. This continues until the final printout is:
3 5 8 11
11
The
last 11 appears on its own line, because of the last Print and Print m
statements, which are outside the outside For loop
5.13 e b t w list box
4 0 5 5 à
1 6 11 à
2 7 18 à
3 8 26 à
3 0 4 30 à
1 5 35 à
2 6 41 à
2 0 3 44 à
1 4 48 à
1 0 2 50 à
0 - - - à 50*
5.10It makes the same number of columns and rows that
you have requested 5.11 it does not run because of a syntax error 5.12 it runs
the amount in 2 then 3 then 3 again or in other words a pattern of 3 5 8 11
then repeats 5.13 it won’t run because it has a syntax error for the Next
variable, it won’t be able to use it as a cyclic variable.ßthey all run without
syntax errors.
5.10
This is a table format (rows/columns).
The inside loop prints the rows, and the inside loop sets the values for
the number of & on a column (x),
and the outer loops set the number of rows (y). For example, if you entered 2 for x and 3 for y, it would print:
&&
&&
&&
5.11
3 loops.
The program goes through the first 2 loops, and then runs the k loop 4
times, producing “****”
Once it
goes through the k loop 4 times, it goes back up to the j loop, which
increments by 1, making the value 2, and it goes back through the k loops
another 4 times creating another
“****”
Then it
falls out of the k loop again and increments the j loop to 3, goes back through
k producing another “****.”
Then,
it falls out of the k loop, falls out of the j loop and increments the “i” loop
to 2, and then goes back through the j loop sequence, creating 3 more rows
of “****.”
The final output is:
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
5.12 Outside For loop starts at 0 increments to 4 (step 1)
Inside
loop says while c < p, go through Select sequence.
First
time through, p = 0, c = 0, so it does not do while loop.
So, it
increments p to 1, gets to while statement and c=0, so it enters the while
loop.
Select
Case (p + c – 1) will equal 1 + 0 – 1 =
0, so it uses the first case, and m = 1, now m = 1, and
P = 1, so Case 1, 2, 3 m = m + 2 is selected and m = 3; then you fall out of select print “m” which is “3,” then increments c by 1, making it 1, and back up to top for loop, making it 2. This continues until the final printout is:
3 5 8 11
11
The
last 11 appears on its own line, because of the last Print and Print m
statements, which are outside the outside For loop.
5.13
It prints the numbers “5, 11, 18, 26, 30, 35,
41, 44, 48, 50, and 50*
E
values start at 4 and increment by –1 until they reach 0.
For
loop inside starts with b=0 and goes to (e-1)
The
first value of t is 5.
e b t w list
box
4 0 5 5 à 5
1 6 11 à 11
2 7 18 à 18
3 8 26 à 26
3 0 4 30 à 30
1
5 35 à 35
2 6 41 à 41
2 0 3 48 à 44
1
4 48 à 48
1 0 2 50 à 50
0 - - - à 50*
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
BeginProperty Font
Name = "Courier"
Size = 9.75
Charset =
0
Weight = 700
Underline = 0
'False
Italic = 0
'False
Strikethrough = 0
'False
EndProperty
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth =
4680
StartUpPosition =
3 'Windows Default
Begin VB.CommandButton Command2
Caption = "D"
BeginProperty Font
Name
= "MS Sans Serif"
Size
= 13.5
Charset
= 0
Weight
= 700
Underline
= 0 'False
Italic
= 0 'False
Strikethrough
= 0 'False
EndProperty
Height = 615
Left = 2760
TabIndex = 1
Top = 2040
Width = 735
End
Begin VB.CommandButton Command1
Caption = "C"
BeginProperty Font
Name
= "MS Sans Serif"
Size
= 13.5
Charset
= 0
Weight
= 700
Underline
= 0 'False
Italic
= 0 'False
Strikethrough
= 0 'False
EndProperty
Height = 615
Left = 720
TabIndex = 0
Top = 2040
Width = 735
End
End
Attribute VB_Name =
"Form1"
Attribute VB_GlobalNameSpace =
False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId =
True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_Click()
Dim row As Integer
Dim col As Integer
Cls
For row = 1 To 10
Print Space$(row - 1);
For col = 10 To row Step -1
Print "*";
Next col
Print
Next row
End Sub
Private Sub Command2_Click()
Dim row As Integer
Dim col As Integer
Cls
For row = 1 To 10 Step 1
Print Space$(10 - row);
For col = 1 To row
Print "*";
Next col
Print
Next row
End
Sub
5.16
( C
)
STARZ2.FRM:
VERSION
5.00
Begin
VB.Form Starz2
Caption =
"Starz2"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
BeginProperty Font
Name =
"Courier New"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form2"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Starz2
Caption =
"Starz2"
BeginProperty Font
Name =
"Courier New"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough =
0 'False
EndProperty
Height = 495
Left = 1800
TabIndex = 0
Top = 1320
Width = 1215
End
End
Attribute
VB_Name = "Starz2"
Attribute
VB_GlobalNameSpace = False
Attribute
VB_Creatable = False
Attribute
VB_PredeclaredId = True
Attribute
VB_Exposed = False
Private
Sub Starz2_Click()
Dim nStar As Integer
Dim row As Integer
For row = 10 To 1 Step -1
Print Space$(10 - row);
For nStar = 1 To row Step 1
Print "*";
Next nStar
Print
Next row
End Sub
( D
)
Begin
VB.Form Starz
Caption =
"Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
BeginProperty Font
Name =
"Courier New"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Starz
Caption =
"Starz"
BeginProperty Font
Name =
"Courier New"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0
'False
EndProperty
Height = 495
Left = 1800
TabIndex = 0
Top = 1320
Width = 1215
End
End
Attribute
VB_Name = "Starz"
Attribute
VB_GlobalNameSpace = False
Attribute
VB_Creatable = False
Attribute
VB_PredeclaredId = True
Attribute
VB_Exposed = False
Private
Sub Starz_Click()
Dim nStar As Integer
Dim row As Integer
For row = 1 To 10 Step 1
Print Space$(10 - row);
For nStar = 1 To row Step 1
Print "*";
Next nStar
Print
Next row
End Sub
Option Explicit
Private Sub Command1_Click()
Dim nstar As Integer
Dim row As Integer
Dim sp As Integer
Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim e As String
Dim maxNum As Integer
Dim valid As Boolean
Cls
a = "Enter a positive odd
number up to 25:"
b = "Enter an odd number
between 1 and 25:"
c = "Enter a positive odd
number less than 26:"
d = "Enter a positive
number up to 25 but not divisible by 2:"
e = "Enter a whole odd
number up to 25:"
valid = False
Do
maxNum = InputBox(a)
If maxNum >= 25 Then
InputBox (b)
Else
If maxNum <= 0 Then
InputBox (c)
Else
If maxNum Mod 2 = 0 Then
InputBox (c)
Else
Rem Data Okay
valid = True
End If
End If
End If
Loop Until valid
sp = maxNum \ 2
For row = 1 To maxNum Step 2
Print Space$(sp);
sp = sp - 1
For nstar = 1 To row
Print "*";
Next nstar
Print
Next row
sp = 1
For row = maxNum - 2 To 1 Step
-2
Print Space$(sp);
sp = sp + 1
For nstar = 1 To row Step 1
Print "*";
Next nstar
Print
Next row
End Sub
Private Sub Command2_Click()
End Sub
Private Sub Command1_Click()
Dim nstar As Integer
Dim row As Integer
Dim sp As Integer
Dim a As String
Dim valid As Boolean
Dim maxNum As Single
a = "Enter a positive odd
number up to 25:"
valid = False
Do
maxNum = InputBox(a)
If maxNum >= 25 Then
a = "Enter an odd number between 1 and 25:"
Else
If maxNum <= 0 Then
a = "Enter a positive odd number greater then 0
and less than 26:"
Else
If CInt(maxNum) Mod 2 = 0 Then
a = "Enter a positive number up to 25 but not
divisible by 2:"
Else
If
maxNum - CInt(maxNum) <> 0 Then
a = "Enter a whole odd number up to
25:"
Else
Rem Data Okay
valid = True
End If
End If
End If
End If
Loop Until valid
sp = maxNum \ 2
For row = 1 To maxNum Step 2
Print Space$(sp);
sp = sp - 1
For nstar = 1 To row
Print "*";
Next nstar
Print
Next row
sp = 1
For row = maxNum - 2 To 1 Step
-2
Print Space$(sp);
sp = sp + 1
For nstar = 1 To row Step 1
Print "*";
Next nstar
Print
Next row
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Command3_Click()
Cls
End Sub