Document Sample
c2 Powered By Docstoc
					                       More Controls                Chapter 2 P. 1

Introducing more controls (on the Toolbox) (Fig. 2.1)
       - Text box
       - Frame
       - Option button
       - Check box
       - Image

Example P. 44 Figure 2.2 Message formatter
Text boxes    The white box next to “Your Name”
              The white box next to the words “Message”
Frame         The box enclosing the Color buttons
              The box enclosing the Style buttons
Option button The buttons labeled Red, Green, Blue, Black.
Check boxes The boxes labeled Bold, Italic, and Underline.
Image         The picture of the CD at the lower left.
                                                    Chapter 2 P. 2
Labels The label containing “Your Name”
       The box containing “Message”
       The two blank boxes at the bottom of the screen
       The label containing “Click Me”
Command buttons - Display, Clear, Print, Exit

Create the form in the book
Create the labels
       Create the label containing “Your Name”
               - you need not change the Name
               - in Properties change Caption to “Your Name”
       Create the label containing “Message”
               - you need not change the Name
               - in Properties change Caption to “Message”
       Create one blank label at the bottom center of the form
               - in Properties change Name to lblMessage
               - in Properties delete Caption
                                                       Chapter 2 P. 3
       - create the label containing “Click Me”
               - you need not change the Name
               - in Properties change Caption to “Click Me”

Note: You need to change the name of a label if you refer to the label
in the code; i.e., if you place some message in the label.

