SlideShare a Scribd company logo
CHAPTER FOUR Variables andArithmetic Operations
Chapter 4: Variables and Arithmetic Operations 2 Objectives Create, modify, and program a TextBox object Use code to place data in the Text property of a Label object Use the AcceptButton and CancelButton properties Understand and declare String and Numeric variables Use assignments statements to place data in variables
Chapter 4: Variables and Arithmetic Operations 3 Objectives Use literals and constants in coding statements Understand scope rules for variables Convert string and numeric data Understand and use arithmetic operators and arithmetic operations
Chapter 4: Variables and Arithmetic Operations 4 Objectives Format and display numeric data as a string Create a form load event  Create a concatenated string Debug a program
Introduction Chapter 4: Variables and Arithmetic Operations 5
Chapter 4: Variables and Arithmetic Operations 6 TextBox Objects With Visual Studio open and the frmDigitalDownloads.vb [Design] tabbed page visible, point to the TextBox .NET component in the Toolbox Drag the TextBox .NET component onto the Windows Form object at the desired location When the upper-left corner of the pointer is located where you want the TextBox object’s upper-left corner, release the left mouse button
TextBox Objects Chapter 4: Variables and Arithmetic Operations 7
Chapter 4: Variables and Arithmetic Operations 8 Sizing and Positioning a TextBox Object Select the TextBox object. Select the (Name) property and name the TextBoxobject txtNumberOfDownloads. Scroll in the Properties window until the Text property is visible and then click the right column for the Text property Type the maximum number of characters the user normally will enter into the text box and then press the ENTER key. When entering numbers, the digit 8 often is entered because it is wider than other digits. In this example, the value 888 is entered because three digits is the maximum number of digits the user normally will enter
Chapter 4: Variables and Arithmetic Operations 9 Sizing and Positioning a TextBox Object Using the Font property in the Properties window, change the Font property to the correct font and font size. For this application, change the font to Century Gothic and change the font size to 12. Then, drag the right edge of the TextBox object to resize the TextBox object so it is slightly wider than the 888 entry To horizontally align the text in the label and the text in the text box, drag the text box up until a red snap line indicates the bottoms of the text are aligned. Then, release the left mouse button
Chapter 4: Variables and Arithmetic Operations 10 Sizing and Positioning a TextBox Object
Chapter 4: Variables and Arithmetic Operations 11 Aligning Text in a TextBox Object Select the TextBox object. In the Properties window, scroll until the TextAlign property is visible, click the TextAlign property in the left column, and then click the list arrow in the right column of the TextAlign property Click Center in the TextAlign property list Because the TextBox object is sized properly, remove the digits in the TextBox object. Select the characters 888 in the Text property, press the DELETE key on your keyboard, and then press the ENTER key
Chapter 4: Variables and Arithmetic Operations 12 Aligning Text in a TextBox Object
Chapter 4: Variables and Arithmetic Operations 13 Creating a MultiLine Text Box Select the TextBox object, click the Action tag, and point to the MultiLine check box Click the MultiLine check box
Chapter 4: Variables and Arithmetic Operations 14 Creating a MaskedTextBox Object
Chapter 4: Variables and Arithmetic Operations 15 Creating a MaskedTextBox Object Drag a MaskedTextBox .NET component from the Toolbox to the Windows Form object. Then, click the Action tag on the TextBox object and point to the Set Mask command Click Set Mask on the MaskedTextBox Tasks list and then click the Short date mask description in the Input Mask dialog box Click the OK button in the Input Mask dialog box and then click anywhere in the Windows Form object
Chapter 4: Variables and Arithmetic Operations 16 Creating a MaskedTextBox Object
Chapter 4: Variables and Arithmetic Operations 17 Label Objects Drag a Label object onto the Windows Form object to the correct location. Name the label lblTotalCostOfDownloads. Change the label to the appropriate font size (Century Gothic, 12 point). In the Text property for the Label object, enter the maximum number of characters ($888.88) that will appear in the label during execution of the program Drag the Label object up until the red snap line appears. Then release the left mouse button
Chapter 4: Variables and Arithmetic Operations 18 Label Objects
Chapter 4: Variables and Arithmetic Operations 19 Accept Button in Form Properties The program will carry out the event handler processing associated with the Accept button if the user clicks the button or if the user presses the ENTER key Click a blank area in the Windows Form object to select it. Scroll in the Properties window until the AcceptButton property is visible. Click the AcceptButton property name in the left column and then click the AcceptButton property list arrow in the right column Click btnCalculateCost in the AcceptButton property list
Accept Button in Form Properties Chapter 4: Variables and Arithmetic Operations 20
Chapter 4: Variables and Arithmetic Operations 21 Cancel Button in Form Properties When the user presses the ESC key, the event handler processing for the button identified as the Cancel button will be executed Click a blank area in the Windows Form object to select it Click the CancelButton property name in the left column in the Properties window for the Windows Form object, and then click the CancelButton list arrow Click the button name (btnClear) in the CancelButton property list
Chapter 4: Variables and Arithmetic Operations 22 Visual Studio Preparation for Code Entry With the Toolbox visible, click the Toolbox Close button. The Toolbox closes and the work area expands in size. To reshow the Toolbox after it has been closed, click the Toolbox button on the Standard toolbar
Chapter 4: Variables and Arithmetic Operations 23 String Data Type A String data type allows every character available on the computer to be stored in it The data type for the value the user enters in a TextBox object and that is stored in the Text property of the TextBox object is string A variable is a named location in RAM where data is stored A String variable is a named location in RAM that can store a string value
Chapter 4: Variables and Arithmetic Operations 24 String Data Type
Chapter 4: Variables and Arithmetic Operations 25 Assignment Statements One method to place data in the variable is to use an assignment statement
Chapter 4: Variables and Arithmetic Operations 26 Assignment Statements With Visual Studio displaying the code editing window and the insertion point located in the desired column, type Dim followed by a space. Then, type the name of the String variable you want to define, strNumberOfSongs on your keyboard Press the SPACEBAR, type the word As and then press the SPACEBAR again Because the entry should be String, type str on your keyboard Press the ENTER key
Chapter 4: Variables and Arithmetic Operations 27 Assignment Statements To begin the assignment statement, type strn. IntelliSense displays the only variable name that starts with the letter strn, the String variable strNumberOfDownloads Press the SPACEBAR, press the EQUAL SIGN key, and then press the SPACEBAR Type txt to display the IntelliSense list of the Form objects, and then type n to identify the txtNumberof Downloads Text Box object in the IntelliSense list Press the PERIOD key and then, if necessary, type te to highlight the Text entry in the IntelliSense list Press the ENTER key
Chapter 4: Variables and Arithmetic Operations 28 Assignment Statements
Chapter 4: Variables and Arithmetic Operations 29 Numeric Data Types A numeric data type must be used in arithmetic operations
Numeric Data Types Chapter 4: Variables and Arithmetic Operations 30
Chapter 4: Variables and Arithmetic Operations 31 Other Data Types
Other Data Types Chapter 4: Variables and Arithmetic Operations 32
Miscellaneous Data Types Chapter 4: Variables and Arithmetic Operations 33
Variable Prefixes Chapter 4: Variables and Arithmetic Operations 34
Chapter 4: Variables and Arithmetic Operations 35 Literals A value is called a literal when the value being used in the assignment statement is literally the value that is required
Chapter 4: Variables and Arithmetic Operations 36 Forced Literal Types Sometimes you might want a literal to be a different data type than the Visual Basic default
Chapter 4: Variables and Arithmetic Operations 37 Constants A constant variable will contain one permanent value throughout the execution of the program
Chapter 4: Variables and Arithmetic Operations 38 Constants The declaration of a constant variable begins with the letters Const, not the letters Dim You must assign the value to be contained in the constant on the same line as the definition of the constant. You cannot attempt to change the value in the constant variable anywhere in the program. If you attempt this, you will produce a compiler error The letter c often is placed before the prefix of the constant variable name to identify throughout the program that it is a constant variable and cannot be changed Other than the letter c constant variable names are formed using the same rules and techniques as nonconstant names
Chapter 4: Variables and Arithmetic Operations 39 Referencing a Variable When a variable is declared, it will be underlined with a green squiggly line until it is referenced in a statement It is mandatory when using a variable in a program that the variable is defined prior to using the variable name in a statement
Chapter 4: Variables and Arithmetic Operations 40 Scope of Variables The scope of a variable specifies where within the program the variable can be referenced in a Visual Basic statement A variable can be referenced only within the region of the program where it is defined The code between the Sub statement and the End Sub statement is a procedure A variable that can only be referenced within the region of the program where it is defined is called a local variable Local variables have a certain lifetime in the program Global variables can be used in multiple regions of a program
Chapter 4: Variables and Arithmetic Operations 41 Converting Variable Data Visual Basic includes several procedures that allow you to convert one data type to another data type A procedure to convert a String data type to an Integer data type is named ToInt32 The procedure is found in the Convert class, which is available in a Visual Studio 2010 class library
Chapter 4: Variables and Arithmetic Operations 42 Using a Procedure A procedure that performs its task but does not return a value is called a Sub procedure A procedure that returns a value is called a Function procedure, or a function An argument identifies a value required by a procedure Every procedure is part of a class
Using a Procedure Chapter 4: Variables and Arithmetic Operations 43
Chapter 4: Variables and Arithmetic Operations 44 Option Strict On Visual Basic will, by default, automatically convert data types if the data type on the right side of the equal sign in an assignment statement is different from the data type on the left side of the equal sign To prevent automatic conversion of values, the developer must insert the Option Strict On statement in the program prior to any event handler code in the program The Option Strict On statement explicitly disallows any default data type conversions in which data loss would occur and any conversion between numeric types and strings
Option Strict On Chapter 4: Variables and Arithmetic Operations 45
Arithmetic Operations Chapter 4: Variables and Arithmetic Operations 46
Arithmetic Operators Chapter 4: Variables and Arithmetic Operations 47
Chapter 4: Variables and Arithmetic Operations 48 Multiple Operations A single assignment statement can contain multiple arithmetic operations Hierarchy of Operations Exponentiation (^) is performed first Multiplication (*) and division (/) are performed next Integer division ( is next MOD then occurs Addition (+) and subtraction (-) are performed last Within these five steps, calculations are performed left to right
Displaying Numeric Output Data Chapter 4: Variables and Arithmetic Operations 49
Chapter 4: Variables and Arithmetic Operations 50 Format Specifications for the ToString Function Use the format specifierto identify the format for the numeric data to be returned by the ToString function
Chapter 4: Variables and Arithmetic Operations 51 Precision Specifier The precision specifieris a number that is included within the quotation marks in the function call to identify the number of positions to the right of the decimal point that should be returned
Chapter 4: Variables and Arithmetic Operations 52 Clear Procedure The Clear procedure clears any data currently placed in the Text property of a TextBox object
Chapter 4: Variables and Arithmetic Operations 53 Clearing the Text Property of a Label The Clear procedure cannot be used with a Label object You must write an assignment statement that assigns a null length string to the Text property of a Label object
Chapter 4: Variables and Arithmetic Operations 54 Setting the Focus When the focus is on a TextBox object, the insertion point is located in the text box
Chapter 4: Variables and Arithmetic Operations 55 Form Load Event A form load event occurs when the program starts and the Digital Downloads form is loaded This event handler completes the following tasks: Display the cost per download heading Clear the placeholder from the lblTotalCostOfDownloads Text property Set the focus on the txtNumberOfDownloads text box
Chapter 4: Variables and Arithmetic Operations 56 Concatenation The process of joining two different values into a single string is called concatenation The values being concatenated must be String data types
Concatenation Chapter 4: Variables and Arithmetic Operations 57
Chapter 4: Variables and Arithmetic Operations 58 Class Scope When a variable is referenced in two different event handling procedures, it must be defined at the class level instead of the procedure (event handler) level
Debugging Your Program A Format Exception occurs when the user enters data that a statement within the program cannot process properly Chapter 4: Variables and Arithmetic Operations 59
Chapter 4: Variables and Arithmetic Operations 60 Debugging Your Program An Overflow Exception occurs when the user enters a value greater than the maximum value that can be processed by the statement It is not possible to divide by zero, so if your program contains a division operation and the divisor is equal to zero, the Divide By Zero Exception will occur
Program Design Chapter 4: Variables and Arithmetic Operations 61
Program Design Chapter 4: Variables and Arithmetic Operations 62
Event Planning Document Chapter 4: Variables and Arithmetic Operations 63
Chapter 4: Variables and Arithmetic Operations 64 Summary Create, modify, and program a TextBox object Use code to place data in the Text property of a Label object Use the AcceptButton and CancelButton properties Understand and declare String and Numeric variables Use assignments statements to place data in variables
Chapter 4: Variables and Arithmetic Operations 65 Summary Use literals and constants in coding statements Understand scope rules for variables Convert string and numeric data Understand and use arithmetic operators and arithmetic operations
Chapter 4: Variables and Arithmetic Operations 66 Summary Format and display numeric data as a string Create a form load event  Create a concatenated string Debug a program
CHAPTER FOUR COMPLETE Variables andArithmetic Operations

More Related Content

What's hot

Cookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business RulesCookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business Rules
Emiel Paasschens
 
Chapter 03 - Program Coding and Design
Chapter 03 - Program Coding and DesignChapter 03 - Program Coding and Design
Chapter 03 - Program Coding and Design
patf719
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
Ali Mattash
 
Chapter 05 show
Chapter 05 showChapter 05 show
Chapter 05 showchda01008
 
C# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesC# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slides
Sami Mut
 
Open Office Calc : Lesson 06
Open Office Calc : Lesson 06Open Office Calc : Lesson 06
Open Office Calc : Lesson 06thinkict
 
Tugas testing
Tugas testingTugas testing
Tugas testing
Astrid yolanda
 
Chapter03 Ppt
Chapter03 PptChapter03 Ppt
Chapter03 Ppt
Osama Yaseen
 
C# Tutorial MSM_Murach chapter-02-slides
C# Tutorial MSM_Murach chapter-02-slidesC# Tutorial MSM_Murach chapter-02-slides
C# Tutorial MSM_Murach chapter-02-slides
Sami Mut
 
Adobe Illustrator CS5 Part 2 : Vector Graphic Effects
Adobe Illustrator CS5 Part 2 : Vector Graphic EffectsAdobe Illustrator CS5 Part 2 : Vector Graphic Effects
Adobe Illustrator CS5 Part 2 : Vector Graphic Effects
csula its training
 
Ms Access
Ms AccessMs Access
Project1 VB
Project1 VBProject1 VB
Project1 VB
sunmitraeducation
 
Lab1
Lab1Lab1
Lab1
rksrks
 

What's hot (15)

Cookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business RulesCookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business Rules
 
Chapter 03 - Program Coding and Design
Chapter 03 - Program Coding and DesignChapter 03 - Program Coding and Design
Chapter 03 - Program Coding and Design
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
 
Chapter 05 show
Chapter 05 showChapter 05 show
Chapter 05 show
 
C# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slidesC# Tutorial MSM_Murach chapter-10-slides
C# Tutorial MSM_Murach chapter-10-slides
 
Open Office Calc : Lesson 06
Open Office Calc : Lesson 06Open Office Calc : Lesson 06
Open Office Calc : Lesson 06
 
Excel chapter-7
Excel chapter-7Excel chapter-7
Excel chapter-7
 
Tugas testing
Tugas testingTugas testing
Tugas testing
 
Chapter03 Ppt
Chapter03 PptChapter03 Ppt
Chapter03 Ppt
 
C# Tutorial MSM_Murach chapter-02-slides
C# Tutorial MSM_Murach chapter-02-slidesC# Tutorial MSM_Murach chapter-02-slides
C# Tutorial MSM_Murach chapter-02-slides
 
Adobe Illustrator CS5 Part 2 : Vector Graphic Effects
Adobe Illustrator CS5 Part 2 : Vector Graphic EffectsAdobe Illustrator CS5 Part 2 : Vector Graphic Effects
Adobe Illustrator CS5 Part 2 : Vector Graphic Effects
 
Ms Access
Ms AccessMs Access
Ms Access
 
OpenCastLabs Excel chapter-3
OpenCastLabs Excel chapter-3OpenCastLabs Excel chapter-3
OpenCastLabs Excel chapter-3
 
Project1 VB
Project1 VBProject1 VB
Project1 VB
 
Lab1
Lab1Lab1
Lab1
 

Viewers also liked

Chapter 1 — Introduction to Visual Basic 2010 Programming
Chapter 1 — Introduction to Visual Basic 2010 Programming Chapter 1 — Introduction to Visual Basic 2010 Programming
Chapter 1 — Introduction to Visual Basic 2010 Programming
francopw
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programmingRoger Argarin
 
Chapter 01
Chapter 01Chapter 01
Chapter 01llmeade
 
14b. Computer Systems Extra Output
14b. Computer Systems   Extra Output14b. Computer Systems   Extra Output
14b. Computer Systems Extra OutputNew Era University
 
13a.Computer Systems Extra Input
13a.Computer Systems   Extra Input13a.Computer Systems   Extra Input
13a.Computer Systems Extra InputNew Era University
 
Intro to MATLAB GUI
Intro to MATLAB GUIIntro to MATLAB GUI
Intro to MATLAB GUI
Asjad Ali
 
27. Computer Applications Business And Engineering
27. Computer Applications   Business And Engineering27. Computer Applications   Business And Engineering
27. Computer Applications Business And EngineeringNew Era University
 
12. Computer Systems Hardware 2
12. Computer Systems   Hardware 212. Computer Systems   Hardware 2
12. Computer Systems Hardware 2New Era University
 
15. Computer Systems Basic Software 1
15. Computer Systems   Basic Software 115. Computer Systems   Basic Software 1
15. Computer Systems Basic Software 1New Era University
 
Matlab GUI
Matlab GUIMatlab GUI
Vtoc, Use Case, Activity Diagram
Vtoc, Use Case, Activity DiagramVtoc, Use Case, Activity Diagram
Vtoc, Use Case, Activity DiagramNew Era University
 
16. Computer Systems Basic Software 2
16. Computer Systems   Basic Software 216. Computer Systems   Basic Software 2
16. Computer Systems Basic Software 2New Era University
 
Basic components of computer system
Basic components  of computer systemBasic components  of computer system
Basic components of computer system
TECHNOHABIT
 
Internal parts of a computer and their functions
Internal parts of a computer and their functionsInternal parts of a computer and their functions
Internal parts of a computer and their functionsEmily-scamell
 
Internal components of the computer
Internal components of the computerInternal components of the computer
Internal components of the computer
DanielAtkinson96
 
Presentation on visual basic 6 (vb6)
Presentation on visual basic 6 (vb6)Presentation on visual basic 6 (vb6)
Presentation on visual basic 6 (vb6)pbarasia
 
Sample Narrative report for seminars
Sample Narrative report for seminarsSample Narrative report for seminars
Sample Narrative report for seminars
New Era University
 
Introduction to Basic Computer Concepts Presentation
Introduction to Basic Computer Concepts PresentationIntroduction to Basic Computer Concepts Presentation
Introduction to Basic Computer Concepts Presentation
Ana Tan
 
Components of a computer system
Components of a computer systemComponents of a computer system
Components of a computer systemlistergc
 

Viewers also liked (19)

Chapter 1 — Introduction to Visual Basic 2010 Programming
Chapter 1 — Introduction to Visual Basic 2010 Programming Chapter 1 — Introduction to Visual Basic 2010 Programming
Chapter 1 — Introduction to Visual Basic 2010 Programming
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programming
 
Chapter 01
Chapter 01Chapter 01
Chapter 01
 
14b. Computer Systems Extra Output
14b. Computer Systems   Extra Output14b. Computer Systems   Extra Output
14b. Computer Systems Extra Output
 
13a.Computer Systems Extra Input
13a.Computer Systems   Extra Input13a.Computer Systems   Extra Input
13a.Computer Systems Extra Input
 
Intro to MATLAB GUI
Intro to MATLAB GUIIntro to MATLAB GUI
Intro to MATLAB GUI
 
27. Computer Applications Business And Engineering
27. Computer Applications   Business And Engineering27. Computer Applications   Business And Engineering
27. Computer Applications Business And Engineering
 
12. Computer Systems Hardware 2
12. Computer Systems   Hardware 212. Computer Systems   Hardware 2
12. Computer Systems Hardware 2
 
15. Computer Systems Basic Software 1
15. Computer Systems   Basic Software 115. Computer Systems   Basic Software 1
15. Computer Systems Basic Software 1
 
Matlab GUI
Matlab GUIMatlab GUI
Matlab GUI
 
Vtoc, Use Case, Activity Diagram
Vtoc, Use Case, Activity DiagramVtoc, Use Case, Activity Diagram
Vtoc, Use Case, Activity Diagram
 
16. Computer Systems Basic Software 2
16. Computer Systems   Basic Software 216. Computer Systems   Basic Software 2
16. Computer Systems Basic Software 2
 
Basic components of computer system
Basic components  of computer systemBasic components  of computer system
Basic components of computer system
 
Internal parts of a computer and their functions
Internal parts of a computer and their functionsInternal parts of a computer and their functions
Internal parts of a computer and their functions
 
Internal components of the computer
Internal components of the computerInternal components of the computer
Internal components of the computer
 
Presentation on visual basic 6 (vb6)
Presentation on visual basic 6 (vb6)Presentation on visual basic 6 (vb6)
Presentation on visual basic 6 (vb6)
 
Sample Narrative report for seminars
Sample Narrative report for seminarsSample Narrative report for seminars
Sample Narrative report for seminars
 
Introduction to Basic Computer Concepts Presentation
Introduction to Basic Computer Concepts PresentationIntroduction to Basic Computer Concepts Presentation
Introduction to Basic Computer Concepts Presentation
 
Components of a computer system
Components of a computer systemComponents of a computer system
Components of a computer system
 

Similar to Chapter 04

EX19_AC_CH03_GRADER_HOE_AS_InstructionsGrader - Instructi
EX19_AC_CH03_GRADER_HOE_AS_InstructionsGrader - InstructiEX19_AC_CH03_GRADER_HOE_AS_InstructionsGrader - Instructi
EX19_AC_CH03_GRADER_HOE_AS_InstructionsGrader - Instructi
BetseyCalderon89
 
Cognos TM1 Assignments
Cognos TM1 Assignments Cognos TM1 Assignments
Cognos TM1 Assignments
rameshkp054
 
WP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Diseño del interfaz con SilverlightWP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Diseño del interfaz con Silverlight
MICTT Palma
 
CBIS 327 ~ Chapter 06
CBIS 327 ~ Chapter 06CBIS 327 ~ Chapter 06
CBIS 327 ~ Chapter 06mwarrick
 
Fahri tugas cloud1
Fahri tugas cloud1Fahri tugas cloud1
Fahri tugas cloud1
FAHRIZAENURIPUTRA
 
Variables and calculations_chpt_4
Variables and calculations_chpt_4Variables and calculations_chpt_4
Variables and calculations_chpt_4
cmontanez
 
Getting started with the visual basic editor
Getting started with the visual basic editorGetting started with the visual basic editor
Getting started with the visual basic editor
putiadetiara
 
Html Xhtml And Xml 3e Tutorial 6
Html Xhtml And Xml 3e Tutorial 6Html Xhtml And Xml 3e Tutorial 6
Html Xhtml And Xml 3e Tutorial 6larsonsb
 
Computer Assignment #2 Company Database GuideCustomer Table Da.docx
Computer Assignment #2 Company Database GuideCustomer Table Da.docxComputer Assignment #2 Company Database GuideCustomer Table Da.docx
Computer Assignment #2 Company Database GuideCustomer Table Da.docx
zollyjenkins
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3
Bhushan Mulmule
 
-Working with Text in Corel Draw
-Working with Text in Corel Draw-Working with Text in Corel Draw
-Working with Text in Corel Draw
SanthiNivas
 
Chapter 02
Chapter 02Chapter 02
Chapter 02llmeade
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2ppJ. C.
 
Sharbani bhattacharya VB Structures
Sharbani bhattacharya VB StructuresSharbani bhattacharya VB Structures
Sharbani bhattacharya VB Structures
Sharbani Bhattacharya
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
Bagzzz
 
Visualbasic tutorial
Visualbasic tutorialVisualbasic tutorial
Visualbasic tutorial
Andi Simanjuntak
 
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Rebekka Aalbers-de Jong
 

Similar to Chapter 04 (20)

EX19_AC_CH03_GRADER_HOE_AS_InstructionsGrader - Instructi
EX19_AC_CH03_GRADER_HOE_AS_InstructionsGrader - InstructiEX19_AC_CH03_GRADER_HOE_AS_InstructionsGrader - Instructi
EX19_AC_CH03_GRADER_HOE_AS_InstructionsGrader - Instructi
 
Cognos TM1 Assignments
Cognos TM1 Assignments Cognos TM1 Assignments
Cognos TM1 Assignments
 
WP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Diseño del interfaz con SilverlightWP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Diseño del interfaz con Silverlight
 
CBIS 327 ~ Chapter 06
CBIS 327 ~ Chapter 06CBIS 327 ~ Chapter 06
CBIS 327 ~ Chapter 06
 
Fahri tugas cloud1
Fahri tugas cloud1Fahri tugas cloud1
Fahri tugas cloud1
 
Variables and calculations_chpt_4
Variables and calculations_chpt_4Variables and calculations_chpt_4
Variables and calculations_chpt_4
 
Getting started with the visual basic editor
Getting started with the visual basic editorGetting started with the visual basic editor
Getting started with the visual basic editor
 
Excel chapter-4
Excel chapter-4Excel chapter-4
Excel chapter-4
 
Html Xhtml And Xml 3e Tutorial 6
Html Xhtml And Xml 3e Tutorial 6Html Xhtml And Xml 3e Tutorial 6
Html Xhtml And Xml 3e Tutorial 6
 
Computer Assignment #2 Company Database GuideCustomer Table Da.docx
Computer Assignment #2 Company Database GuideCustomer Table Da.docxComputer Assignment #2 Company Database GuideCustomer Table Da.docx
Computer Assignment #2 Company Database GuideCustomer Table Da.docx
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3
 
-Working with Text in Corel Draw
-Working with Text in Corel Draw-Working with Text in Corel Draw
-Working with Text in Corel Draw
 
Chapter 02
Chapter 02Chapter 02
Chapter 02
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
Sharbani bhattacharya VB Structures
Sharbani bhattacharya VB StructuresSharbani bhattacharya VB Structures
Sharbani bhattacharya VB Structures
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 
Visualbasic tutorial
Visualbasic tutorialVisualbasic tutorial
Visualbasic tutorial
 
Excel chapter-6
Excel chapter-6Excel chapter-6
Excel chapter-6
 
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
 
Mca 504 dotnet_unit5
Mca 504 dotnet_unit5Mca 504 dotnet_unit5
Mca 504 dotnet_unit5
 

Chapter 04

  • 1. CHAPTER FOUR Variables andArithmetic Operations
  • 2. Chapter 4: Variables and Arithmetic Operations 2 Objectives Create, modify, and program a TextBox object Use code to place data in the Text property of a Label object Use the AcceptButton and CancelButton properties Understand and declare String and Numeric variables Use assignments statements to place data in variables
  • 3. Chapter 4: Variables and Arithmetic Operations 3 Objectives Use literals and constants in coding statements Understand scope rules for variables Convert string and numeric data Understand and use arithmetic operators and arithmetic operations
  • 4. Chapter 4: Variables and Arithmetic Operations 4 Objectives Format and display numeric data as a string Create a form load event Create a concatenated string Debug a program
  • 5. Introduction Chapter 4: Variables and Arithmetic Operations 5
  • 6. Chapter 4: Variables and Arithmetic Operations 6 TextBox Objects With Visual Studio open and the frmDigitalDownloads.vb [Design] tabbed page visible, point to the TextBox .NET component in the Toolbox Drag the TextBox .NET component onto the Windows Form object at the desired location When the upper-left corner of the pointer is located where you want the TextBox object’s upper-left corner, release the left mouse button
  • 7. TextBox Objects Chapter 4: Variables and Arithmetic Operations 7
  • 8. Chapter 4: Variables and Arithmetic Operations 8 Sizing and Positioning a TextBox Object Select the TextBox object. Select the (Name) property and name the TextBoxobject txtNumberOfDownloads. Scroll in the Properties window until the Text property is visible and then click the right column for the Text property Type the maximum number of characters the user normally will enter into the text box and then press the ENTER key. When entering numbers, the digit 8 often is entered because it is wider than other digits. In this example, the value 888 is entered because three digits is the maximum number of digits the user normally will enter
  • 9. Chapter 4: Variables and Arithmetic Operations 9 Sizing and Positioning a TextBox Object Using the Font property in the Properties window, change the Font property to the correct font and font size. For this application, change the font to Century Gothic and change the font size to 12. Then, drag the right edge of the TextBox object to resize the TextBox object so it is slightly wider than the 888 entry To horizontally align the text in the label and the text in the text box, drag the text box up until a red snap line indicates the bottoms of the text are aligned. Then, release the left mouse button
  • 10. Chapter 4: Variables and Arithmetic Operations 10 Sizing and Positioning a TextBox Object
  • 11. Chapter 4: Variables and Arithmetic Operations 11 Aligning Text in a TextBox Object Select the TextBox object. In the Properties window, scroll until the TextAlign property is visible, click the TextAlign property in the left column, and then click the list arrow in the right column of the TextAlign property Click Center in the TextAlign property list Because the TextBox object is sized properly, remove the digits in the TextBox object. Select the characters 888 in the Text property, press the DELETE key on your keyboard, and then press the ENTER key
  • 12. Chapter 4: Variables and Arithmetic Operations 12 Aligning Text in a TextBox Object
  • 13. Chapter 4: Variables and Arithmetic Operations 13 Creating a MultiLine Text Box Select the TextBox object, click the Action tag, and point to the MultiLine check box Click the MultiLine check box
  • 14. Chapter 4: Variables and Arithmetic Operations 14 Creating a MaskedTextBox Object
  • 15. Chapter 4: Variables and Arithmetic Operations 15 Creating a MaskedTextBox Object Drag a MaskedTextBox .NET component from the Toolbox to the Windows Form object. Then, click the Action tag on the TextBox object and point to the Set Mask command Click Set Mask on the MaskedTextBox Tasks list and then click the Short date mask description in the Input Mask dialog box Click the OK button in the Input Mask dialog box and then click anywhere in the Windows Form object
  • 16. Chapter 4: Variables and Arithmetic Operations 16 Creating a MaskedTextBox Object
  • 17. Chapter 4: Variables and Arithmetic Operations 17 Label Objects Drag a Label object onto the Windows Form object to the correct location. Name the label lblTotalCostOfDownloads. Change the label to the appropriate font size (Century Gothic, 12 point). In the Text property for the Label object, enter the maximum number of characters ($888.88) that will appear in the label during execution of the program Drag the Label object up until the red snap line appears. Then release the left mouse button
  • 18. Chapter 4: Variables and Arithmetic Operations 18 Label Objects
  • 19. Chapter 4: Variables and Arithmetic Operations 19 Accept Button in Form Properties The program will carry out the event handler processing associated with the Accept button if the user clicks the button or if the user presses the ENTER key Click a blank area in the Windows Form object to select it. Scroll in the Properties window until the AcceptButton property is visible. Click the AcceptButton property name in the left column and then click the AcceptButton property list arrow in the right column Click btnCalculateCost in the AcceptButton property list
  • 20. Accept Button in Form Properties Chapter 4: Variables and Arithmetic Operations 20
  • 21. Chapter 4: Variables and Arithmetic Operations 21 Cancel Button in Form Properties When the user presses the ESC key, the event handler processing for the button identified as the Cancel button will be executed Click a blank area in the Windows Form object to select it Click the CancelButton property name in the left column in the Properties window for the Windows Form object, and then click the CancelButton list arrow Click the button name (btnClear) in the CancelButton property list
  • 22. Chapter 4: Variables and Arithmetic Operations 22 Visual Studio Preparation for Code Entry With the Toolbox visible, click the Toolbox Close button. The Toolbox closes and the work area expands in size. To reshow the Toolbox after it has been closed, click the Toolbox button on the Standard toolbar
  • 23. Chapter 4: Variables and Arithmetic Operations 23 String Data Type A String data type allows every character available on the computer to be stored in it The data type for the value the user enters in a TextBox object and that is stored in the Text property of the TextBox object is string A variable is a named location in RAM where data is stored A String variable is a named location in RAM that can store a string value
  • 24. Chapter 4: Variables and Arithmetic Operations 24 String Data Type
  • 25. Chapter 4: Variables and Arithmetic Operations 25 Assignment Statements One method to place data in the variable is to use an assignment statement
  • 26. Chapter 4: Variables and Arithmetic Operations 26 Assignment Statements With Visual Studio displaying the code editing window and the insertion point located in the desired column, type Dim followed by a space. Then, type the name of the String variable you want to define, strNumberOfSongs on your keyboard Press the SPACEBAR, type the word As and then press the SPACEBAR again Because the entry should be String, type str on your keyboard Press the ENTER key
  • 27. Chapter 4: Variables and Arithmetic Operations 27 Assignment Statements To begin the assignment statement, type strn. IntelliSense displays the only variable name that starts with the letter strn, the String variable strNumberOfDownloads Press the SPACEBAR, press the EQUAL SIGN key, and then press the SPACEBAR Type txt to display the IntelliSense list of the Form objects, and then type n to identify the txtNumberof Downloads Text Box object in the IntelliSense list Press the PERIOD key and then, if necessary, type te to highlight the Text entry in the IntelliSense list Press the ENTER key
  • 28. Chapter 4: Variables and Arithmetic Operations 28 Assignment Statements
  • 29. Chapter 4: Variables and Arithmetic Operations 29 Numeric Data Types A numeric data type must be used in arithmetic operations
  • 30. Numeric Data Types Chapter 4: Variables and Arithmetic Operations 30
  • 31. Chapter 4: Variables and Arithmetic Operations 31 Other Data Types
  • 32. Other Data Types Chapter 4: Variables and Arithmetic Operations 32
  • 33. Miscellaneous Data Types Chapter 4: Variables and Arithmetic Operations 33
  • 34. Variable Prefixes Chapter 4: Variables and Arithmetic Operations 34
  • 35. Chapter 4: Variables and Arithmetic Operations 35 Literals A value is called a literal when the value being used in the assignment statement is literally the value that is required
  • 36. Chapter 4: Variables and Arithmetic Operations 36 Forced Literal Types Sometimes you might want a literal to be a different data type than the Visual Basic default
  • 37. Chapter 4: Variables and Arithmetic Operations 37 Constants A constant variable will contain one permanent value throughout the execution of the program
  • 38. Chapter 4: Variables and Arithmetic Operations 38 Constants The declaration of a constant variable begins with the letters Const, not the letters Dim You must assign the value to be contained in the constant on the same line as the definition of the constant. You cannot attempt to change the value in the constant variable anywhere in the program. If you attempt this, you will produce a compiler error The letter c often is placed before the prefix of the constant variable name to identify throughout the program that it is a constant variable and cannot be changed Other than the letter c constant variable names are formed using the same rules and techniques as nonconstant names
  • 39. Chapter 4: Variables and Arithmetic Operations 39 Referencing a Variable When a variable is declared, it will be underlined with a green squiggly line until it is referenced in a statement It is mandatory when using a variable in a program that the variable is defined prior to using the variable name in a statement
  • 40. Chapter 4: Variables and Arithmetic Operations 40 Scope of Variables The scope of a variable specifies where within the program the variable can be referenced in a Visual Basic statement A variable can be referenced only within the region of the program where it is defined The code between the Sub statement and the End Sub statement is a procedure A variable that can only be referenced within the region of the program where it is defined is called a local variable Local variables have a certain lifetime in the program Global variables can be used in multiple regions of a program
  • 41. Chapter 4: Variables and Arithmetic Operations 41 Converting Variable Data Visual Basic includes several procedures that allow you to convert one data type to another data type A procedure to convert a String data type to an Integer data type is named ToInt32 The procedure is found in the Convert class, which is available in a Visual Studio 2010 class library
  • 42. Chapter 4: Variables and Arithmetic Operations 42 Using a Procedure A procedure that performs its task but does not return a value is called a Sub procedure A procedure that returns a value is called a Function procedure, or a function An argument identifies a value required by a procedure Every procedure is part of a class
  • 43. Using a Procedure Chapter 4: Variables and Arithmetic Operations 43
  • 44. Chapter 4: Variables and Arithmetic Operations 44 Option Strict On Visual Basic will, by default, automatically convert data types if the data type on the right side of the equal sign in an assignment statement is different from the data type on the left side of the equal sign To prevent automatic conversion of values, the developer must insert the Option Strict On statement in the program prior to any event handler code in the program The Option Strict On statement explicitly disallows any default data type conversions in which data loss would occur and any conversion between numeric types and strings
  • 45. Option Strict On Chapter 4: Variables and Arithmetic Operations 45
  • 46. Arithmetic Operations Chapter 4: Variables and Arithmetic Operations 46
  • 47. Arithmetic Operators Chapter 4: Variables and Arithmetic Operations 47
  • 48. Chapter 4: Variables and Arithmetic Operations 48 Multiple Operations A single assignment statement can contain multiple arithmetic operations Hierarchy of Operations Exponentiation (^) is performed first Multiplication (*) and division (/) are performed next Integer division ( is next MOD then occurs Addition (+) and subtraction (-) are performed last Within these five steps, calculations are performed left to right
  • 49. Displaying Numeric Output Data Chapter 4: Variables and Arithmetic Operations 49
  • 50. Chapter 4: Variables and Arithmetic Operations 50 Format Specifications for the ToString Function Use the format specifierto identify the format for the numeric data to be returned by the ToString function
  • 51. Chapter 4: Variables and Arithmetic Operations 51 Precision Specifier The precision specifieris a number that is included within the quotation marks in the function call to identify the number of positions to the right of the decimal point that should be returned
  • 52. Chapter 4: Variables and Arithmetic Operations 52 Clear Procedure The Clear procedure clears any data currently placed in the Text property of a TextBox object
  • 53. Chapter 4: Variables and Arithmetic Operations 53 Clearing the Text Property of a Label The Clear procedure cannot be used with a Label object You must write an assignment statement that assigns a null length string to the Text property of a Label object
  • 54. Chapter 4: Variables and Arithmetic Operations 54 Setting the Focus When the focus is on a TextBox object, the insertion point is located in the text box
  • 55. Chapter 4: Variables and Arithmetic Operations 55 Form Load Event A form load event occurs when the program starts and the Digital Downloads form is loaded This event handler completes the following tasks: Display the cost per download heading Clear the placeholder from the lblTotalCostOfDownloads Text property Set the focus on the txtNumberOfDownloads text box
  • 56. Chapter 4: Variables and Arithmetic Operations 56 Concatenation The process of joining two different values into a single string is called concatenation The values being concatenated must be String data types
  • 57. Concatenation Chapter 4: Variables and Arithmetic Operations 57
  • 58. Chapter 4: Variables and Arithmetic Operations 58 Class Scope When a variable is referenced in two different event handling procedures, it must be defined at the class level instead of the procedure (event handler) level
  • 59. Debugging Your Program A Format Exception occurs when the user enters data that a statement within the program cannot process properly Chapter 4: Variables and Arithmetic Operations 59
  • 60. Chapter 4: Variables and Arithmetic Operations 60 Debugging Your Program An Overflow Exception occurs when the user enters a value greater than the maximum value that can be processed by the statement It is not possible to divide by zero, so if your program contains a division operation and the divisor is equal to zero, the Divide By Zero Exception will occur
  • 61. Program Design Chapter 4: Variables and Arithmetic Operations 61
  • 62. Program Design Chapter 4: Variables and Arithmetic Operations 62
  • 63. Event Planning Document Chapter 4: Variables and Arithmetic Operations 63
  • 64. Chapter 4: Variables and Arithmetic Operations 64 Summary Create, modify, and program a TextBox object Use code to place data in the Text property of a Label object Use the AcceptButton and CancelButton properties Understand and declare String and Numeric variables Use assignments statements to place data in variables
  • 65. Chapter 4: Variables and Arithmetic Operations 65 Summary Use literals and constants in coding statements Understand scope rules for variables Convert string and numeric data Understand and use arithmetic operators and arithmetic operations
  • 66. Chapter 4: Variables and Arithmetic Operations 66 Summary Format and display numeric data as a string Create a form load event Create a concatenated string Debug a program
  • 67. CHAPTER FOUR COMPLETE Variables andArithmetic Operations