Results 1 to 30 of 30

Thread: [2005] MaskEditBox Problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    [2005] MaskEditBox Problem

    Hi Peeps,

    I have wrote a function which validates a masked exit box date format. I have entered into the Mask property 00/00/0000 which on screen translates to __/__/____, great. If I get an invalid date I want to clear the content and reset the focus back giving a message box warning. Here is my code :-

    Code:
            If mskControl.Text.ToString = "" Or mskControl.Text.ToString = sFormat.ToString Then
                sCheckDate = sFormat
                Exit Function
            End If
    
            If Not IsDate(mskControl.Text.ToString) Then
                Beep()
                MessageBox.Show("Invalid date format.", "Validate Date Format", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                sCheckDate = sFormat.ToString            
                mskControl.Focus()
            Else
                sCheckDate = sFormatDate(mskControl.Text.ToString, "dd/MM/yyyy")
            End If
    I should explain that I assign the returned value of this function to the control, i.e. maskedit.text = scheckdate(mskcontrol," / /")

    It all seems to work until I then try to enter a new date. The first number a press seems to push the mask characters to the right and then the rest works. So if I entered 01/01/2009 I end up with _1/01/2009.

    Any ideas please as I thought this would be no time at all to do.

    Thanks,

    Jiggy!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] MaskEditBox Problem

    Why use a MaskedTextBox at all? Why not use a DateTimePicker, which is made for dates. It handles all the validation for you and is far more user-friendly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    I don't want the user to click to select the date. It is far quicker to type in for how they use the system.

  4. #4
    Addicted Member
    Join Date
    Dec 2008
    Posts
    185

    Re: [2005] MaskEditBox Problem

    Maybe this?

    Code:
            If mskControl.Text <> "  /  /" Then
                If Not IsDate(mskControl.Text.ToString) Then
                    MessageBox.Show("Invalid date format.", "Validate Date Format", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                    mskControl.Text = ""
                    e.Cancel = True
                End If
            End If
    ps: msgbox already causes a beep

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    It would seem it is because of the code below which allows the user to use the enter key to move to the next control.

    Code:
        'Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
    
        '    If e.KeyChar = ControlChars.Cr Then
        '        e.Handled = True
        '        SendKeys.Send(ControlChars.Tab)
        '    Else
        '        MyBase.OnKeyPress(e)
        '    End If
    
        'End Sub
    I don't know why, any ideas please?

    Jiggy!

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2005] MaskEditBox Problem

    I don't know if this can cause your problem, but as I've said in another thread (yours? I don't know), you should not use SendKeys to send a Tab to select the next control. There are methods in the .NET framework that can do that for you, use them.
    Using SendKeys for this is just like intercepting every keypress, and then using SendKeys to send the key anyway :S

  7. #7
    Addicted Member
    Join Date
    Dec 2008
    Posts
    185

    Re: [2005] MaskEditBox Problem

    My code above is for the validating event of the masked edit box

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    Hi All,

    Thanks for all your advice, it would seem that I am trying to use old school coding. Can someone give me a pointer into what to use so I can use the enter key to go to the next control.

    Thank you all for your help,

    Jiggy!

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    I have read somewhere about using the selectnextcontrol but you have to pass the next control as a parameter and I just wanted to create a user defined control for the textbox and masktextbox so it would go to the next control using the tab index by pressing enter.

    Regards,

    Jiggy!

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    I give up! This has taken me all morning and all I want to do is use the enter key to navigate.

  11. #11
    Addicted Member
    Join Date
    Dec 2008
    Posts
    185

    Re: [2005] MaskEditBox Problem

    Heres a pointer:

    Code:
        Private Sub Control_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles mskControl.KeyPress, TextBox1.KeyPress, TextBox2.KeyPress
            If Asc(e.KeyChar) = System.Windows.Forms.Keys.Enter Then
                e.Handled = True
                FocusNextControl(sender)
            End If
        End Sub
    
        Private Sub FocusNextControl(ByRef CurrentControl As Control)
            Dim iTargetTabIndex As Integer = CurrentControl.TabIndex + 1
            Dim found As Boolean = False
            Do Until found
                For Each ctl As Control In Me.Controls
                    If ctl.TabIndex = iTargetTabIndex Then
                        found = True
                        ctl.Select()
                    End If
                Next
                iTargetTabIndex = 0
            Loop
        End Sub
    ps: I can't get selectnextcontrol to work either - it always seems to return false

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    Hi Mate,

    I don't mean to sound ungreatful but that seems more over head than using the sendkeys.send(controlchars.tab).

    Regards,

    Jiggy!

    PS. Thanks for the help mate, much appreciated.

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2005] MaskEditBox Problem

    Code:
        Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If e.KeyChar = Chr(Keys.Enter) Then
                Me.SelectNextControl(TextBox1, True, True, True, True)
            End If
        End Sub

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    That works perfect; thanks mate. Can you tell me why to use this method rather than the old sendkeys method?

    Thanks again to you all,

    Jiggy!

  15. #15
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2005] MaskEditBox Problem

    Because I have found SendKeys to be a little unreliable, and because it is old. There is a new method available, which was not created for the fun of it...
    As far as I know SendKeys sends the keypress to the control that has focus. What if the focus switches just before you use SendKeys? You can't control that...

    Using SendKeys to send a tab to set the focus to the next control is just like doing something like this:
    Code:
        Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    
            e.Handled = True
    
            Select Case e.KeyChar
                  Case Chr(Keys.Enter)
                        SendKeys.Send(Keys.Enter)
                  Case Chr(Keys.Control)
                        SendKeys.Send(Keys.Control)
                  Case Chr(Keys.A)
                        SendKeys.Send("A")
                  Case Chr(Keys.B)
                        SendKeys.Send("B")
                  etc...
            End Select
        End Sub
    There is no reason to use SendKeys here. The only time I can think of when you need to use SendKeys is to send a keypress to a different program outside of your control. If the program you want the keypress to receive is under your control, there is always a better way.

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    Hi Mate,

    Just one slight problem I have and that is I wanted to put that routine into a user control which inherits the maskedtextbox control so I don't have to put the code on every control on all my applications and I don't know the control name.

    Cheers for the explanation, I totally agree.

    Regards,

    Jiggy!

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    wups, I think I can use ME is that correct?

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    Nop that does not work if I put it in a user control which inherits a textbox.

  19. #19
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2005] MaskEditBox Problem

    If you use Me.SelectNextControl(...) in your usercontrol, it will look for the next control in your usercontrol, not the next control on the form/panel/tabpage/etc your control is on.

    Instead of using Me.SelectNextControl, you can probably use Me.Parent.SelectNextControl(), but be sure to check if the parent is not nothing first.

    Code:
    If Me.Parent IsNot Nothing Then
       Me.Parent.SelectNextControl(...)
    End If
    (I haven't tested this but I assume it works... SelectNextControl might be a method of the form only, in which case you need to find the ParentForm instead of just the Parent.)

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    Thanks mate but that does not seem to do anything either.

  21. #21

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    Sweet, soz mate I forgot to put the me.parent in. Cheers again for all your help! Jiggy!

  22. #22

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    God no; Hair is being pulled out. On my masktextbox if I clear it because an invalid date has been entered and set the focus back I get the same problem when I try to enter a correct date. It shifts the mask right by one and on the second entry it moves it back and I end up with _2/22/2222 instead of 22/22/2222. The weird thing is if I put the same code from the user control Keypress in to the actual project control / keypress it works fine. I don't think this is going to work for masktextboxes.

    Ahhhhhhhhhhhhhhhhh,

    Jiggy!

  23. #23

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    I think this must be a bug in the masktextbox. I will have to put the same code in my user control in the keypress of each of my masktextboxes. Shame tho!!!

  24. #24
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: [2005] MaskEditBox Problem

    I am also having the same problem. I use this in the validation to check if the social security number length is correct then either give an error message or move on. If they get the message box then it gives focus back to the masked text box and when you type in it it moves the format to the right.

    Code:
    Private Sub mtbSSN_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles mtbSSN.Validated
            
            If mtbSSN.Text.Length = 11 Then
                Add_Validated()
            Else
                MsgBox("The SSN number must be 9 digits", MsgBoxStyle.Exclamation)
                If vbOK Then mtbSSN.Text = Nothing
                mtbSSN.Focus()
            End If
            Exit Sub
    
        End Sub
    
    Private Sub mtbSSN_KeyDown(ByVal sender As System.Object, _
                                   ByVal e As System.Windows.Forms.KeyEventArgs) Handles mtbSSN.KeyDown
    
            If e.KeyCode = Keys.Enter Then
                Me.SelectNextControl(Me.ActiveControl, Not e.Shift, True, True, True)
            End If
    'i had to force the focus to the next control because a 'usercontrol on the form was causing me grief.        
    mtbDOB.Focus()
        End Sub
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] MaskEditBox Problem

    Quote Originally Posted by Always_Confused
    I am also having the same problem. I use this in the validation to check if the social security number length is correct then either give an error message or move on. If they get the message box then it gives focus back to the masked text box and when you type in it it moves the format to the right.

    Code:
    Private Sub mtbSSN_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles mtbSSN.Validated
            
            If mtbSSN.Text.Length = 11 Then
                Add_Validated()
            Else
                MsgBox("The SSN number must be 9 digits", MsgBoxStyle.Exclamation)
                If vbOK Then mtbSSN.Text = Nothing
                mtbSSN.Focus()
            End If
            Exit Sub
    
        End Sub
    
    Private Sub mtbSSN_KeyDown(ByVal sender As System.Object, _
                                   ByVal e As System.Windows.Forms.KeyEventArgs) Handles mtbSSN.KeyDown
    
            If e.KeyCode = Keys.Enter Then
                Me.SelectNextControl(Me.ActiveControl, Not e.Shift, True, True, True)
            End If
    'i had to force the focus to the next control because a 'usercontrol on the form was causing me grief.        
    mtbDOB.Focus()
        End Sub
    You don't use the Validated event for validating. The Validated event is for processing validated input. The Validating event is for actual validating that input. Whether or not that relates to your issue I don't know but it's best to start from the right place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  26. #26
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: [2005] MaskEditBox Problem

    Could you show me a good example? If I enter data into a text box and click or tab to another, Validated I thought was where you would want to check format, length etc. and it goes there automatically. I would appreciate better ideas if this is not proper.
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  27. #27

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    I would say though this will not fix your problem. If I put the same code on my form in my project it works fine. If I use the same code in a user control and start to type I get the format string move to the right and it seems to miss off the key I press.

  28. #28
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: [2005] MaskEditBox Problem

    Well, I have been determined to use the Masked Textbox for my SSN and overcome whatever the world is the problem with the cursor shifting when re-focusing on the box.

    What I found is the problem seems to be in .clear , .text=nothing, and anything else to clear the masked text box. Backspace seems to be the only way to solve it programmatic.

    This is my solution. It may not be pretty but it works.
    Code:
                If Not mtbSSN.Text = "" Then
    
                Dim charcount As Integer = CInt(mtbSSN.Text.Length)
                If mtbSSN.Text.Length < 9 Then
                    MsgBox("The SSN number must be 9 digits", MsgBoxStyle.Exclamation)
                    If CBool(vbOK) Then
                        
                        Dim counter As Integer = 0
    ' The options on the loop count is because of the hyhpen in the mask format "###-##-####" so it take and extra back space to get back to position 0
                        If charcount <= 3 Then
                            Do Until counter = charcount + 1
                                SendKeys.Send(vbBack)
                                counter += 1
                            Loop
                        ElseIf charcount >= 4 Then
                            Do Until counter = charcount + 2
                                SendKeys.Send(vbBack)
                                counter += 1
                            Loop
                            'MsgBox("The loop ran " & charcount & " times.")
    
                        End If
    
                        mtbSSN.Focus()
    
                    End If
                End If
            End If
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

  29. #29

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] MaskEditBox Problem

    Yes your 100% correct with that. I think it loses the position or something when you do it in a user control.

    Thank you for sharing your solution, I will implement that same process for a date format.

    Cheers,

    Jiggy!

  30. #30
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    Re: [2005] MaskEditBox Problem

    Actually my masked textbox is not in a usercontrol, but it is on a tabcontrol. Probably a similar situation.
    If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.

    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison

    Do illiterate people get the full effect of Alphabet Soup?

    ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool

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