Results 1 to 4 of 4

Thread: Can someone take a look at this for me!

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Can someone take a look at this for me!

    I have a simple Calculate Change form that allows a user to enter a price and amount paid, then it calculates the change and breaks it down into dollars, quarters, dimes, etc..

    My problem is on validating the text in the two textboxes (price and paid). I am checking to make sure the text that is entered is numeric (using the Lost_Focus event)...if they try to tab to the next field and their input wasn't numeric it should show a messagebox, which it does, but then it shows the messagebox notice for the field they tabbed to as well....which isn't right...It should show the messagebox, then select all the text in the textbox that didn't have valid input....

    Can someone check this out for me and let me know what I am doing wrong... Thanks!
    Attached Files Attached Files
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can use the Validating event instead and that will work:
    VB Code:
    1. Private Sub txtPrice_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtPrice.Validating
    2.         If (IsNumeric(txtPrice.Text) = False) Then
    3.             e.Cancel = True
    4.             MessageBox.Show("The Price must be numeric.", "Invalid Input")
    5.             txtPrice.Select(0, txtPrice.Text.Length)
    6.         Else
    7.             txtPrice.Text = String.Format("{0:c}", Decimal.Parse(txtPrice.Text))
    8.             txtPaid.Text = "0"
    9.             txtChange.Text = "0"
    10.         End If
    11.     End Sub
    12.  
    13.     Private Sub txtPaid_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtPaid.Validating
    14.         If (IsNumeric(txtPaid.Text) = False) Then
    15.             e.Cancel = True
    16.             MessageBox.Show("The amount Paid must be numeric.", "Invalid Input")
    17.             txtPaid.Select(0, txtPaid.Text.Length)
    18.         Else
    19.             txtPaid.Text = String.Format("{0:c}", Decimal.Parse(txtPaid.Text))
    20.         End If
    21.     End Sub

    The only problem I see with it is if there is invalid text in a textbox and you try to exit it gets held up by the validation. You should be able to use a flag or what not to get around that though.

  3. #3
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    in VB6 we checked the ascii value on the key_press event of the text box. Check to make sure the key pressed is a number, otherwise don't put value in text box.

    Haven't got pc with .net with me at the mo so can't try this.

    hope it helps
    nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  4. #4

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Used your validating idea and modified some of the existing code...works like a charm...Thanks!
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

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