Command,Checkbox&Optionbuttons Visual basic 6.0

Embed Size (px)

Citation preview

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    1/35

    COMMAND BUTTON, CHECKBOX TOOL,

    OPTION BUTTON TOOL

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    2/35

    COMMAND BUTTON TOOL

    CHECK BOX TOOL OPTION BUTTON TOOL

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    3/35

    We use, a buttons Caption property to set itscaption.

    This property is available at both design time andruntime.

    We can also change the buttons caption at runtime,of course. As an example, well use our tic-tac-toe

    program from Chapter 1:Private Sub Form_Load()

    xNow = TrueEnd Sub

    Private Sub Command_Click(Index As Integer)If xNow Then

    Command(Index).Caption = "x"

    ElseCommand(Index).Caption = "o"

    End IfxNow = Not xNow

    End Sub

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    4/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    5/35

    We have a property called ForeColor to setthe text in a specified color.

    We can also set the text color through code

    also, forexample,

    Private Sub Command1_click()Check1.ForeColor=RGB(255,0,0)

    End Sub

    Same way we can also set the back color

    through coding with the help of theRGB()

    function, forexample,Command1.BackColor=RGB(255,0,0)

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    6/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    7/35

    Notice that there are number of options in theFont dialog box, which means that you cant seta singleproperty at runtime to set a buttonsfont.

    Instead, we can use the following properties:y

    FontBoldy FontItalicy FontNamey FontSizey FontStrikethruy FontUnderline

    We also have direct access to the buttons Fontobject, so we can set thoseproperties byreferring to them as, forexample,Option1.Font.Bold, Option1.Font.Italic, and soon.

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    8/35

    Werespond to button clicks with the buttons Clickevent.

    To add a Click event handler, just double-click the buttonat design time, which adds a subroutine like this one:

    Private Sub Command1_Click()

    End Sub

    Place the code we want to execute when the button isclicked in this subroutine:Private Sub Command1_Click()

    MsgBox "You clicked the command button!"

    End Sub

    All three buttons have a Click eventthey wouldnt be

    much use otherwiseand option buttons also have adouble-click event, DblClick.

    If you double-click a checkbox, we select and thendeselect it

    If you double-click an option button, however, we selectit, no matter what its original state, and cause a DblClick

    event.

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    9/35

    To create a control array, just give two controls ofthe same type the same name (in theNameproperty); when you do, Visual Basic will ask if you

    want to create a control array.

    We use a control array and oneevent handler

    function (the control array index of the button thatwas clicked is passed to theevent handler, so you

    can tell which button you need to respond to).

    When you create an event handler subroutine for a

    button in the control array, Visual Basic willautomatically pass the index of the control in the

    control array to that subroutine:

    Private Sub GamePiece_Click(Index As Integer)

    End Sub

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    10/35

    We do that with the controls SetFocus() method,which is something you frequently do in real

    programs after button clicks.

    Heres how it might look in code:

    Private Sub Command1_Click()RichTextBox1.UpTo (gstrStringToFind)

    RichTextBox1.SetFocus

    End Sub

    Now, when the user clicks the button and starts

    typing again, the focus will be back on therichtext box, as it should be.

    Note that you can set the control that has the

    focus when a form first appears by setting the

    controls Default property to True

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    11/35

    We can give menu items access characters

    those underlined characters in a menu item that

    the user can reach with the Alt key.

    Just place an ampersand (&) in front of the

    character in the buttons caption that you want

    to make into the access character for that

    button

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    12/35

    To make your buttons more accessible from thekeyboardespecially if weve got a lot of them

    you can use theTabStop, TabIndex, and Defaultproperties.

    Heres what thoseproperties do: TabStop indicates if this button can accept the focus whenthe user tabs to it.

    TabIndex is the index of the current button in the tab order(starts at 0).

    Default is True for one control on a form only; that controlwill have the focus when the form first appears

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    13/35

    We can disable the button by setting its

    Enabled property to False when itsinappropriate to use that button.

    We can also disable buttons at runtime, ofcourse, like this:

    Private Sub Command1_Click()

    Command1.Enabled = False

    End Sub

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    14/35

    To make a button disappear, just set its Visibleproperty to False.

    To make it reappear, set theVisible property toTrue.

    In runtime also we can disable is, forexamplePrivate Sub Command1_Click()

    Command1.Visible= False

    End Sub

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    15/35

    We can do it using ToolTipText property forbuttons

    We just place the text we want to appear in the

    tool tip into theToolTipText property to create a

    tool tip for the button.We can also set tool tip text at runtime, using

    theToolTipText property this way in code:Private Sub Command1_Click()

    Command1.ToolTipText ="You already clicked me!"

    End Sub

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    16/35

    We can do it using theTop, Left, Height, andWidth properties, or theMove method. Hereswhat thoseproperties hold:Left holds the horizontal coordinate of the upper left ofthe button.

    Top holds the vertical coordinate of the upper left of thebutton.

    Height holds the buttons height.

    Width holds the buttons width.

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    17/35

    Using thePicture property, we can load animage into a buttonjust click the button

    with theellipsis () in thePicture propertys

    entry in the Properties window and indicatean image file in the Load Picture dialog box

    that opens.

    Thats not all, howeverwe also have to set

    the buttons Style property to Graphical.Weve loaded an image into a command

    button

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    18/35

    We can use theLoad statement to load new buttons iftheyrepart of a control array.

    To see how this works, add a new button to a form,

    giving it the name, say, Command.

    To make it the first member of a control array, set itsIndex property to 0.

    Now when the user clicks this button, we can add a

    new button of the same type to the form with Load.

    Here, we load Command(1), becauseCommand(0) isalready on the form:

    Private Sub Command_Click(Index As Integer)

    Load Command(1)

    End Sub

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    19/35

    Weve got 200 buttons in your new program, and

    each one has to be initialized with a long series ofcode statements.

    We can organize it easily.

    We can pass the buttons to a procedure and place

    the initialization code in that procedure.Heres an example.

    Private Sub Command1_Click()

    SetCaption Command1

    End Sub

    In theSetCaption() procedure, you just declare thebutton as a parameter; well name that parameter

    Button and make it of typeControl:Private Sub SetCaption(Button As Control)

    End Sub

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    20/35

    We can see if a checkbox is checked by examiningits Value property.

    Here are thepossibleValue settings forcheckboxes:

    0 Unchecked

    1 Checked

    2 Grayed

    Heres an example:Private Sub Command1_Click()

    If Check1.Value= 1 Then

    Command1.Caption ="The check mark is checked"

    End If

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    21/35

    We can set a checkboxs state by setting its Valueproperty to one of the following:

    0Unchecked

    1Checked

    2GrayedHeres an example; In this case, we check a

    checkbox, Check1, from code:Private Sub Command1_Click()

    Check1.Value= 1End Sub

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    22/35

    We can use frame control to group option

    buttons together. Just draw a frame foreach group of option

    buttons we want on a form and add the option

    buttons to the frames.

    Each frame of option buttons will act as its owngroup, and the user can select one option button

    in either group.

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    23/35

    You can check if an option button is selected

    or not with theValue property.

    Unlike checkboxes, which have three settings

    for theValue property (corresponding tochecked, not checked, and grayed), option

    buttons Value property only has twosettings:

    True if the button is selected, and False if

    not.

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    24/35

    We add an image to a button by connecting animage (as from an image file) to its Pictureproperty.

    When were working with checkboxes and

    option buttons, we should also set the buttonsDownPicture property to specify what image itshould display when selected.

    Graphical checkboxes and option buttons look

    like image-bearing command buttons, notstandard checkboxes and option buttons.

    The only way we tell them apart from command

    buttons when theprogram runs is that

    checkboxes and option buttons, when clicked,

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    25/35

    After this we must also set theSTYLE propertyof the option button to True

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    26/35

    Using frames we can also use both check boxes and

    option boxes together

    Forexample

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    27/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    28/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    29/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    30/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    31/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    32/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    33/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    34/35

  • 8/3/2019 Command,Checkbox&Optionbuttons Visual basic 6.0

    35/35