Results 1 to 9 of 9

Thread: A simple Masked Date box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    82

    Post

    I have seen several requests for a masked date box. Here's a simple one for anyone who's interested. It's messy, but feel free to post better ones.

    Make a new form and place a text box on it called Text1

    Code:
    Dim Selected As Integer 'The selected aspect of the date
    Dim curDate As String 'The current text (before invalid keypresses)
    Dim BadKey As Boolean
    
    Private Sub SelectIt()
        'Select a particular aspect of the date
        Select Case (Selected)
        Case 0 'No selection
            Text1.SelStart = 0
            Text1.SelLength = 0
        Case 1 'Day
            Text1.SelStart = 0
            Text1.SelLength = 2
        Case 2 'Month
            Text1.SelStart = 3
            Text1.SelLength = 2
        Case 3 'Year
            Text1.SelStart = 6
            Text1.SelLength = 4
        End Select
    End Sub
    
    Private Function ValidateDate(DateStr As String) As Boolean
        'Returns False for an invalid date, or True for a valid one
        Dim dTemp As Date
        ValidateDate = True
        On Error GoTo BadDate
            'This will cause an error if the date is invalid
            dTemp = Day(DateStr) & "/" & Month(DateStr) & "/" & Year(DateStr)
        On Error GoTo 0
        Exit Function
    BadDate:
        ValidateDate = False
    End Function
    
    Private Sub Form_Load()
        'Put todays date in text1
        Text1.Text = Format$(Date, "DD/MM/YYYY")
        curDate = Text1
        Selected = 0
    End Sub
    
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
        Dim TempVal As Integer
        Dim TempStr As String
            
        SelectIt
        BadKey = False
        Select Case (KeyCode)
            Case vbKeyRight
                curDate = Text1
                If Selected < 3 Then Selected = Selected + 1
            Case vbKeyLeft
                curDate = Text1
                If Selected > 1 Then Selected = Selected - 1
            Case vbKeyEnd
                curDate = Text1
                Selected = 3
            Case vbKeyHome
                curDate = Text1
                Selected = 1
            Case vbKeyUp
                curDate = Text1
                TempVal = Val(Text1.SelText)
                TempVal = TempVal + 1
                TempStr = Trim(Str(TempVal))
                If Len(TempStr) = 1 Then TempStr = "0" & TempStr
                Text1.SelText = TempStr
                If ValidateDate(Text1) = False Then
                    Text1 = curDate
                End If
            Case vbKeyDown
                curDate = Text1
                TempVal = Val(Text1.SelText)
                TempVal = TempVal - 1
                TempStr = Trim(Str(TempVal))
                If Len(TempStr) = 1 Then TempStr = "0" & TempStr
                Text1.SelText = TempStr
                If ValidateDate(Text1) = False Then
                    Text1 = curDate
                End If
            Case Else
                BadKey = True
        End Select
        SelectIt
    End Sub
    
    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
        If BadKey = True Then
            Text1 = curDate
        End If
        SelectIt
    End Sub
    
    Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Text1.SelStart < 3 Then
            Selected = 1
        ElseIf Text1.SelStart > 2 And Text1.SelStart < 6 Then
            Selected = 2
        Else
            Selected = 3
        End If
        SelectIt
    End Sub

  2. #2
    Guest

    Nice code but why not just add the MS Control

    At least with vb5 & 6 you get the MS Masked Edit Control. Why not simple add to your project????

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    82
    I think some people don't like using the masked edit control because they think it's ugly, and another ocx control to add to your setup list. If you're not worried about the look of the masked edit control, then by all means use it.

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    I like the Date/Time Picker control (introduced in VB6) even better!
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    ..dates..

    masked edit is great..but take it further and use
    the calendar or date picker ctrl...I think that's what
    it's called...can't screw up the date with that one.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    82
    The date picker control is very cool, but again, it's an extra 630 Kb to add to your distributed app. If the date part of that set is the only control you use, it's a bit of a waste of resourses, particularly if your end users are downloading your software.

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    yup

    it is..that's why people are buying 20 g hard drives...
    there's room to spare these days and everyone is getting
    in on using some. Look at the size of programs today!!!
    Why not use the best, even it is the biggest!The whole y2k issue started because they didn't want to use the extra 2 digits...space was an issue...look what it cost in the end...a lot more that the extra space would have.

    Go for it..do it big and beautiful!

    Have fun!
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    Guest

    Exclamation Andrew have you thought about this......

    Ok any line of code you include in a project is a potential bug. Therefore aren't you better off using the standard controls, rather than replacing them and possibly introducing additional bugs to your projects?

    Just a thought

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    82
    True, but I'm not worried about the drive space - the database my app uses could easily get up to 50Mb, but that is created by the user, not downloaded. Unless you have a wide bandwidth modem (which many do these days), you don't want to download unnessecarily large files. I want as many people as possible to have access to my software. I guess it's up to the individual developer how they approach the size issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width