1 / 19

Chapter 3 – Variables, Input, and Output

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

Download Presentation

Chapter 3 – Variables, Input, and Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output

  2. 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

  3. Formatting Output with Format Functions

  4. 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)

  5. 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))

  6. 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.

  7. Zone Formatting Symbols Dim fmtStr As String = "{0,15:N1}{1,10:C2}{2,8:P0}"

  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.

  10. Input Mask Dialog Box • We will use this mask on our next problem.

  11. 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

  12. Sample Masks • State abbreviation: LL • Phone number: 000-0000 • Social Security Number: 000-00-0000 • License plate: &&&&&&

  13. 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.

  14. 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

  15. Getting Input from an Input Dialog Box stringVar = InputBox(prompt, title) fullName = InputBox("Enter your full name.", "Name") title prompt

  16. 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

  17. 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.

  18. Using a Message Dialog Box for Output MessageBox.Show(prompt, title) MessageBox.Show("Nice try, but no cigar.", "Consolation") title prompt

  19. 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"

More Related