Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.

Slides:



Advertisements
Similar presentations
Chapter 3 – Fundamentals of Programming in VB.NET
Advertisements

Chapter 2 –Visual Basic, Controls, and Events
Chapter 31 Fundamentals of Programming in Visual Basic (Continue IV) Strings Variables and Strings Using Text Boxes for Input and Output Concatenation.
Chapter 31 Visual Basic Controls A Form is a windows-style screen displayed by Visual Basic programs. In a form, a programmer can create objects in a form.
Chapter 31 Fundamentals of Programming in VB(Continue I) Numbers Arithmetic Operations Variables Incrementing the Value of a Variable.
Chapter 2 –Visual Basic, Controls, and Events
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 31 Fundamentals of Programming in Visual Basic (Continue VI) String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Created By Mayson Al-Duwais1. Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special.
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Visual Basic Chapter 1 Mr. Wangler.
CS0004: Introduction to Programming Variables – Numbers.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Chapter 3 - VB 2005 by Schneider1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Introduction to Visual Basic.NET Chapter 2 Introduction to Controls, Events.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Introduction to Visual Basic.NET Your First Visual Basic.NET Application.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 2 –Visual Basic, Controls, and Events
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 2 - VB 2008 by Schneider1 Chapter 2 –Visual Basic, Controls, and Events 2.1 An Introduction to Visual Basic 2.2 Visual Basic Controls 2.3 Visual.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Applications Development
3.2 VB.NET Events An Event Procedure Properties and Event Procedures of the Form Tab Order of Controls Exercises.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET Part I VB.NET Controls VB.NET Events.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
3.4 Strings Variables and Strings Using Text Boxes for Input and Output Concatenation ANSI Character Set String Properties and Methods: LengthToUpper TrimToLower.
Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Chapter 2 - VB 2008 by Schneider1 Chapter 2 –Visual Basic, Controls, and Events 2.1 An Introduction to Visual Basic 2.2 Visual Basic Controls 2.3 Visual.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Introduction to Computer CC111 Week 10 Visual Basic 3 1.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
ALMAJMA'AH UNIVERSITY College of Science and Humanitarians Studies in Alghat Information Technology Section (211Tal course) 1.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 3 – Fundamentals of Programming in Visual Basic
Chapter 3 – Variables, Input, and Output
Variables and Arithmetic Operations
Do Now: What are the three steps to writing a Visual Basic Program?
Chapter 3 Fundamentals of Programming in Visual Basic 3
Chapter 3 – Variables, Input, and Output
Variables and Arithmetic Operations
Chapter 3 – Variables, Input, and Output
Fundamentals of Programming in VB.NET
Strings(Part 1) String Literal and String Variable
String Variable, Methods and Properties
Section 3.3 Numbers Arithmetic Operations Variables
Variable Review & IO User 12/26/2018.
Chapter 3.5 Input and Output
Input and Output.
String Variable, Methods and Properties
Additional Topics in VB.NET
Fundamentals of Programming in VB.Net
String Variable, Methods and Properties
Introduction to Programming
String Variable, Methods and Properties
Input and Output.
Input and Output Chapter 3.5
Presentation transcript:

Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output

Chapter 3 - VB.NET by Schneider2 3.1 VB.NET Controls Invoking VB.NET A Text Box Walkthrough A Button Walkthrough A Label Walkthrough A List Box Walkthrough The Name Property A Help Walkthrough Fonts / Auto Hide

Chapter 3 - VB.NET by Schneider3 Invoking VB.NET

Chapter 3 - VB.NET by Schneider4 Create a New Project

Chapter 3 - VB.NET by Schneider5 Initial VB.NET Screen

Chapter 3 - VB.NET by Schneider6 Demo 1: A Text Box Walkthrough Create a text box with the following properties: contains your first name Verdana font Bold and italic Size 16 Color red Run it and enter your last name Change the textbox to read-only Run again

Chapter 3 - VB.NET by Schneider7 Demo 2: A Text Box Walkthrough Create 3 text boxes on a form Run the program Demo how to change focus forward and backward

Chapter 3 - VB.NET by Schneider8 Demo 3: A Text Box Walkthrough Reopen Demo 1

Chapter 3 - VB.NET by Schneider9 Demo 4: A Button Walkthrough Create a button with the phrase “Please Push Me” on it Run the program and click the button with the mouse Make P as the access key of the button by changing the phrase to “Please Push Me” Run the program and Press Alt+P

Chapter 3 - VB.NET by Schneider10 Demo 5: A Label Walkthrough Create a label with the text “Enter Your Phone Number” Enlarge the label and move the text to the 9 positions Run the program

Chapter 3 - VB.NET by Schneider11 Demo 6: A List Box Walkthrough Create multiple boxes in a form: a listbox, a textbox, a button, and a label Enter text properties Any text property for list box? Run the program

Chapter 3 - VB.NET by Schneider12 Demo 7: Name Property Change the name of controls so that our code can refer to the controls Convention: ButtonbtnSubmit LabellblInstructions List boxlstOutput TextboxtxtInput

