Post on 31-Dec-2015
description
3.3 Input and Output
• Formatting Output with Format Functions
• Formatting Output with Zones
• Using a Masked Text Box for Input
• Dates as Input and Output
• Getting Input from an Input Dialog Box
• Using a Message Dialog Box for Output
• Named Constants
2
3
Formatting Output with Format Functions
Function String Value
FormatNumber(12345.628, 1) 12,345.6
FormatCurrency(12345.628, 2) $12,345.63
FormatPercent(0.183, 0) 18%
Using Formatting
• Format functions return a string
• Used primarily with Listbox or textboxes
• Examples:Listbox1.items.add(FormatNumber(3523.785,2)
Textbox.text = “Profit = “ & FormatCurrency(32)
String myStr = FormatPercent(0.25,1)
4
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))
5
Formatting Output with Zones and Justification
Dim fmtStr As String = "{0, -15}{1, 10}{2, 8}"
lstOutput.Items.Add(String.Format(fmtStr, _
data0, data1, data2))
Here, 15 was preceded by a minus sign. This
produces left justification in 0th zone. There will
be right justification in the other two zones.
6
Zone Formatting Symbols
Dim fmtStr As String = "{0,15:N1}{1,10:C2}{2,8:P0}"
7
Symbols: N, C, and P Effect on zone
:Nr FormatNumber(data, r)
:Cr FormatCurrency(data, r)
:Pr FormatPercent(data, r)
8
Masked Text Box Control
Similar to an ordinary text box, but has a Mask property that restricts what can be typed into the masked text box.
Tasks button
9
Masked Text Box Control
Click on the Tasks button to reveal the Set Mask property.
Click Set Mask to invoke the Input Mask dialog box.
Mask
A Mask setting is a sequence of characters, with 0, L, and & having special meanings.
•0 Placeholder for a digit.
•L Placeholder for a letter.
•& Placeholder for a character
11
Sample Masks
• State abbreviation: LL
• Phone number: 000-0000
• Social Security Number: 000-00-0000
• License plate: &&&&&&
12
Dates as Input and Output
• Date literal: #7/4/1776#
• Declarations: Dim indDay As Date Dim d As Date = CDate(txtBox.Text)
Dim indDay As Date = #7/4/1776#
13
Date literals are enclosed in #s, just like strings are
enclosed in “s.
Masked Textbox and Dates Example
• Problem: Create a program to compute a person’s age in days.
14
Private Sub btnCompute_Click(…) Handles btnCompute.Click Dim d As Date = CDate(mtbDayOfBirth.Text) txtFullDate.Text = FormatDateTime(d, DateFormat.LongDate) txtToday.Text = FormatDateTime(Today, DateFormat.LongDate) txtAgeInDays.Text = FormatNumber(DateDiff(DateInterval.Day, d, Today), 0) End Sub
15
Getting Input from an Input Dialog Box
stringVar = InputBox(prompt, title)
fullName = InputBox("Enter your full name.",
"Name")
title
prompt
Inputbox Example
Problem Description: Ask a user for their full name and report their first name in a textbox.
16
Private Sub btnDisplay_Click(…) Handles btnDisplay.Click Dim prompt, fullName, firstName, title As String prompt = "Enter your full name." title = "Name" fullName = InputBox(prompt, title) firstName = fullName.Substring(0, fullName.IndexOf(" ")) txtOutput.Text = "Your first name is " & firstName End Sub
InputBox Challenge
• Problem: compute the perfect age for a companion for going on a date. Perfect age = 2/3 your age + 10% age
• Remember to convert the user inputbox to an integer and assign it to a variable.
17
18
Using a Message Dialog Box for Output
MessageBox.Show(prompt, title)
MessageBox.Show("Nice try, but no cigar.",
"Consolation")
title
prompt