Presentation is loading. Please wait.

Presentation is loading. Please wait.

3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.

Similar presentations


Presentation on theme: "3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2."— Presentation transcript:

1 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2

2 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 2 Review: The VB6 IDE The Forms Designer The Forms Designer The Toolbox window contains the controls available for use The Toolbox window contains the controls available for use Double click an icon in the Toolbox or Double click an icon in the Toolbox or Click an icon in the Toolbox and click and drag the sizing handles on the form Click an icon in the Toolbox and click and drag the sizing handles on the form Release the mouse button Release the mouse button Resize control by selecting and click & drag sizing handles Resize control by selecting and click & drag sizing handles

3 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 3 Review: The First App – Hello World Open VB6 Open VB6 Create a New Standard EXE project Create a New Standard EXE project Double-click the form Double-click the form Enter the following code in Sub Form_Load() Enter the following code in Sub Form_Load() MsgBox "Hello World" MsgBox "Hello World" Press F5 or click the  button Press F5 or click the  button

4 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 4 Review: Hello World Variations Using constants and expressions Using constants and expressions Sub Form_Load() Sub Form_Load() Const WORLD = "World" Const WORLD = "World" MsgBox "Hello " & WORLD MsgBox "Hello " & WORLD End Sub End Sub

5 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 5 Review: Hello World Variations Using Controls and Variables Using Controls and Variables Place a CommandButton on the form Place a CommandButton on the form Add a Label, TextBox, and another Label to the form Add a Label, TextBox, and another Label to the form Make the Caption for the first label "Name" Make the Caption for the first label "Name" Double-click the Button Double-click the Button Enter the following code in Sub CommandButton1_Click(): Enter the following code in Sub CommandButton1_Click(): Dim sHello as String Dim sHello as String sHello = "Hello " & Text1.Text sHello = "Hello " & Text1.Text Label2.Caption = sHello Label2.Caption = sHello Press F5 to run, enter a name and click the button Press F5 to run, enter a name and click the button

6 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 6 Review: Hello World Variations This is what your form and code should look like: This is what your form and code should look like:

7 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 7 Hello World Variations What would make the last Hello World better? What would make the last Hello World better?

8 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 8 Hello World Variations What would make the last Hello World better? What would make the last Hello World better? Set the Text property for Text1 to an empty string Set the Text property for Text1 to an empty string Set the Caption property of the CommandButton to "Hello" Set the Caption property of the CommandButton to "Hello"

9 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 9 Approximations of PI Sometimes it is useful to be able to approximate the value of PI by using a fraction. Sometimes it is useful to be able to approximate the value of PI by using a fraction. See http://www.math.hmc.edu/funfacts/ffiles/10004.5.shtml See http://www.math.hmc.edu/funfacts/ffiles/10004.5.shtmlhttp://www.math.hmc.edu/funfacts/ffiles/10004.5.shtml 22 / 7 is a common simple fraction that produces PI with a result correct to 2 decimals (3.14) 22 / 7 is a common simple fraction that produces PI with a result correct to 2 decimals (3.14) 333 / 106 is good to 5 decimal places 333 / 106 is good to 5 decimal places 355 / 113 is good for 6 decimal places 355 / 113 is good for 6 decimal places To get a better result, we must use values greater than 30,000. To get a better result, we must use values greater than 30,000.

10 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 10 Approximations of PI Create a new Standard EXE project Create a new Standard EXE project Place three TextBoxes, three Labels and a CommandButton on the form: Place three TextBoxes, three Labels and a CommandButton on the form:

11 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 11 Approximations 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 TextBoxes: Clear the Text property of all the TextBoxes: Set the Caption for the CommandButton Set the Caption for the CommandButton Command1.Caption = "PI =" Command1.Caption = "PI ="

12 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 12 Approximations of PI After setting these and other properties (Form Caption, other…), your form should look something like this After setting these and other properties (Form Caption, other…), your form should look something like this

