CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Error trapping

  1. #1
    Join Date
    Oct 2005
    Posts
    3

    Error trapping

    Major NEWBBIE.....

    I am trying to error trap for non-numeric values ("r", "?") in the following text boxes:
    -Degrees_TextBox.Text
    -Minutes_TextBox.Text
    -Seconds_TextBox.Text

    Also, I need to be sure...
    values greater than 90 aren't entered in Degrees_TextBox.Text
    values greater than 60 aren't entered in Minutes_TextBox.Text and Seconds_TextBox.Text

    I considered making the three text boxes a control array, but can't manage to figure that out.

    This is what I have done so far...it only addresses the non-numeric problem:

    Private Sub Degrees_TextBox_Change()

    If IsNumeric(Me.Degrees_TextBox.Text) Then
    DisplayDegree = Me.Degrees_TextBox.Text
    Else
    MsgBox Me.Degrees_TextBox.Text & " Numeric Values Only", vbExclamation, "Warning"
    Me.Degrees_TextBox.Text = ""
    End If

    End Sub

    This code would be duplicated for each textbox.

    It just seems to me I could possibly error trap all three boxes at once.

    Also, when I hit OK on the msgbox I get another msgbox that says "Numeric Values Only"...it ignores the value in MsgBox Me.Degrees_TextBox.Text ...regardless why am I getting two message boxes.

    Suggestions

  2. #2
    Join Date
    Dec 2004
    Posts
    423

    Re: Error trapping

    For each text box use this code in the keypress event:
    Code:
    'Only Allow Numbers
    
        If KeyAscii = 13 Then
            SendKeys "{Tab}"
            Exit Sub
        End If
        If (KeyAscii < 48 And KeyAscii <> 8) Or KeyAscii > 57 Then
            'We are not good
            KeyAscii = 0
        End If
    It will only allow numbers and backspace, 'Enter' acts as Tab.

    On Change Event or LostFocus event use something like this

    Code:
       If val(text1.text) > 90 then
          msgbox "Can't have more than 90 degrees"
       End If
    You can change the 90 to 60 for each other textbox.

  3. #3
    Join Date
    Oct 2005
    Posts
    3

    Re: Error trapping

    Sabin,

    Can you briefly explain what this means. How does this code look into the, for example, Degrees_TextBox text box and make sure it's a numeric value only? Is keypress event the same as click evenT?

    I plugged in the second part of your suggestion and it worked perfectly.

    Thanks.

    For each text box use this code in the keypress event:

    Code:

    'Only Allow Numbers

    If KeyAscii = 13 Then
    SendKeys "{Tab}"
    Exit Sub
    End If
    If (KeyAscii < 48 And KeyAscii <> 8) Or KeyAscii > 57 Then
    'We are not good
    KeyAscii = 0
    End If

    It will only allow numbers and backspace, 'Enter' acts as Tab.

  4. #4
    Join Date
    Dec 2004
    Posts
    423

    Re: Error trapping

    Well if your Degrees_Textbox is named Degrees then
    Code:
    Private Sub Degress_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then
            SendKeys "{Tab}"
            Exit Sub
        End If
        If (KeyAscii < 48 And KeyAscii <> 8) Or KeyAscii > 57 Then
        'We are not good
           KeyAscii = 0
        End If
    End Sub
    All KeyAscii is a code that the computer interputs as each character hit. It is the Ascii value of each key. So by :

    Code:
        If KeyAscii = 13 Then
            SendKeys "{Tab}"
            Exit Sub
        End If
    Your saying if the key = 13 (which is the Enter Key) then do your next part of code.

    And this :
    Code:
        If (KeyAscii < 48 And KeyAscii <> 8) Or KeyAscii > 57 Then
    Is just trapping for numbers. The 'KeyAscii = 0' sends 0 instead of what the user hit if it is not a number.

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Error trapping

    Quote Originally Posted by Sabin_33
    ...
    Code:
    If KeyAscii = 13 Then
    SendKeys "{Tab}"
    Exit Sub
    End If
    Your saying if the key = 13 (which is the Enter Key) then do your next part of code.
    This is a really good one Sabin I ever did it much more complicate!
    To explain it a bit more You are sending 'Tab' after 'Enter' was done. So that will cause the Focus to leave the control and going to the next one which is following in the TabIndex order. Rhenny, You can choose the order in which Focus is given to different controls by setting 'TabIndex' in the properties of each control. Got it ?

    Sabin_33. whats to send if you want to get Shift +Tab for moving backwards true the TabIndexList ?

    Jonny Poet
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  6. #6
    Join Date
    Oct 2005
    Posts
    3

    Re: Error trapping

    Sabin,

    Thanks for the code and explanation.

    RH

  7. #7
    Join Date
    Dec 2004
    Posts
    423

    Re: Error trapping

    Going backwards and hit enter and go
    ' SendKeys "+{Tab}"'
    Last edited by Sabin_33; October 7th, 2005 at 06:07 PM.

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Error trapping

    Quote Originally Posted by Sabin_33
    Going backwards and hit enter and go
    ' SendKeys "{+Tab}"'
    You would not believe it but I never worked with 'SendKeys' because some years ago in MS Access VBA I did and only had troubles with that. I never tried it again. But thats really very practical for using Enter and getting effect of TAB Key.

    So next time its on me to help you


    Jonny Poet
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  9. #9
    Join Date
    Dec 2004
    Posts
    423

    Re: Error trapping

    I think you've helped me plenty on this forum so far johnny! Thanks

  10. #10
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Error trapping

    Quote Originally Posted by Sabin_33
    I think you've helped me plenty on this forum so far johnny! Thanks
    You are everytime welcome

    Jonny Poet
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

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

Featured