More Form Controls - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

More Form Controls

Description:

May also be created directly from Visual Basic. Sequential File Modes ... DropDown (and DropDownList) combo box. Example 2. Private Sub btnDisplay_Click ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 40
Provided by: mil94
Category:
Tags: controls | dropdown | form | more

less

Transcript and Presenter's Notes

Title: More Form Controls


1
More Form Controls
  • CS0004
  • Lecture 20
  • Chapter 9.1 and 9.2

2
Admin
  • Project 4 Due Wednesday
  • April 4th, by midnight
  • Project 3 Graded
  • Hand back after class
  • Class Canceled Next Wednesday
  • April 11th

3
Review - Sequential Files
  • A sequential file consists of data stored in a
    text file on disk.
  • May be created using Notepad
  • May also be created directly from Visual Basic

4
Sequential File Modes
  • CreateText open for output
  • OpenText open for input
  • AppendText open for append
  • A file should not be opened in two different
    modes at the same time.

5
Exception Handling
6
Try-Catch-Finally Block
  • Dim taxCredit As Double
  • Private Sub btnComputeCredit_Click(...)
  • Handles
    btnComputeCredit.Click
  • Dim numDependents As Integer, message As String
  • Try
  • numDependents CInt(InputBox("How many
    dependents?"))
  • Catch
  • message "You did not answer the question "
    _
  • " with an integer value. We will "
    _
  • " assume your answer is zero."
  • MsgBox(message)
  • numDependents 0
  • Finally
  • taxCredit 1000 numDependents
  • End Try
  • End Sub

7
Exception Handling and File Errors
  • Exception handling can also catch file access
    errors.
  • File doesn't exist causes an IO.FileNotFoundExcept
    ion
  • If an attempt is made to delete an open file,
    IO.IOException is thrown.

8
File Formats
  • Line Separated Values (LSV)
  • Each value appears on its own line
  • Comma Separated Values (CSV)
  • Records are stored on one line with a comma
    between each field
  • Example
  • Bryan Mills, 412.327.9999, 1728 My Street
  • Joe Smith, 412.327.8323, 999 East Carson

9
Split Function
  • Facilitates working with CSV formatted files.
  • Split can convert a line containing commas into a
    String array.
  • The 0th element contains the text preceding the
    first comma, the 1st element contains the text
    between the first and second commas, ..., and the
    last element contains the text following the last
    comma.

10
Split
  • Dim line As String _ Bob,412.232.2321,333 E
    Street
  • Dim Employees() As String
  • employees line.Split(","c)
  • sets the size of employees() to 3
  • employees(0) Bob
  • employees(1) 412.232.2321
  • employees(2) 333 E Street

11
Split Comments
  • Employees line.Split(",c)
  • In this example, the character comma is called
    the delimiter for the Split function, and the
    letter c specifies that the comma has data type
    Character instead of String. (If Option Strict is
    Off, the letter c can be omitted.)
  • Any character can be used as a delimiter. If no
    character is specified, the Split function will
    use the space character as delimiter.

12
Join Function
  • The reverse of the Split function is the Join
    function
  • Join concatenates the elements of a string array
    into a string containing the elements separated
    by a specified delimiter.
  • Dim greatLakes() As String _
  • "Huron","Ontario","Michigan","Erie","Superi
    or"
  • Dim lakes As String
  • lakes Join(greatLakes, ",")
  • txtOutput.Text lakes
  • OUTPUT
  • Huron,Ontario,Michigan,Erie,Superior

13
List Box Review
  • lstBox.Items.Count
  • Number of items in lstBox
  • lstBox.Items.Add(string)
  • Adds string to lstBox
  • lstBox.SelectedIndex
  • Gets the item the user has selected
  • lstBox.Items(n)
  • Gets the nth item in the lstBox

14
List Box Review
  • lstBox.Items.RemoveAt(n)
  • Remove the item at the nth location
  • lstBox.Items.Remove(string)
  • Remove ONLY the first occurrence of string in the
    lstBox
  • lstBox.Items.Clear()
  • Removes all items in the lstBox
  • lstBox.Text
  • Gets the currently selected item

15
The Combo Box Control
  • A list box combined with a text box
  • The user has the option of selecting from a list
    or typing in something
  • Essentially same properties, events, and methods
    as list box

16
The Combo Box Control
  • Three types of combo boxes in the DropDownStyle
    property

DropDown (and DropDownList) combo box
Simple combo box
17
Example 2
  • Private Sub btnDisplay_Click(...) _
  • Handles
    btnDisplay.Click
  • txtDisplay.Text cboTitle.Text " "
    txtName.Text
  • End Sub

txtName
cboTitle
txtDisplay
18
The OpenFileDialog Control
  • Implements the standard File Open dialog box
  • Found in the Dialogs section of the Toolbox
  • When you place the control on the form, it will
    not be visible.
  • The icon and default name will appear in the pane
    below the Main area.

19
An Open File Dialog Box
20
The Filter Property
  • Determines what appears in the Files of type
    combo box, and what types of files will be
    displayed. The setting has the general form
  • text for combo box.ext
  • Example Text Files (.TXT).TXT

21
Using the OpenFileDialog control
  • To display the control
  • OpenFileDialog1.ShowDialog()
  • After the Open button has been pressed, the file
    name selected and its complete filespec will be
    contained in the property
  • OpenFileDialog1.FileName

22
Example 3 Task
  • Select a text file and display its contents.
  • Note The Filter property of OpenFileDialog1 is
    set to
  • Text Files (.TXT).TXT

23
Example 3 Code
  • Private Sub btnSelect_Click(...) Handles _
  • btnSelect.Click
  • Dim textFile As String
  • OpenFileDialog1.ShowDialog()
  • textFile OpenFileDialog1.FileName
  • Dim sr As IO.StreamReader _
  • IO.File.OpenText(textFile)
  • Do While sr.Peek ltgt -1
  • lstOutput.Items.Add(sr.ReadLine)
  • Loop
  • sr.Close()
  • End Sub

24
9.2 Seven Elementary Controls
  • The Group Box Control
  • The Check Box Control
  • The Radio Button Control
  • The Timer Control
  • The Picture Box Control
  • The Horizontal and Vertical Scroll Bar Controls

25
The Group Box Control
  • Group boxes are passive objects used to group
    other objects together
  • When you drag a group box, the attached controls
    follow as a unit
  • To attach a control to a group box, create the
    group box, then drag the control you want to
    attach into the group box

26
Group Box Example
Text property of the group box
Three attached controls Button1 Button2 Button3
27
The Check Box Control
  • Consists of a small square and a caption
  • Presents the user with a Yes/No choice
  • During run time, clicking on the check box
    toggles the appearance of a check mark.
  • Checked property is True when the check box is
    checked and False when it is not
  • CheckedChanged event is triggered when the user
    clicks on the check box

28
Example 1 Form
29
Example 1 Code
  • Private Sub Tally(...) Handles chkDrugs.CheckedCha
    nged, _
  • chkDental.CheckedChanged, chkVision.CheckedCha
    nged, _
  • chkMedical.CheckChanged
  • Dim sum As Double 0
  • If chkDrugs.Checked Then
  • sum 12.51
  • End If
  • If chkDental.Checked Then
  • sum 9.68
  • End If
  • If chkVision.Checked Then
  • sum 1.5
  • End If
  • If chkMedical.Checked Then
  • sum 25.25
  • End If
  • txtTotal.Text FormatCurrency(sum)
  • End Sub

30
Example 1 Output
31
The Radio Button Control
  • Consists of a small circle with a caption (that
    is set by the Text property)
  • Normally several radio buttons are attached to a
    group box
  • Gives the user a single choice from several
    options
  • Clicking on one radio button removes the
    selection from another

32
Radio Button Properties
  • To determine if the button is on or off
  • radButton.Checked
  • has value True if button in on.
  • To turn a radio button on
  • radButton.Checked True

33
Example 2 Form
radCandidate1
radCandidate2
txtVote
34
Example 2 Code
  • Private Sub btnVote_Click(...) Handles
    btnVote.Click
  • If radCandidate1.Checked Then
  • txtVote.Text "You voted for Kennedy."
  • ElseIf radCandidate2.Checked Then
  • txtVote.Text "You voted for Nixon."
  • Else
  • txtVote.Text "You voted for neither."
  • End If
  • End Sub

35
Example 2 Output
36
The Timer Control
  • Invisible during run time
  • Triggers an event after a specified period of
    time
  • The Interval property specifies the time period
    measured in milliseconds
  • To begin timing, set the Enabled property to True
  • To stop timing, set the Enabled property to False
  • The event triggered each time Timer1.Interval
    elapses is called Timer1.Tick.

37
Example 3 Form
txtSeconds
38
Example 3 Code
  • Private Sub btnStart_Click(...) Handles
    btnStart.Click
  • txtSeconds.Text "0" 'Reset watch
  • tmrWatch.Enabled True
  • End Sub
  • Private Sub btnStop_Click(...) Handles
    btnStop.Click
  • tmrWatch.Enabled False
  • End Sub
  • Private Sub tmrWatch_Tick(...) Handles
    tmrWatch.Tick
  • txtSeconds.Text CStr((CDbl(txtSeconds.Text)
    0.1))
  • End Sub

39
Example 3 Output
Write a Comment
User Comments (0)
About PowerShow.com