Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLet the user graphically select a range of hours
DescriptionThis example shows how to let the user graphically select a range of hours in Visual Basic 6. It draws a series of hours on a PictureBox and let the user click and drag to select them.
Keywordstime, hours, select hours
CategoriesGraphics, Software Engineering
 
Thanks to Nico Panayi for a bug fix.

The program uses the PictureBox's MouseDown, MouseMove, and MouseUp events to let the user select times.

 
Private Sub TimePic_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    Drawing = True
    
    StartY = YtoHour(Y)
    CurY = StartY
    DrawBox StartY, StartY
End Sub

Private Sub TimePic_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Not Drawing Then Exit Sub
    
    CurY = YtoHour(Y)
    DrawBox StartY, CurY
End Sub

Private Sub TimePic_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim tmp As Integer

    If Not Drawing Then Exit Sub
    Drawing = False
    
    If StartY > CurY Then
        tmp = StartY
        StartY = CurY
        CurY = tmp
    End If
    If CurY >= StartY Then DrawBox StartY, CurY

    ' Bounds checks added by Nico Panayi
    ' (np069@telinco.co.uk)
    If CurY > (num_hours) Then CurY = num_hours
    If StartY < 1 Then StartY = 1

    TimeLabel.Caption = HourNames(StartY) & _
        " to " & HourNames(CurY + 1)
End Sub
 
Subroutine DrawBox shades part of the timeline. Subroutine DrawHours draws the hour names and dividing lines on the PictureBox.
 
' Shade part of the picture.
Private Sub DrawBox(ByVal i1 As Integer, ByVal i2 As _
    Integer)
Dim tmp As Integer
Dim y1 As Single
Dim y2 As Single

    If i1 > i2 Then
        tmp = i1
        i1 = i2
        i2 = tmp
    End If
    
    y1 = TimePic.ScaleHeight * (i1 - 1) / num_hours
    y2 = TimePic.ScaleHeight * i2 / num_hours
    
    TimePic.Cls
    TimePic.Line (0, y1)-(ScaleWidth, y2), vbRed, BF
    DrawHours
End Sub

' Draw hours on the PictureBox.
Private Sub DrawHours()
Dim i As Integer

    ' Draw times and lines on the PictureBox.
    For i = 2 To num_hours
        TimeLine TimePic, i - 1, HourNames(i)
    Next i
End Sub

' Draw a time line on the PictureBox.
Private Sub TimeLine(pic As PictureBox, i As Integer, txt _
    As String)
Dim gap As Single
Dim Y As Single
Dim x1 As Single
Dim x2 As Single
Dim wid As Single

    gap = pic.ScaleX(4, vbPixels, pic.ScaleMode)
    Y = pic.ScaleHeight / num_hours * i
    wid = pic.TextWidth(txt)
    x1 = (pic.ScaleWidth - wid) / 2
    x2 = x1 + wid
    pic.CurrentX = x1
    pic.CurrentY = Y - ScaleY(pic.Font.Size, vbPoints, _
        pic.ScaleMode) / 2
    pic.Print txt

    pic.Line (0, Y)-(x1 - gap, Y)
    pic.Line (x2 + gap, Y)-(pic.ScaleWidth, Y)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated