home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


MouseDown

The Boolean variable dragging allows the control to tell if the user is currently dragging the mouse to select a new value. This is important for the MouseMove event handlers because they detect mouse movement whenever the mouse crosses the picture box even if no button is pressed.

The MouseDown subroutine begins a dragging operation. First it verifies that the control’s Min value is less than its Max value. Then it makes sure that the control’s Enabled property is true. If either of these conditions is not met, the control cancels the operation.

Next, MouseDown sets dragging to true so that later MouseMove events know the user is changing the control’s value. The routine then updates the control’s Value property. This invokes the corresponding property let procedure that, in turn, calls the Paint subroutine to redisplay the control using the new value. Finally, MouseDown raises a Scroll event to indicate that the value is scrolling. The application can catch this event and perform tasks such as updating label controls to reflect the new value.

Private dragging As Boolean
    :
Private Sub MouseDown(X As Single, Y As Single)
    ‘ Do nothing if there is no range or the
    ‘ control is disabled.
    If (the_min >= the_max) Or (Not Enabled) Then _
        Exit Sub
   
   ‘ Start dragging.
    dragging = True
   
   ‘ Update the value.
    Value = ComputeValue(the_canvas, X, Y)
   
   ‘ Generate a Scroll event.
    RaiseEvent Scroll
End Sub

ComputeValue

The MouseDown subroutine, and the MouseMove and MouseUp routines described in the following sections, use the ComputeValue function to set the control’s new value. This function begins by checking that the control’s Min value is less than the Max value. If it is not, the function arbitrarily returns the Min value.

Next, ComputeValue determines at what fraction of the distance through the control the selected point lies. It uses that fraction to compute the new value. Finally, it ensures that the result remains between the Min and Max values before it returns the result.

Private Function ComputeValue(the_canvas As PictureBox, _
    X As Single, Y As Single) As Long
Dim fract As Single
Dim val As Long
Dim wid As Single
Dim hgt As Single

    ‘ No range. Pick the_min.
     If the_min >= the_max Then
        ComputeValue = the_min
        Exit Function
     End If
   
   ‘ See at what fraction of the way through
    ‘ the control the point lies.
     wid = the_canvas.ScaleWidth
    hgt = the_canvas.ScaleHeight
   
    Select Case the_style
        Case gsLeftToRight
            fract = X / wid
        Case gsRightToLeft
            fract = (wid - X) / wid
        Case gsBottomToTop
            fract = (hgt - Y) / hgt
        Case gsTopToBottom
            fract = Y / hgt
        Case Else
            ComputeValue = the_min
            Exit Function
    End Select
    ‘ Compute the result.
     val = the_min + _
         (the_max - the_min) * fract
   
     ‘ Keep the result in range.
     If val < the_min Then
         val = the_min
    ElseIf val > the_max Then
        val = the_max
    End If
   
    ComputeValue = val
End Function

MouseMove

Subroutine MouseMove is a little simpler than MouseDown. It first checks the Boolean variable dragging to see if the user is changing the control’s value. If it is, the control uses the ComputeValue function to update the control’s Value property. As is the case in subroutine MouseDown, updating the value makes the control redraw using the new value. Finally, MouseMove raises a Scroll event so the application can take action if necessary.

Private Sub MouseMove(X As Single, Y As Single)
    ‘ Do nothing if the user is not selecting
    ‘ a value.
    If Not dragging Then Exit Sub

    ‘ Update the value.
    Value = ComputeValue(the_canvas, X, Y)
   
    ‘ Generate a Scroll event.
     RaiseEvent Scroll
End Sub

MouseUp

Subroutine MouseUp is similar to MouseMove. It, too, does nothing unless the user is changing the control’s value. If the user is changing the value, the routine sets dragging to false to indicate that future mouse events are not related to this value change. It then updates the control’s value with ComputeValue and raises a Change event, indicating that the user has finished the value drag operation.

Private Sub MouseUp(X As Single, Y As Single)
    ‘ Do nothing if the user is not selecting
    ‘ a value.
    If Not dragging Then Exit Sub
    dragging = False
   
    ‘ Update the value.
    Value = ComputeValue(the_canvas, X, Y)
   
    ‘ Generate a Change event.
     RaiseEvent Change
End Sub

Handling Keyboard Events

The bar gauge control handles keyboard events much as it handles mouse events. The KeyDown event handlers for Picture2D and Picture3D both invoke the KeyDown subroutine to do all the actual work.

KeyDown begins by checking the key’s code and the control’s Style to determine how the value should change. For example, if the control’s Style is gsTopToBottom and the user has pressed the down arrow, the subroutine should increase the control’s value toward Max. It will increase by the amount specified by the control’s KeyChangeAmount property.

KeyDown then computes the new value and checks whether it lies between the Min and Max values. This protects the control from situations where the value could move out of bounds. This would happen, for example, if the control’s value was already the same as the Max property and the arrow key would increase the value even more.

If the new value is in bounds, KeyDown updates the Value property. This invokes the Value property let procedure, which redraws the control to display the new value.

Private Sub KeyDown(KeyCode As Integer)
Dim dv As Long
Dim val As Long

    ‘ See by how much the value should change.
     Select Case KeyCode
        Case vbKeyLeft
            If the_style = gsLeftToRight Then
                dv = -value_change
            ElseIf the_style = gsRightToLeft Then
                dv = value_change
            End If
        
        Case vbKeyRight
            If the_style = gsLeftToRight Then
                dv = value_change
            ElseIf the_style = gsRightToLeft Then
                dv = -value_change
            End If
        
        Case vbKeyUp
            If the_style = gsBottomToTop Then
                dv = value_change
            ElseIf the_style = gsTopToBottom Then
                dv = -value_change
            End If
        
        Case vbKeyDown
            If the_style = gsBottomToTop Then
                dv = -value_change
            ElseIf the_style = gsTopToBottom Then
                dv = value_change
            End If
        
        Case Else
            dv = 0
    End Select
   
     ‘ Make the change.
     If dv <> 0 Then
        ‘ Make sure the new value is in bounds.
        val = the_value + dv
        If val >= the_min And val <= the_max Then
            ‘ Update the value.
            Value = val
        
            ‘ Generate a Change event.
             RaiseEvent Change
         End If
     End If
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.