Introduction to VB6 Week 7 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup.

Download Report

Transcript Introduction to VB6 Week 7 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup.

4/13/2004

Introduction to VB6

Week 7 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1

Review: String Manipulation Exercise

   You have received a mailing list with names entered in different ways.

There are no middle names in the list, but some names include titles, etc.

 The titles may be:   "Mr.", "Mrs.", "Ms." at the beginning of the string ", Jr." or ", Sr." after the name Names may be entered as  [Title] First Last[Title]  [Title] Last, First[Title] PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 2

Review: String Manipulation Exercise

  Our job is to write a subroutine that receives the name string as one parameter, and outputs the name into four separate TextBoxes  txtPrefix, txtFirst, txtLast, txtSuffix We also need to create a "test harness" for the subroutine  In this case, a project with a form that allows us to type in an arbitrary name entry, calls the subroutine, and contains the TextBoxes for the output.

PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 3

Review: String Manipulation Exercise

  You will at least use Like with pattern matching as well as Instr, Left, and Mid, and you can also make good use of

Select Case

See Sample Project SplitName 2.vbp

for code 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 4

Review: String Manipulation Exercise

 Form Label1(0) txtFullName cmdSplitName txtPrefix txtFirstName txtLastName txtSuffix PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 5

Review: String Manipulation Exercise

 The Code  Starting with cmdSplitText_Click(): Private Sub cmdSplit_Click() txtPrefix.Text = "" txtFirst.Text = "" txtLast.Text = "" txtSuffix.Text = "" SplitName txtFullName.Text

End Sub PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 6

Review: String Manipulation Exercise

 We need a  Sub SplitName(ByVal sFullName As String)  This function will determine what the original string looks like, and split out the different parts.

4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 7

Review: String Manipulation Exercise

 The different "allowed" name layouts       Prefix Prefix Prefix Prefix First Last, Last, First, First Last Last, First First Last, Suffix Last, First, Suffix Suffix Suffix  First Last  Last, First PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 8

Review: String Manipulation Exercise

  Allowed

Prefixes

  Mr.

Mrs.

 Ms.

Allowed

Suffixes

  Jr.

Sr.

PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 9

Review: String Manipulation Exercise

 By using the Pattern Matching, we can simplify this some:  Remember   * matches any string [xy] matches one character that is an x or a y PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 10

Review: String Manipulation Exercise

  So we have the following match conditions  M[rs]. *, [JS]r.

      Mrs. *, [JS]r.

M[rs]. * Mrs. * *, [JS]r.

*, [JS]r.

* * can actually be "First Last" or "Last, First" 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 11

Review: String Manipulation Exercise

 Construct a Select Case statement that tests against those cases using the following approach:  Select Case True   Case sFullName Like "Pattern" … PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 12

Review: String Manipulation Exercise

  Show the initial code sample (NameSplit.vbp) Show the changes to arrive at the final code sample (NameSplit 2.vbp) 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 13

More controls (Option Button, CheckBox, Frame)

 Look at a typical Print dialog box: Option Buttons Checkboxes 4/13/2004 Frames PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 14

More controls (Option Button, CheckBox, Frame)

  A CheckBox can be Checked, Unchecked, and Disabled  Any number of CheckBoxes can be in any state An Option Button can be selected or unselected  Only ONE Option Button within the same container can be selected  Selecting another Option Button will unselect the previously selected Option Button in the group (on the same container) 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 15

More controls (Option Button, CheckBox, Frame)

 Frames are used for "visual grouping" of related input controls.

 Frames are Containers – meaning that you can place other controls on it 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 16

Containers

 Containers are controls (or windows) that can contain other controls (members)  Place a control on a container, and move the container – the member controls follow the container.

 Container Controls:   PictureBox Frame   Tabbed Dialog CoolBar PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 17

Excercise

    Create a New Project (Standard EXE) Place three Option Buttons on the form Run the project   Select one of the option buttons Select another Now stop the application PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 18

Excercise

     Add a Frame to the form Place three Option Buttons on the Frame Drag the frame to a different position Run the project  Select one of the option buttons outside the Frame    Select one of the option buttons inside the Frame Select another option button outside the Frame Select another option button inside the Frame Now stop the application 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 19

