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

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
Chapter 31 Fundamentals of Programming in Visual Basic (Continue IV) Strings Variables and Strings Using Text Boxes for Input and Output Concatenation.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
1/26/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Materials & Prerequisites Course Outline Background Information.
Visual Programming w/ Visual Basic How to prepare your mind to face the endless nightmare By Williem.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Sep-05 Slide:1 ActiveX Controls in VB ActiveX Controls in VB6.
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Applications Development
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
VAT Calculator program Controls Properties Code Results.
CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
31/01/ Selection If selection construct.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic A Quick Tutorial VB Review for ACS 367.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Instructors: Sidra Ehsan Budoor Bawazeer CpCS 206.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
1 By Dr. HANY ELSALAMONY. 1. What are differences between interpreted and compiled languages? 2. What are the programming steps to creating the Visual.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Variables and Arithmetic Operations
CHAPTER FIVE Decision Structures.
Introduction to VB6 Week 1 3/2/2004 PPCC - Introduction to VB6
CHAPTER FIVE Decision Structures.
Variables and Arithmetic Operations
Department Array in Visual Basic
CS285 Introduction - Visual Basic
Introduction to Programming
Presentation transcript:

3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 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/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

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

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

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:

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?

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"

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 See 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.

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:

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 ="

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

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 =

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…

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

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)

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

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)

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:

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!

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

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)

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

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

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

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

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"?

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

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