Chapter 3 - VB.NET by Schneider13 Demo 8: A Help Walkthrough Create a project, place an object on the form and select the object. Click on Help | Dynamic Help or Alt/H/H for “Textbox Members” and “Public Properties” Click on Help | Index or Alt/H/I for “controls” and “Overview”

Chapter 3 - VB.NET by Schneider14 A Text Box Walkthrough In the ToolBox, double click the Text Box icon The control is selected when you see the sizing handles Press the Del key to delete

Chapter 3 - VB.NET by Schneider15 Text Box Properties Categorized view Alphabetical view

Chapter 3 - VB.NET by Schneider16 Changing Properties

Chapter 3 - VB.NET by Schneider17 ForeColor Property

Chapter 3 - VB.NET by Schneider18 Font Property

Chapter 3 - VB.NET by Schneider19 A Button Walkthrough Add the button Change the Text property

Chapter 3 - VB.NET by Schneider20 Add an "access key"

Chapter 3 - VB.NET by Schneider21 A Label Walkthrough Add the Label Change the Text property Resize the control

Chapter 3 - VB.NET by Schneider22 A List Box Walkthrough Add the List Box Change the Text property Resize the control

Chapter 3 - VB.NET by Schneider23 The Name Property How the programmer refers to a control in code Name must begin with a letter Must be less than 215 characters long May include numbers and the underscore Use appropriate 3 character naming prefix

Chapter 3 - VB.NET by Schneider24 Control Name Prefixes

Chapter 3 - VB.NET by Schneider25 Fonts Proportional width fonts take up less space for "I" than for "W" – like Microsoft Sans Serif Fixed-width fonts take up the same amount of space for each character – like Courier New Fixed-width fonts are good for tables

Chapter 3 - VB.NET by Schneider26 Auto Hide Hides tool windows when not in use Vertical push pin icon indicates auto hide is disabled Click the push pin to make it horizontal and enable auto hide

Chapter 3 - VB.NET by Schneider VB.NET Events An Event Procedure Walkthrough Properties and Event Procedures of the Form The Declaration Statement of an Event Procedure

Chapter 3 - VB.NET by Schneider28 An Event Procedure Walkthrough An event is an action, such as the user clicking on a button Usually, nothing happens until the user does something and generates an event

Chapter 3 - VB.NET by Schneider29 The three steps in creating a VB.NET program: 1.Create the interface; that is, generate, position, and size the objects. 2.Set properties; that is, configure the appearance of the objects. 3.Write the code that executes when events occur.

Chapter 3 - VB.NET by Schneider30 Changing Properties Properties are changed in code with the following: controlName.property = setting This is an assignment statement txtBox.ForeColor = Color.Red

