Results 1 to 12 of 12

Thread: [Help me please!] Textboxes and math

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    36

    [Help me please!] Textboxes and math

    well, I made this thingy, but I'm stuck on making text boxes only accept numbers. I've tried
    Code:
    Private Sub Text1_TextChanged(By Val KeyAscii as integer)
    blah blah blah
    End Sub
    Apparently, KeyAscii does not pick up what ever is happening when I type.

    Also, I want my text boxes to be numbers since they are only to be numbers, but they are picked up as strings. help please!!


    Thanks.

    P.S. If you can, please input the stuffs for me for only one text box. (text1)
    Attached Files Attached Files

  2. #2
    New Member
    Join Date
    May 2008
    Posts
    13

    Re: [Help me please!] Textboxes and math

    I am a noobie myself, but for the second part of your questions here are a couple functions you could try:

    Code:
    Convert.ToInt32(text1) 
    
    CInt(text1)
    The CInt() function will round decimals, but Convert.ToInt32() will throw an exception if there is a decimal.

    Hope this helps you :-)

    Russ

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

    Re: [Help me please!] Textboxes and math

    Not really for the Maths forum but...

    You have two things wrong in your sub declaration, first of all you should use the KeyPress event, and it should be 'ByVal' instead of 'By Val'
    (Might have been a typo, don't know)

    Your KeyAscii should be working now, and you can check if the KeyAscii is numerical, or the delete, backspace keys or whatever keys you need.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [Help me please!] Textboxes and math

    That's a very strange sub. It looks like an event handler, but it isn't a typical one by any stretch, at least not in .NET. Since I'm unclear on the language, I'm not going to suggest a solution, as I don't even remember what a VB6 event handler looks like, so it could be one of those. However, you should really ask a moderator to move this thread to the proper forum, as you will get better responses than this one.
    My usual boring signature: Nothing

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

    Re: [Help me please!] Textboxes and math

    Looks VB6 to me. I think he means:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    ...
    End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    36

    Re: [Help me please!] Textboxes and math

    ok. appearently, the
    Code:
    Private Sub Text1_KeyPress(KeyAscii as Interger)
    ---
    End sub
    that does NOT work, for
    vb 9.0
    and neither does the
    Code:
    Convert.ToInt32(text1) 
    
    CInt(text1)
    . thanks anyway! where's the help section o___o?

    PS: reminder, I'm using vb 9.0 express 2008 Microsoft.

  7. #7
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: [Help me please!] Textboxes and math

    This will work. It allows numbers and decimal points.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If (KeyAscii < 48 Or KeyAscii > 57) Or KeyAscii <> 46 Then
            KeyAscii = 0
        End If
    End Sub

  8. #8
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [Help me please!] Textboxes and math

    That code should allow the user to enter multiple decimal points as well, which won't give correctly-formed results. That case is easy to deal with, though, given the example.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [Help me please!] Textboxes and math

    Since this is .NET 2008, search for that topic in the .NET forum. The question gets answered about once a week or so, and there are a variety of solutions. Pick the one you prefer.

    One option that isn't very good, but is similar to an earlier suggestion here would be to use Double.TryParse (or Integer.TryParse if decimals are not allowed). That would return True if the string was a double, and False if it was not. However, it wouldn't restrict the textbox entries to just numeric characters. It seems that there were a series of answers based on regular expressions, but I haven't dealt with this problem, so I haven't been looking for a solution.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    36

    Re: [Help me please!] Textboxes and math

    hmm. the keyascii one does NOT work. do you think it would be better if I used vb 6.0??

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [Help me please!] Textboxes and math

    It won't be better in VB6, because you'll be trapped into a language that is four versions out of date. You should be asking this in the .NET forum, not here, but here's a link to a thread that may help. The last snippet in the first post should cover most situations for you:

    http://www.vbforums.com/showthread.p...number+textbox
    My usual boring signature: Nothing

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

    Re: [Help me please!] Textboxes and math

    I thought you were talking about VB6 since the syntax looks nothing like VB.NET.
    In VB.NET it's done a tiny bit different.

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