13 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 13 Approximations of PI So let us add some code to the application So let us add some code to the application Double-click the button: Double-click the button: Private Sub Command1_Click() Private Sub Command1_Click() Text3.Text = Text1.Text / Text2.Text Text3.Text = Text1.Text / Text2.Text Run the application (F5) Run the application (F5) Type 22 in the Dividend field Type 22 in the Dividend field Type 7 in the Divisor field Type 7 in the Divisor field Click on PI = Click on PI =

14 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 14 Behind the Scenes What goes on behind the scenes in this simple statement? What goes on behind the scenes in this simple statement? A simple experiment: Change the line of code to: A simple experiment: Change the line of code to: Text3.Text = Text1.Text + Text2.Text Text3.Text = Text1.Text + Text2.Text Run the app (F5) and enter the same values Run the app (F5) and enter the same values You get 227! You get 227! Because Text1.Text and Text2.Text are both Strings, the plus sign means CONCATENATE strings Because Text1.Text and Text2.Text are both Strings, the plus sign means CONCATENATE strings But with the original line of code, we got the correct answer? But with the original line of code, we got the correct answer? Because VB is often (NOT ALWAYS) smart enough to know that values must be converted to another type… Because VB is often (NOT ALWAYS) smart enough to know that values must be converted to another type…

15 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 15 Behind the Scenes Contd: Contd: But with the original line of code, we got the correct answer? But with the original line of code, we got the correct answer? Because VB is often (NOT ALWAYS) smart enough to know that values must be converted to another type… Because VB is often (NOT ALWAYS) smart enough to know that values must be converted to another type… REMEMBER REMEMBER There is a difference between a string containing a number and a numeric value. The same is true for dates, etc. There is a difference between a string containing a number and a numeric value. The same is true for dates, etc. Best Practice Best Practice Perform Explicit Type Conversions Perform Explicit Type Conversions

16 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 16 Approximations of PI Back to our application – perform explicit conversions, etc: Back to our application – perform explicit conversions, etc: Double-click the button: Double-click the button: Private Sub Command1_Click() Private Sub Command1_Click() Dim iDividend as Integer Dim iDividend as Integer Dim iDivisor as Integer Dim iDivisor as Integer Dim dblPI as Double Dim dblPI as Double iDividend = Cint(Text1.Text) iDividend = Cint(Text1.Text) iDivisor = Cint(Text2.Text) iDivisor = Cint(Text2.Text) dblPI = iDividend / iDivisor dblPI = iDividend / iDivisor Text3.Text = dblPI Text3.Text = dblPI Or: Or: Text3.Text = CDbl(Text1.Text) / Cint(Text2.Text) Text3.Text = CDbl(Text1.Text) / Cint(Text2.Text)

17 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 17 Type Conversion Functions Each function coerces an expression to a specific data type. Each function coerces an expression to a specific data type.expression data typeexpression data 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 expression argument is any string expression or numeric expression. The required expression argument is any string expression or numeric expression.argumentstring expressionnumeric expressionargumentstring expressionnumeric expression

18 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 18 Type Conversion Functions Numeric – returns integer values Numeric – returns integer values CByte(expression), CInt(expression), CLng(expression) CByte(expression), CInt(expression), CLng(expression) Numeric – returns floating point values Numeric – returns floating point values CSng(expression), CDbl(expression) CSng(expression), CDbl(expression) Numeric – returns fixed decimal point values Numeric – returns fixed decimal point values CDec(expression), CCur(expression) CDec(expression), CCur(expression) Date Date CDate(expression) CDate(expression) Boolean (True/False) Boolean (True/False) CBool(expression) CBool(expression) Other Other CStr(expression), CVar(expression) CStr(expression), CVar(expression)

19 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 19 Approximations of PI Try running the app again, but this time don't type anything into the text boxes: Try running the app again, but this time don't type anything into the text boxes:

20 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 20 Input Validation This is an extremely common problem with many applications: This is an extremely common problem with many applications: Invalid input creates not only the wrong answer, but results in program errors! Invalid input creates not only the wrong answer, but results in program errors! REMEMBER REMEMBER ALWAYS validate input data! ALWAYS validate input data!