Chapter 3 - VB.NET by Schneider31 Event Procedures Private Sub objectName_event(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles objectName.event Shown in the book as: Private Sub objectName_event(…) Handles objectName.event

Chapter 3 - VB.NET by Schneider32 Structure of an Event Procedure Private Sub objectName_event(...) Handles objectName.event statements End Sub

Chapter 3 - VB.NET by Schneider33 Program Region

Chapter 3 - VB.NET by Schneider34 IntelliSense Automatically pops up to give the programmer help.

Chapter 3 - VB.NET by Schneider35 Code for Walkthrough Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub Private Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.Black End Sub

Chapter 3 - VB.NET by Schneider36 Assigning properties in code The following won't work: Form1.Text = "Demonstration" The form is referred to by the keyword Me. Me.Text = "Demonstration"

Chapter 3 - VB.NET by Schneider37 The Declaration Statement of an Event Procedure A declaration statement for an event procedure: Private Sub btnOne_Click(...) Handles btnOne.Click The name can be changed at will. For example Private Sub ButtonPushed(...) Handles btnOne.Click Handling more than one event: Private Sub ButtonPushed(...) Handles btnOne.Click, btnTwo.Click

Chapter 3 - VB.NET by Schneider Numbers Arithmetic Operations Variables Incrementing the Value of a Variable Built-In Functions: Math.Sqrt Int Math.Round

Chapter 3 - VB.NET by Schneider39 Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors

Chapter 3 - VB.NET by Schneider40 Arithmetic Operations Numbers are called numeric literals Five arithmetic operations in VB.NET + addition - subtraction * multiplication / division ^ exponentiation

Chapter 3 - VB.NET by Schneider41 Variables Declaration: Dim speed As Double Variable name Data type Assignment: speed = 50

Chapter 3 - VB.NET by Schneider42 Initialization Numeric variables are automatically initialized to 0: Dim varName As Double To specify a nonzero initial value Dim varName As Double = 50

Chapter 3 - VB.NET by Schneider43 Incrementing To add 1 to the numeric variable var var = var + 1 Or as a shortcut var +=1

Chapter 3 - VB.NET by Schneider44 Built-in Functions Functions return a value Math.Sqrt(9) returns 3 Int(9.7) returns 9 Math.Round(2.7) is 3

Chapter 3 - VB.NET by Schneider45 Integer Data Type An integer is a whole number Declaring an integer variable: Dim varName As Integer

Chapter 3 - VB.NET by Schneider46 Multiple Declarations Dim a, b As Double Two other types of multiple-declaration statements are Dim a As Double, b As Integer Dim c As Double = 2, b As Integer = 5

Chapter 3 - VB.NET by Schneider47 Three Types of Errors Syntax error Run-time error Logic error

Chapter 3 - VB.NET by Schneider Strings Variables and Strings Using Text Boxes for Input and Output Concatenation ANSI Character Set String Properties and Methods: LengthToUpper TrimToLower IndexOfSubstring

Chapter 3 - VB.NET by Schneider49 Strings continued The Empty String Initial Value of a String Option Strict Internal Documentation Line-Continuation Character

Chapter 3 - VB.NET by Schneider50 Variables and Strings Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim today As String today = "Monday" With lstOutput.Items.Clear().Add("hello").Add(today) End With End Sub

Chapter 3 - VB.NET by Schneider51 Using Text Boxes for Input and Output The contents of a text box is always a string Input example strVar = txtBox.Text Output example txtBox.Text = strVar

Chapter 3 - VB.NET by Schneider52 Data Conversion Because the contents of a text box is always a string, sometimes you must convert the input or output numVar = CDbl(txtBox.Text) txtBox.Text = CStr(numVar) Converts a String to a Double Converts a number to a string

Chapter 3 - VB.NET by Schneider53 Concatenation Combining two strings to make a new string quote1 = "The ballgame isn't over, " quote2 = "until it's over." quote = quote1 & quote2 txtOutput.Text = quote & " Yogi Berra" Displays The ball game isn't over until it's over. Yogi Berra

Chapter 3 - VB.NET by Schneider54 ANSI Character Set A numeric representation for every key on the keyboard

Chapter 3 - VB.NET by Schneider55 String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123 Hike".Length is 8. "123 Hike".ToLower is 123 hike. "a" & " bcd ".Trim & "efg" is abcdefg.

Chapter 3 - VB.NET by Schneider56 More String Properties and Methods: "fanatic".Substring(0, 3) is "fan". "fanatic".IndexOf("ati") is 3. "fanatic".Substring(4, 2) is "ti". "fanatic".IndexOf("a") is 1. "fanatic".Substring(4) is "tic". "fanatic".IndexOf("nt") is –1.

Chapter 3 - VB.NET by Schneider57 The Empty String The string "", which contains no characters, is called the empty string or the zero-length string. The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txtBox.Clear() or the statement txtBox.Text = ""

Chapter 3 - VB.NET by Schneider58 Initial Value of a String By default the initial value is Nothing Strings can be given a different initial value as follows: Dim today As String = "Monday"

Chapter 3 - VB.NET by Schneider59 Option Strict VB.NET allows numeric variables to be assigned strings and vice versa, a poor programming practice. To turn this feature off, put the following statement at the very top of the code window Option Strict On

Chapter 3 - VB.NET by Schneider60 Internal Documentation 1.Other people can easily understand the program. 2.You can understand the program when you read it later. 3.Long programs are easier to read because the purposes of individual pieces can be determined at a glance.

Chapter 3 - VB.NET by Schneider61 Line-Continuation Character A long line of code can be continued on another line by using underscore (_) preceded by a space msg = "640K ought to be enough " & _ "for anybody. (Bill Gates, 1981)"

Chapter 3 - VB.NET by Schneider Input and Output Formatting Output with Format Functions Formatting Output with Zones Reading Data from Files Getting Input from an Input Dialog Box Using a Message Dialog Box for Output

Chapter 3 - VB.NET by Schneider63 Formatting Output with Format Functions FunctionString Value FormatNumber( ,1)12,345.6 FormatCurrency( ,2)$12, FormatPercent(0.185,2)18.50%

Chapter 3 - VB.NET by Schneider64 Formatting Output with Zones Use a fixed-width font such as Courier New Divide the characters into zones with a format string. Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}" lstOutput.Items.Add(String.Format(fmtStr, data0, data1, data2))

Chapter 3 - VB.NET by Schneider65 Inputting Data Data can be stored in files and accessed with a StreamReader object or supplied by the user with an input dialog box.

Chapter 3 - VB.NET by Schneider66 Steps to Use StreamReader 1.Execute a statement of the form Dim readerVar As IO.StreamReader = _ IO.File.OpenText(filespec) or the pair of statements Dim readerVar As IO.StreamReader readerVar = IO.File.OpenText(filespec) 2.Assume the file contains one item of data per line. Read items of data in order, one at a time, from the file with the ReadLine method. strVar = readerVar.ReadLine 3.After the desired items have been read from the file, terminate the communications link readerVar.Close()

Chapter 3 - VB.NET by Schneider67 Getting Input from an Input Dialog Box stringVar = InputBox(prompt, title) fileName = InputBox("Enter the name " _ & "of the file containing the " & _ "information.", "Name of File")

Chapter 3 - VB.NET by Schneider68 Using a Message Dialog Box for Output MsgBox(prompt,, title) MsgBox("Nice try, but no cigar.",, "Consolation")