3/9/2004 ppcc - introduction to vb6 copyright ©2004, tore bostrup 1 introduction to vb6 week 2
Post on 25-Dec-2015
217 Views
Preview:
TRANSCRIPT
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright ©2004, Tore BostrupCopyright ©2004, Tore Bostrup 11
Introduction to VB6Introduction to VB6
Week 2Week 2
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 22
Review: The VB6 IDEReview: The VB6 IDE
The Forms DesignerThe Forms Designer The Toolbox window contains the The Toolbox window contains the
controls available for usecontrols available for use Double click an icon in the Toolbox orDouble click an icon in the Toolbox or Click an icon in the Toolbox and click and Click an icon in the Toolbox and click and
drag the sizing handles on the formdrag the sizing handles on the form Release the mouse buttonRelease the mouse button Resize control by selectingResize control by selecting
and click & drag sizing handlesand click & drag sizing handles
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 33
Review: The First App – Review: The First App – Hello WorldHello World
Open VB6Open VB6 Create a New Standard EXE projectCreate a New Standard EXE project Double-click the formDouble-click the form Enter the following code in Enter the following code in
Sub Form_Load()Sub Form_Load() MsgBox "Hello World"MsgBox "Hello World"
Press F5 or click the Press F5 or click the button button
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 44
Review: Hello World Review: Hello World VariationsVariations
Using constants and expressionsUsing constants and expressions Sub Form_Load()Sub Form_Load()
Const WORLD = "World"Const WORLD = "World" MsgBox "Hello " & WORLDMsgBox "Hello " & WORLD
End SubEnd Sub
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 55
Review: Hello World Review: Hello World VariationsVariations
Using Controls and VariablesUsing Controls and Variables Place a CommandButton on the formPlace a CommandButton on the form Add a Label, TextBox, and another Label to the Add a Label, TextBox, and another Label to the
formform Make the Caption for the first label "Name"Make the Caption for the first label "Name"
Double-click the ButtonDouble-click the Button Enter the following code in Enter the following code in
Sub CommandButton1_Click():Sub CommandButton1_Click(): Dim sHello as StringDim sHello as String sHello = "Hello " & Text1.TextsHello = "Hello " & Text1.Text Label2.Caption = sHelloLabel2.Caption = sHello
Press F5 to run, enter a name and click the buttonPress F5 to run, enter a name and click the button
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 66
Review: Hello World Review: Hello World VariationsVariations
This is what your form and code This is what your form and code should look like:should look like:
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 77
Hello World VariationsHello World Variations
What would make the last Hello What would make the last Hello World better?World better?
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 88
Hello World VariationsHello World Variations
What would make the last Hello What would make the last Hello World better?World better? Set the Text property for Text1 to an Set the Text property for Text1 to an
empty stringempty string Set the Caption property of the Set the Caption property of the
CommandButton to "Hello"CommandButton to "Hello"
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 99
Approximations of PIApproximations of PI
Sometimes it is useful to be able to Sometimes it is useful to be able to approximate the value of PI by using a approximate the value of PI by using a fraction.fraction. See See http://www.math.hmc.edu/funfacts/ffiles/10004.5.http://www.math.hmc.edu/funfacts/ffiles/10004.5.shtmlshtml
22 / 7 is a common simple fraction that 22 / 7 is a common simple fraction that produces PI with a result correct to 2 decimals produces PI with a result correct to 2 decimals (3.14)(3.14)
333 / 106 is good to 5 decimal places333 / 106 is good to 5 decimal places 355 / 113 is good for 6 decimal places355 / 113 is good for 6 decimal places To get a better result, we must use values To get a better result, we must use values
greater than 30,000.greater than 30,000.
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1010
Approximations of PIApproximations of PI
Create a new Standard EXE projectCreate a new Standard EXE project Place three TextBoxes, three Labels Place three TextBoxes, three Labels
and a CommandButton on the form:and a CommandButton on the form:
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1111
Approximations of PIApproximations of PI
Set the label Captions:Set the label Captions: Label1.Caption = "Dividend"Label1.Caption = "Dividend" Label2.Caption = "Divisor"Label2.Caption = "Divisor" Label3.Caption = "/"Label3.Caption = "/"
Clear the Text property of all the Clear the Text property of all the TextBoxes:TextBoxes:
Set the Caption for the Set the Caption for the CommandButtonCommandButton Command1.Caption = "PI ="Command1.Caption = "PI ="
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1212
Approximations of PIApproximations of PI
After setting these and other After setting these and other properties (Form Caption, other…), properties (Form Caption, other…), your form should look something like your form should look something like thisthis
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1313
Approximations of PIApproximations of PI
So let us add some code to the So let us add some code to the applicationapplication Double-click the button:Double-click the button:
Private Sub Command1_Click()Private Sub Command1_Click() Text3.Text = Text1.Text / Text2.TextText3.Text = Text1.Text / Text2.Text
Run the application (F5)Run the application (F5) Type 22 in the Dividend fieldType 22 in the Dividend field Type 7 in the Divisor fieldType 7 in the Divisor field Click on PI =Click on PI =
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1414
Behind the ScenesBehind the Scenes
What goes on behind the scenes in this What goes on behind the scenes in this simple statement?simple statement? A simple experiment: Change the line of code to:A simple experiment: Change the line of code to:
Text3.Text = Text1.Text Text3.Text = Text1.Text ++ Text2.Text Text2.Text Run the app (F5) and enter the same valuesRun the app (F5) and enter the same values You get 227!You get 227!
Because Text1.Text and Text2.Text are both Strings, the Because Text1.Text and Text2.Text are both Strings, the plus sign means CONCATENATE stringsplus sign means CONCATENATE strings
But with the original line of code, we got the But with the original line of code, we got the correct answer?correct answer?
Because VB is often (NOT ALWAYS) smart enough to know Because VB is often (NOT ALWAYS) smart enough to know that values must be converted to another type…that values must be converted to another type…
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1515
Behind the ScenesBehind the Scenes
Contd:Contd: But with the original line of code, we got the But with the original line of code, we got the
correct answer?correct answer? Because VB is often (NOT ALWAYS) smart enough to Because VB is often (NOT ALWAYS) smart enough to
know that values must be converted to another type…know that values must be converted to another type… REMEMBERREMEMBER
There is a difference between a string containing a There is a difference between a string containing a number and a numeric value. The same is true for number and a numeric value. The same is true for dates, etc.dates, etc.
Best PracticeBest Practice Perform Explicit Type ConversionsPerform Explicit Type Conversions
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1616
Approximations of PIApproximations of PI
Back to our application – perform explicit Back to our application – perform explicit conversions, etc:conversions, etc: Double-click the button:Double-click the button:
Private Sub Command1_Click()Private Sub Command1_Click() Dim iDividend as IntegerDim iDividend as Integer Dim iDivisor as IntegerDim iDivisor as Integer Dim dblPI as DoubleDim dblPI as Double iDividend = Cint(Text1.Text)iDividend = Cint(Text1.Text) iDivisor = Cint(Text2.Text)iDivisor = Cint(Text2.Text) dblPI = iDividend / iDivisordblPI = iDividend / iDivisor Text3.Text = dblPIText3.Text = dblPI
Or:Or: Text3.Text = CDbl(Text1.Text) / Cint(Text2.Text)Text3.Text = CDbl(Text1.Text) / Cint(Text2.Text)
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1717
Type Conversion FunctionsType Conversion Functions
Each function coerces an Each function coerces an expressionexpression to a to a specific specific data typedata type.. CByte(expression), CInt(expression), CLng(expression)CByte(expression), CInt(expression), CLng(expression) CSng(expression), CDbl(expression)CSng(expression), CDbl(expression) CDec(expression), CCur(expression)CDec(expression), CCur(expression) CDate(expression)CDate(expression) CBool(expression)CBool(expression) CStr(expression), CVar(expression)CStr(expression), CVar(expression)
The required The required expressionexpression argumentargument is any is any string expressionstring expression or or numeric expressionnumeric expression..
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1818
Type Conversion FunctionsType Conversion Functions
Numeric – returns integer valuesNumeric – returns integer values CByte(expression), CInt(expression), CLng(expression)CByte(expression), CInt(expression), CLng(expression)
Numeric – returns floating point valuesNumeric – returns floating point values CSng(expression), CDbl(expression)CSng(expression), CDbl(expression)
Numeric – returns fixed decimal point valuesNumeric – returns fixed decimal point values CDec(expression), CCur(expression)CDec(expression), CCur(expression)
DateDate CDate(expression)CDate(expression)
Boolean (True/False)Boolean (True/False) CBool(expression)CBool(expression)
OtherOther CStr(expression), CVar(expression)CStr(expression), CVar(expression)
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1919
Approximations of PIApproximations of PI
Try running the app again, but this Try running the app again, but this time don't type anything into the text time don't type anything into the text boxes:boxes:
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2020
Input ValidationInput Validation
This is an extremely common This is an extremely common problem with many applications:problem with many applications: Invalid input creates not only the wrong Invalid input creates not only the wrong
answer, but results in program errors!answer, but results in program errors! REMEMBERREMEMBER
ALWAYS validate input data!ALWAYS validate input data!
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2121
Input ValidationInput Validation
Let us add some validation code:Let us add some validation code: Double-click the buttonDouble-click the button
Add the following statements at the beginning ofAdd the following statements at the beginning ofPublic Sub Command1_Click() (after declarations):Public Sub Command1_Click() (after declarations):
If Trim$(Text1.Text) = "" ThenIf Trim$(Text1.Text) = "" Then MsgBox "You must specify a Dividend"MsgBox "You must specify a Dividend" Exit SubExit Sub
End IfEnd If If Trim$(Text2.Text) = "" ThenIf Trim$(Text2.Text) = "" Then
MsgBox "You must specify a Divisor"MsgBox "You must specify a Divisor" Exit SubExit Sub
End IfEnd If
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2222
Input ValidationInput Validation
Typical things to validateTypical things to validate Empty inputs (example "")Empty inputs (example "") Input that is only blank spaces (example Input that is only blank spaces (example
" ")" ") Input that will not convert to the correct Input that will not convert to the correct
data type (example "seven")data type (example "seven") Values that are not allowed (example: a Values that are not allowed (example: a
divisor of 0)divisor of 0)
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2323
Input ValidationInput Validation
Typical things to validateTypical things to validate Empty inputs and input that is only blank spaces Empty inputs and input that is only blank spaces
can be done in one testcan be done in one test The Trim$() function removes leading and trailing The Trim$() function removes leading and trailing
spacesspaces If Trim$(Text1.Text) = "" ThenIf Trim$(Text1.Text) = "" Then
Input that will not convert to the correct data typeInput that will not convert to the correct data type The IsNumeric() function checks if an expression can be The IsNumeric() function checks if an expression can be
evaluated as a Numeric valueevaluated as a Numeric value If Not IsNumeric(Text1.Text) ThenIf Not IsNumeric(Text1.Text) Then
Values that are not allowedValues that are not allowed If CInt(Text2.Text) = 0 thenIf CInt(Text2.Text) = 0 then
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2424
Input ValidationInput Validation
Real Input Validation codeReal Input Validation codeIf Trim$(Text1.Text) = "" Or Trim$(Text2.Text) = "" ThenIf Trim$(Text1.Text) = "" Or Trim$(Text2.Text) = "" Then
MsgBox "You must specify both Dividend and Divisor"MsgBox "You must specify both Dividend and Divisor"Exit SubExit Sub
ElseIf Not IsNumeric(Text1.Text) _ElseIf Not IsNumeric(Text1.Text) _ Or Not IsNumeric(Text2.Text) Then Or Not IsNumeric(Text2.Text) Then
MsgBox "Only Numeric values are allowed"MsgBox "Only Numeric values are allowed"Exit SubExit Sub
ElseElseIf CInt(Text2.Text) = 0 ThenIf CInt(Text2.Text) = 0 Then
MsgBox "You can't divide by 0"MsgBox "You can't divide by 0" Exit SubExit SubEnd IfEnd If
End IfEnd If
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2525
Accessing Controls on the Accessing Controls on the FormForm
In order to access a control on a In order to access a control on a form, we specify the name of the form, we specify the name of the control. Usually, we do this to access control. Usually, we do this to access one of the control's one of the control's propertiesproperties, so we , so we also specify the property following a also specify the property following a period:period: MsgBox "Hello " & Text1.TextMsgBox "Hello " & Text1.Text
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2626
Accessing Controls on the Accessing Controls on the FormForm
We can also use a fully qualified We can also use a fully qualified control name:control name: For instance Form1.Text1.TextFor instance Form1.Text1.Text The pseudo-variable Me is a reference to The pseudo-variable Me is a reference to
the form that the code belongs to:the form that the code belongs to: Me.Text1.TextMe.Text1.Text Typing Me. brings up all the available Typing Me. brings up all the available
properties and methods (procedures) for the properties and methods (procedures) for the form, including all the controls, allowing us to form, including all the controls, allowing us to type in a partial name or scroll through a list type in a partial name or scroll through a list of available selectionsof available selections
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2727
Properties, Methods, and Properties, Methods, and EventsEvents
We have already used both some We have already used both some Properties (Text1.Text, Properties (Text1.Text, Label1.Caption) Label1.Caption)
and some Events – or rather Event and some Events – or rather Event Procedures or Event Handlers Procedures or Event Handlers (Command1_Click(), Form1_Load())(Command1_Click(), Form1_Load())
But what are "Methods"?But what are "Methods"?
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2828
What are MethodsWhat are Methods
A Method is simply a procedure (Sub A Method is simply a procedure (Sub or Function) that exists in a form's or Function) that exists in a form's code (or in an object's code) that is code (or in an object's code) that is available* to be referenced.available* to be referenced.
* available to other code modules, objects, or * available to other code modules, objects, or applicationsapplications
3/9/20043/9/2004PPCC - Introduction to VB6PPCC - Introduction to VB6
Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2929
Form and Control PropertiesForm and Control Properties
Forms and Controls have several Forms and Controls have several Properties - most of which can be Properties - most of which can be accessed and/or changed both during accessed and/or changed both during design and in code.design and in code.
Many form and control properties Many form and control properties describe visual aspects of the describe visual aspects of the form/controlform/control For example, a Label’s Caption property For example, a Label’s Caption property
determines what text it displaysdetermines what text it displays
top related