Results 1 to 9 of 9

Thread: Visual Basic 2010: Text Box Help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Visual Basic 2010: Text Box Help

    Hi,
    I have recently built my own VB 2010 program from scratch, i am still learning, but in my program, when i press a button i get a error, this is because in my text box i am adding numbers then when i press the button it does a sum and pastes them into another text box, but if i add proper text in there ( which i dont want, it crashes)
    So how do i stop users from entering text in a text box and enter only numbers?
    Thanks for any help

  2. #2

    Default Re: Visual Basic 2010: Text Box Help

    Code:
        Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
                e.Handled = True
                MessageBox.Show("Error!  Please Stick to Numbers")
            End If
        End Sub
    That should work, though I don't know if backspace will work. I can't test it at the moment, but I think that it will. Lemme know if you have any problems with it.

    EDIT:

    OK, I just checked it and it works fine.
    Last edited by Bolkonsky; January 23, 2011 at 07:11 AM.
    Under the Patronage of Leonidas the Lion|Patron of Imperator of Rome - Dewy - Crazyeyesreaper|American and Proud

  3. #3

    Default Re: Visual Basic 2010: Text Box Help

    When i pasted it into the code view, it gave me these errors (attached screenshots)
    Thanks

  4. #4
    Dewy's Avatar Something Witty
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    4,697

    Default Re: Visual Basic 2010: Text Box Help

    The key press handler was never defined and the two other errors stem from that.
    Oh no the picture of my dog disappeared!

  5. #5

    Default Re: Visual Basic 2010: Text Box Help

    Sorry i am new at it, so how do i make it work ?

  6. #6
    Dewy's Avatar Something Witty
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    4,697

    Default Re: Visual Basic 2010: Text Box Help

    You are missing this (below) in form1.designer.vb (you'll have to check your project directory to open that file) EDIT: It goes at the bottom after end sub but before end class.
    Code:
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    However that leads me to believe the name of your textbox isn't textbox1, wait you'll get different errors if that was the case or would you . If that doesn't work upload your project and I'll take a look at it. Please note though my vb.net skills aren't that good, so hopefully what I am telling you is correct
    Oh no the picture of my dog disappeared!

  7. #7

    Default Re: Visual Basic 2010: Text Box Help

    Thanks, ive got it working

  8. #8

    Default Re: Visual Basic 2010: Text Box Help

    Good to hear!

    EDIT:

    I've got a question, Dewy. I'm running Visual Basic 2010, and I tested that script, and it worked perfectly, w/o the Friend WithEvents stuff. Why would it be different from his?
    Last edited by Bolkonsky; January 25, 2011 at 11:34 AM.
    Under the Patronage of Leonidas the Lion|Patron of Imperator of Rome - Dewy - Crazyeyesreaper|American and Proud

  9. #9
    Dewy's Avatar Something Witty
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    4,697

    Default Re: Visual Basic 2010: Text Box Help

    Quote Originally Posted by caius_titus_ceasar View Post
    Thanks, ive got it working
    Good to hear.

    Quote Originally Posted by Bolkonsky View Post
    Good to hear!

    EDIT:

    I've got a question, Dewy. I'm running Visual Basic 2010, and I tested that script, and it worked perfectly, w/o the Friend WithEvents stuff. Why would it be different from his?
    I have no idea.
    Oh no the picture of my dog disappeared!

Posting Permissions

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