190 likes | 449 Views
Chapter 3 – Variables, Input, and Output. 3.1 Numbers 3.2 Strings 3.3 Input and Output. 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
E N D
Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output
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
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)
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))
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.
Zone Formatting Symbols Dim fmtStr As String = "{0,15:N1}{1,10:C2}{2,8:P0}"
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
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.
Input Mask Dialog Box • We will use this mask on our next problem.
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
Sample Masks • State abbreviation: LL • Phone number: 000-0000 • Social Security Number: 000-00-0000 • License plate: &&&&&&
Dates as Input and Output • Date literal: #7/4/1776# • Declarations: Dim indDay AsDate Dim d AsDate= CDate(txtBox.Text) Dim indDay AsDate = #7/4/1776# 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. 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
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. 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.
Using a Message Dialog Box for Output MessageBox.Show(prompt, title) MessageBox.Show("Nice try, but no cigar.", "Consolation") title prompt
Named Constants • Declared with ConstCONSTANT_NAMEAsDataType= value • Value cannot be changed. Examples: Const MIN_VOTING_AGE AsInteger= 18 Const INTEREST_RATE AsDouble= 0.035 Const TITLE AsString= "Visual Basic"