Excercise

  Select the Frame in Design view Copy and Paste onto form  Notice how the Member Controls were included 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 20

Excercise

 Option Buttons Best Practice  Name Option Buttons that are in a single group with the same name (i.e. make them a control array)  The easiest way to do this is by creating the first Option Button in the group, naming it and setting any other common properties, and then use Copy & Paste to create the other Option Buttons in the group.

4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 21

Control Arrays

 We already saw an example of a Control Array in the Calculator user interface    Multiple controls (of same type) with the same name, distinguished by an Index property Share a single set of Event Handler procedures with an Index parameter supplied Index does not need to be a set of contiguous values!

4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 22

Control Array Exercise

  Create a New Project Add a Label, a TextBox and a CommandButton    Label Name: "Label1", Caption: "Character" TextBox Name: "txtChar", Text: "" CommandButton Name: "cmdIsBitSet", Caption: "Is Bit n Set?" PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 23

Control Array Exercise

   Add an Option Button  Name: "optBitNo", Caption: "Bit 0" Copy the Option Button and Paste   Answer YES to create a Control Array Move the new Option Button to its location  Modify Caption to "Bit 1" Repeat Paste & Move new option button   Modify Caption to "Bit X" where X is 1 greater than in the previous option button's caption.

Repeat until you have 8 option buttons 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 24

Control Array Exercise

 Add a second label   Name: lblResult Caption: "" 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 25

Control Array Exercise

 Double Click the button in design mode  You should be in the code window inside  Private Sub cmdIsBitSet_Click()   End Sub Enter the code:    If miBitMask and ASC(txtChar.Text) <> 0 Then  lblResult.Caption = "True" Else  lblResult.Caption = "False" End If 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 26

Control Array Exercise

 But we don't have a variable named miBitMask…   Drop down the control selection in code window and select (General) Type in (under Option Explicit):  Private miBitMask As Integer 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 27

Control Array Exercise

  Next, we need some code to set the bitmask: Double click one of the Option Buttons in the form designer, or drop down the control selection combo in the code window and select optBitNo  This brings up the Click event for the options PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 28

Control Array Exercise

 Add the following code: Private Sub optBitNo_Click(Index As Integer) miBitMask = 2 ^ Index End Sub  This results in the bitmask being set when we click one of the options.

PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 29

Control Array Exercise

 Two things remain:   Initializing the bitmask and option Input Validation 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 30

Control Array Exercise

 Initializing the bitmask and option    We should set one of the options as selected By setting it in the Form_Load event (code), we ensure that miBitMask also gets initialized Private Sub Form_Load()  optBitNo(0).Value = True  End Sub PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 4/13/2004 Page 31

Control Array Exercise

 Input Validation   Make sure txtChar.Text is not empty A space is a valid character for this app, so we should not Trim() the text  Private Sub cmdIsBitSet_Click()  If txtChar.Text = "" Then    MsgBox "You must first specify a character"  Exit Sub End If … the remainder of the code is already entered 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 32

Control Array Exercise

     Run the application Enter a character Select a bit number Click "Is Bit N Set?" Try a few subsequent characters with Bit 0 selected   We get TRUE for every one!

We have a BUG in our APP!!!

4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 33

Control Array Exercise

 What is the problem:   Is our miBitMask value incorrect?

Is our IF-test wrong?

4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 34

Control Array Exercise

 It turns out that our IF-test is wrong because of OPERATOR PRECEDENCE.

 If miBitMask And Asc(txtChar.Text) <> 0    Remember: Arithmetic operators are evaluated first, next the Comparison operators, and last the Logical Operators So we get:    1: Asc(txtChar.Text) <> 0 => True 2: miBitmask (=1) And True (-1) => 1 3: 1 is different from False (0), and the expression is therefore considered True Add parentheses to enforce order of operations  If ( miBitMask And Asc(txtChar.Text) ) <> 0 4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 35

Control Array Exercise

  Instead of using the statement miBitMask = 2 ^ Index in the optBitNo_Click event, we could set the Index properties of each of the options to the corresponding value: 1, 2, 4, 8, 16, 32, 64, 128 and use miBitMask = Index Try it!

4/13/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup Page 36