Create the text boxes
       - create text box by “Name” (user types a name at run time)
                - create a Text box next to “Your Name”
                - in Properties change Name to txtName
                - in Properties delete the Text
       - create text box by “Message” (user types a message at run
                - create a Text box next to “Message”
                - in Properties change Name to txtMessage
                - in Properties delete the Text
                                                       Chapter 2 P. 4
       - center the message in this box (this can be done only at
                                               design time)
               - in Properties set Multiline to True
               - in Properties set Alignment to 2-Center

Note: The Name of a text box begins with txt.

Note: A label box has a Name and Caption.
      A text box has a Name and Text.

Example You can write lblMessage.Caption = txtMessage.Text in
the code. This places the message in the second text box into the
label box. (This will go into the code for the Display button.)

Create the frames (for the Color and Style)
A frame is a control which contains several other controls.
                                                       Chapter 2 P. 5
       - create the Color frame
               - in the Toolbox use the Frame tool to draw a frame
               - in Properties change Name to fraColor
               - in Properties change Caption to Color
       - create the Style frame
               - in the Toolbox use the Frame tool to draw a frame
               - in Properties change Name to fraStyle
               - in Properties change Caption to Style

Create the check boxes for Style
       - create the check box for Bold
               - in the Toolbox use the Check box tool to draw a
                        check box within the Style frame
               - in Properties change Name to chkBold
               - in Properties change Caption to Bold
       - create the check boxes for Italic and Underline
                                                      Chapter 2 P. 6
Note: you must draw the check boxes and option buttons inside the
appropriate frame; you can not create them and then drag them into
the frame.

Place the Image onto the form

The VB folder contains several Images, which you can copy into
your form. Simply use Copy and Paste.

       - in the Toolbox use the Image tool to draw a control for the
       - in Properties change Name to imgCDROM
       - copy in the picture
                - in Properties select Picture (Fig. 2.3- 2.5)
                - find the picture you want (the CD-ROM)
                         - select Program Files, Dev Studio, VB,
                                  Graphics, Icons, Computer, CDROM
                                                         Chapter 2 P. 7
               - double-click on the image
       - copy in the other picture
               - create a new Image frame
               - copy the other image into the frame
       - when both pictures are on the frame, drag one image
               frame on top of the other

Note: copying one picture on top of another will hide one of the
pictures. In the code we will see how to show each of the pictures

       - set other properties
                - enlarge the image
                        - resize the Image control
                        - in Properties set Stretch to True
                - hide the picture
                        - set Visible to False (in the code)
                                                        Chapter 2 P. 8
              - giving a 3-dimensional appearance to controls
                      - in Properties select Borderstyle to 1 - 3D
                      - in Properties select Appearance and 1-3D

Working with multiple controls
Select multiple controls “Draw” a box around the controls with the
cursor or hold the Shift key and click on the controls
Deselecting a group of controls Click outside the selected group
Moving controls as a group Click and drag
Setting properties for multiple controls
Align controls
                                                           Chapter 2 P. 9
Setting properties for user convenience
Defining keyboard access keys           (for command buttons, option
                                        buttons, and check boxes)
       - in Properties put the & symbol before a letter in the
                Caption for a command button
       - the letter will appear underlined on the form
       - the user can hit Alt and that letter to activate the command

Example In the message formatter there is an underline under R in
the word Red. If the user hits Alt R, then the message appears red.

Setting the default and cancel properties
Default- in Properties set Default to True
                - user activates this button with Return
Cancel          - in Properties set Cancel to True
                - user activates this button with Esc
                                                     Chapter 2 P. 10
Set the tab order for controls
        - in Properties select Tabindex
        - type in a number indicating the order in which you want the
                control to be chosen when the program runs
        - Example In Properties set Tabindex to 4. When you run the
                program, if you hit the Tab key, this control will be
                the fourth one to be activated.

Coding for the controls
Clearing text boxes and labels (in code)
       - text box
               - set the Text to “ “
               - Example txtName.text= “ “
               - set the Caption to “ “
               - Example lblMessage.Caption = “ “
                                                    Chapter 2 P. 11

Resetting the focus
Definition The focus is the control which holds the cursor when
the program starts.
Example To set the focus in the Name text box, the command is
txtName.SetFocus. (In the Text formatter, this command occurs in
the code for the Clear control.)

Setting the value properties of option buttons and check boxes
(in code)
Example Check the Red option button
Example Check the Bold check box
        chkBold.Value = 1 OR chkBold.Value = Checked
Example Uncheck the Bold check box
        chkBold.Value = 0 OR chkBold.Value = Unchecked
                                                    Chapter 2 P. 12
Changing the font properties of controls
      - before running the program
              - in Properties select Font
              - in the dialogue box check the desired properties
      - in code
              - assign the appropriate font property and value
                       txtName.Font.Bold = True
                       lblMessage.Font.Size = 12
                       lblTitle.Font.Italic = True

Changing the color of text (in code)
      - set the ForeColor to one of the color constants
                      vbBlack, vbRed, etc. (see P. 55)
                                                      Chapter 2 P. 13
Changing multiple properties of a control
Example Book P. 56
The code       txtTitle.Visible = True
               txtTitle.ForeColor = vbWhite
               txtTitle.Font.Bold = True
               txtTitle.Font.Italic = True
can be rewritten as With txtTitle
                               .Visible = True
                               .ForeColor = vbWhite
                       End With

Concatenating text Use the &     symbol
Example lblMessage.Caption = “Hello” & “ “ & txtMessage.text
                                                 Chapter 2 P. 14
Continuing program lines Use &_
lblMessage.Caption = “If you return the winning number,” &_
                     txtName.text &”, you will be rich!”

Using the value of controls
Example lblMessage.Caption = txtName.text
                                          Chapter 2 P. 15

Objects, Methods, and Properties
Option button


                        Name optRed
User                    Caption Red
                     > Value True

                        optRed_Click ()
                                      Chapter 2 P. 16
Check box


                     Name chkBold
User                 Caption Bold
                  > Value Checked

            Method chkBold_Click ()
                                          Chapter 2 P. 17
 Command button


                       Name cmdDisplay
User                   Caption &Display
                    > cmdDisplay
                                      Chapter 2 P. 18

         Your name

                  Name lblName
User <            Caption Your Name
                                     Chapter 2 P. 19
Text box

                    Name   txtName
User              > Text

Shared By: