Lecture Roger Sutton CO331 Visual Programming 17: String handling, Date and Time, Multiple forms 1.

Slides:



Advertisements
Similar presentations
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Advertisements

Chapter 3: Using Variables and Constants
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 5 new The Do…Loop Statement
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
Chapter 3: Using Variables and Constants
Tutorial 14 Working with Forms and Regular Expressions.
CS0004: Introduction to Programming Input and Output.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
CPS120: Introduction to Computer Science
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Chapter 8: Manipulating Strings
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Programming with Microsoft Visual Basic th Edition
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Decisions with Select Case and Strings Chapter 4 Part 2.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
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.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Chapter 6 JavaScript: Introduction to Scripting
Data Types, Arithmetic Operations
The Selection Structure
Introduction to Scripting
Variables and Arithmetic Operations
Chapter 5 The Do…Loop Statement
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
String Variable, Methods and Properties
CIS16 Application Development and Programming using Visual Basic.net
String Variable, Methods and Properties
String Variable, Methods and Properties
String Variable, Methods and Properties
JavaScript: Introduction to Scripting
Visual Programming COMP-315
Presentation transcript:

Lecture Roger Sutton CO331 Visual Programming 17: String handling, Date and Time, Multiple forms 1

CO331 Visual Programming Introduction Strings of characters are a very important form of data. Accordingly a number of methods are available to facilitate their processing. Perhaps one of the most common operations is data validation – ensuring that a data item is consistent with some data type declaration. E.g. if a data item is to be used in a calculation, it is prudent to ensure that it is numeric before attempting to convert it to the appropriate data type. Thus 2 If IsNumeric(txtDisplay.Text) Then n = CInt(txtDisplay.Text) Else MessageBox.Show(“Error in number”) End If

CO331 Visual Programming Type conversion Sometimes referred to as ‘casting’ Conversion methods Strings are often converted to numbers and vice-versa using  CInt – converts a numeric string to an Integer  CDbl – converts a numeric string to a Double, and  CStr – converts a number to a String Parse Method In VB.Net each data type is a class with several methods. Accordingly the Parse method can be used to convert a string to some numeric data type E.g. 3 Dim age As Integer Dim studentAge as String = “25” age = Integer.Parse(studentAge)

CO331 Visual Programming Type conversion – cont’d Convert Class This class contains methods to convert a numeric value to a specified data type, or some string value to a numeric data type: E.g. E.g. 4 MethodConvert to ToDoubleDouble ToInt32Integer ToSingleSingle ToStringString Dim sales As Integer = 1000 Dim bonus As Double bonus = Convert.ToDouble(sales) * 0.05

CO331 Visual Programming Type conversion – cont’d Literal type characters A literal constant is an item of data whose value does not change while the program is running. E.g. 500, “Mary” are literal constants. A literal type character forces a literal constant to assume a data type other that the one the form indicates. E.g. 5 Literal type characterData typeExample SShortage = 35S IIntegerhours = 40I LLongpopulation = 2500L FSinglerate = 0.03F RDoublesales = 2356R CCharinitial = “A”C

CO331 Visual Programming String comparison Strings can be considered to be greater than or less than other strings in the sense that they might be arranged in ascending alphabetical order so that one sequence of characters might precede another. Consequently strings might be compared using the relational operators (=, >, <=, etc.) where < is interpreted as ‘before’ ; > to represent ‘after ’, and = to mean ‘equal’ VB uses a lexicographical comparison, i.e. the integer Unicode value of each character is compared. Hence the comparison is case-sensitive, lowercase coming before uppercase. 6

CO331 Visual Programming String modification Trim - often when a user is required to enter text data, they introduce leading or trailing spaces. Invariably these are not significant and might well be removed. The method Trim removes spaces from both ends of a string. E.g.string1 = “ Centre “ resultString = string1.Trim() yields “Centre” in resultString. TrimStart and TrimEnd may be used to remove space characters only from the respective ends. Remove is used to remove a given number of characters from a given position. E.g.string1 = “ Deadline“ resultString = string1.Remove(1, 4) removes 4 characters from position 1 of string1 to produce “Dine”. 7

CO331 Visual Programming String modification – cont’d Insert is used to insert characters into a string at a specified location. E.g.string1 = “Good students!” resultString = string1.Insert(5, “morning, ”) produces “Good morning, students!” ToLower and ToUpper changes characters from uppercase to lower case, and from lower case to upper case respectively. E.g.string1 = “Good students!” resultString = string1.ToUpper( ) produces “GOOD STUDENTS!” and resultString = string1.ToLower( ) produces “good students!” 8

CO331 Visual Programming String examination Length – the length property provides the number of characters in a string. E.g. string1 = “VB Programming” n = string1.Length would result in n being set to 14. Substring copies a specified part of a string. Its arguments indicate the index starting position and number of characters to be copied respectively. E.g. string1 = “VB Programming” resultString = string1.Substring(8, 3) produces “ram” in resultString and string1 is unchanged. IndexOf determines whether a string is contained within a string. If represent it returns the start index of the substring otherwise –1. E.g. string1 = “Mississippi!” n = string1.IndexOf(“sip”) sets n to 6. 9