21 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 21 Input Validation Let us add some validation code: Let us add some validation code: Double-click the button Double-click the button Add the following statements at the beginning of Public Sub Command1_Click() (after declarations): Add the following statements at the beginning of Public Sub Command1_Click() (after declarations): If Trim$(Text1.Text) = "" Then If Trim$(Text1.Text) = "" Then MsgBox "You must specify a Dividend" MsgBox "You must specify a Dividend" Exit Sub Exit Sub End If End If If Trim$(Text2.Text) = "" Then If Trim$(Text2.Text) = "" Then MsgBox "You must specify a Divisor" MsgBox "You must specify a Divisor" Exit Sub Exit Sub End If End If

22 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 22 Input Validation Typical things to validate Typical 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 data type (example "seven") Input that will not convert to the correct data type (example "seven") Values that are not allowed (example: a divisor of 0) Values that are not allowed (example: a divisor of 0)

23 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 23 Input Validation Typical things to validate Typical things to validate Empty inputs and input that is only blank spaces can be done in one test Empty inputs and input that is only blank spaces can be done in one test The Trim$() function removes leading and trailing spaces The Trim$() function removes leading and trailing spaces If Trim$(Text1.Text) = "" Then If Trim$(Text1.Text) = "" Then Input that will not convert to the correct data type Input that will not convert to the correct data type The IsNumeric() function checks if an expression can be evaluated as a Numeric value The IsNumeric() function checks if an expression can be evaluated as a Numeric value If Not IsNumeric(Text1.Text) Then If Not IsNumeric(Text1.Text) Then Values that are not allowed Values that are not allowed If CInt(Text2.Text) = 0 then If CInt(Text2.Text) = 0 then

24 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 24 Input Validation Real Input Validation code Real Input Validation code If Trim$(Text1.Text) = "" Or Trim$(Text2.Text) = "" Then MsgBox "You must specify both Dividend and Divisor" Exit Sub ElseIf Not IsNumeric(Text1.Text) _ Or Not IsNumeric(Text2.Text) Then MsgBox "Only Numeric values are allowed" Exit Sub Else If CInt(Text2.Text) = 0 Then MsgBox "You can't divide by 0" Exit Sub Exit Sub End If

25 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 25 Accessing Controls on the Form In order to access a control on a form, we specify the name of the control. Usually, we do this to access one of the control's properties, so we also specify the property following a period: In order to access a control on a form, we specify the name of the control. Usually, we do this to access one of the control's properties, so we also specify the property following a period: MsgBox "Hello " & Text1.Text MsgBox "Hello " & Text1.Text

26 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 26 Accessing Controls on the Form We can also use a fully qualified control name: We can also use a fully qualified control name: For instance Form1.Text1.Text For instance Form1.Text1.Text The pseudo-variable Me is a reference to the form that the code belongs to: The pseudo-variable Me is a reference to the form that the code belongs to: Me.Text1.Text Me.Text1.Text Typing Me. brings up all the available properties and methods (procedures) for the form, including all the controls, allowing us to type in a partial name or scroll through a list of available selections Typing Me. brings up all the available properties and methods (procedures) for the form, including all the controls, allowing us to type in a partial name or scroll through a list of available selections

27 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 27 Properties, Methods, and Events We have already used both some Properties (Text1.Text, Label1.Caption) We have already used both some Properties (Text1.Text, Label1.Caption) and some Events – or rather Event Procedures or Event Handlers (Command1_Click(), Form1_Load()) and some Events – or rather Event Procedures or Event Handlers (Command1_Click(), Form1_Load()) But what are "Methods"? But what are "Methods"?

28 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 28 What are Methods A Method is simply a procedure (Sub or Function) that exists in a form's code (or in an object's code) that is available* to be referenced. A Method is simply a procedure (Sub or Function) that exists in a form's code (or in an object's code) that is available* to be referenced. * available to other code modules, objects, or applications

29 3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 29 Form and Control Properties Forms and Controls have several Properties - most of which can be accessed and/or changed both during design and in code. Forms and Controls have several Properties - most of which can be accessed and/or changed both during design and in code. Many form and control properties describe visual aspects of the form/control Many form and control properties describe visual aspects of the form/control For example, a Label’s Caption property determines what text it displays For example, a Label’s Caption property determines what text it displays


Download ppt "3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2."

Similar presentations


Ads by Google