CO331 Visual Programming String examination –cont’d IsNumeric( String) This function returns True if is argument is numeric and False otherwise. Split( String, separator character) Often it is required to separate a string constant into segments. The function, Split, takes two arguments: the string and the character to indicate the division points and stores the resulting segments in a string array. E.g. 10 Function firstName(ByVal letters As String) As String Dim words(4) As String words = Split(letters, " ") Return words(0) End Function

CO331 Visual Programming Data type: Char The Char data type can store one character, compared to the string data type that may store between 0 and 2 billion characters. A string variable may be viewed as a Char array. E.g. Dim letter(10) As Char letter = txtEntry.Text lblLength.Text = "Length of entry is " & _ UBound(letter) + 1 & " characters" Using the string methods it is possible to access individual characters. This, however, is quite cumbersome in comparison to referencing array elements. 11 Run

CO331 Visual Programming Date and Time Processing VB provides several date and time related functions in addition to the methods of the Date class. The function TimeOfDay( ) provides the current system date and time Today( ) produces the current system date Day applied to a date returns the day of the month as an Integer in the range 1 to 31 WeekDay produces the number of the day of the week Month provides the number of the month in the year WeekdayName and MonthName identify the corresponding day and month respectively Year applied to a date yields the year as an Integer 12

CO331 Visual Programming Date/ Time Processing –cont’d DateDiff returns the length of the interval in the units indicated from the start date to the finish date. It takes three arguments – the first is a method of DateInterval that indicates the part of the date/time to consider:  Year indicates the year  Month – the month as a number from 1 to 12  DayOfYear – the day of the year, a number between 1 and 365(6)  Day – the number of the day of the month  Weekday – the number of the weekday  WeekOfYear – the number of the week of the year  Hour, Minute, Second – the hours, minute and second respectively of the time followed by a start date expression and a finish date expression. 13

CO331 Visual Programming Date and Time Formatting VB provides several predefined date and time formats using the Format function. Suppose Now yields 26/8/2004 5:14:12 PM then: Alternatively the methods ToString, ToLongDateString, ToShortDateString, ToLongTimeString, ToShortTimeString of Date may be used to the same effect. 14 Function commandOutput Format(Now, “General Date”)26/8/2004 5:14:12 PM Format(Now, “Long Date”)26 August 2004 Format(Now, “Short Date”)26/8/2004 Format(Now, “Long Time”)17:14:12 Format(Now, “Short Time”)17:14 Run

CO331 Visual Programming Multiple forms Additional windows are produced by adding forms. This can be done in two ways: With the second and subsequent forms opening inside the main form. These are then dependent on the main form. Consequently if the main form is closed, the application ends. With additional forms opening outside the main form, as document windows. These are MDI (Multiple Document Interface) forms. (Not to be covered here) To add a form: 1.From File menu, select Add New Item …., or From Project menu, select Add Windows Form…., or, etc. 2.In Add New Item, ensure Windows form is selected. 3.Provide an appropriate name. 4.Click open. 15

CO331 Visual Programming Multiple form code Secondary forms are brought into the main by declaring them as variables. Accordingly a new instance of the form is created. E.g. where Form2 is the form name given when added to the project. An instance of the form is created each time the routine is activated. Consequently,depending on the routine, several instances may inadvertently be created. To restrict the number of instances to one, use Static in the declaration. E.g. The variable name can be then be used to access the form’s properties. E.g. 16 Dim anotherForm As New Form2 Static Dim anotherForm As New Form2 anotherForm.Text = “The new form”

CO331 Visual Programming Multiple form code – cont’d The new form will not be visible until the Show() or ShowDialog() methods are invoked. E.g This may be located within a button_Click method. While the window is active, the user can switch between the forms by clicking on them. A secondary form is closed using the methods Hide() or Close(). If there are several secondary forms and data is to be passed between them or if methods are to be accessible from a different form, then such variables and methods should be stored in a module. 17 anotherForm.Show()

CO331 Visual Programming Example: Multiple Forms Visual programming: 18 Run

CO331 Visual Programming MessageBox The MessageBox dialog displays a String that may incorporate the result of a calculation. E.g. MessageBox.Show(“Sum is: “ & CStr(sum)) This may be shortened to MsgBox(“Sum is: “ & CStr(sum)) 19 The pop-up MessageBox displays a custom dialog that provides the user with information that must be acknowledged before the program moves on.

CO331 Visual Programming Function InputBox This is similar to MessageBox, in that it does not occupy screen space permanently – it pops up when required. InputBox takes a single String argument which appears in the dialog and may used to prompt. E.g. Typically the value entered is assigned to a variable, in the process performing any conversion appropriate. 20 num1 = CInt(InputBox("Enter first integer")) num2 = CInt(InputBox("Enter second integer"))