ansii and string problem

Hello, I am building a medium difficulty caculator and have finshed it all except for one problem.
I have a text box that displays the caculations and I can type into it using the keyboard. But I only want to be able to type certain ansii codes(only numeric integers) ie 0-9, -, +, and "."
How can I filter out the string so that only those ansii characters can be typed what are the ansii codes for those characters because I don't want to be able to accidently type an "o" instead of a "0"(zero). I am using the mid$ funtion to search the string using a loop but that is about as far as I get. Help would be appreciated.
Thanks,
Korey



Answer this question

ansii and string problem

  • Ayman Badawi

    It will already do something if those keys are pressed. Is that going to keep me from being able to type the other keys. I could already type in 0-9 and -,+,. 



  • yyll

    Hi Paul it looks like I got it now. Thanx again. :-)

  • Yasir Khokhar

    Hi Paul,

    One last thing, The Back button doesn't seem to work in the TextBox but the arrows and Delete do. How can I get the Back button to work in there I've found the VB Character library and tried the System.Windows.Forms.Keys.Back and also just the old VB number for it but it won't work. Here is the code I've got working so far.

    Public Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If e.KeyChar >= "0" And e.KeyChar <= "9" Then

    TextBox1.Show()

    Else : e.Handled = True

    End If

    End Sub



  • Jeff Simmer

    Hi,

    I too am very new to VB and I am looking to do just this with a Textbox except just for just the keys 0 - 9. I understand the code you have just written below but the part I don't know is how or where to put it. lol Can you help me out The Keypress Event is the part I don't get and where to put it. Gotta love newbbies eh ;-)

    Paul Domag wrote:
    Hi,

    You can handle the Keypress Event of the Textbox:
    In your textbox's keypress event try placing this code:

    If e.KeyChar >= "0" And e.KeyChar <= "9" Then
    ' Do something
    ElseIf e.KeyChar = "-" Then
    ' Do something
    ElseIf e.KeyChar = "+" Then
    ' Do something
    ElseIf e.KeyChar = "." Then
    ' Do something
    Else
    e.Handled = True
    End If


    cheers,

    Paul June A. Domag



  • Danieletor

    Hi Paul,

    I can't seem to be able to find that Place on my props. I have the VS 2003, maybe I should have said that first, sorry. :-( Am I looking at something else or do I just not have that lightning icon in my props for VS 2003

    Thanx
    Denis



  • mushy26

    Hi,

    In your IDE, select your textbox. Right click and select properties. The properties window would display. Then, on the properties window click the events button. Its the button that has a lightning icon. ON the list of events, find the Keypress event then double click the empty cell to let the IDE generate it for you. After then you'll have a Keypress event in your code and just place the code theat I have written.

    cheers,

    Paul June A. Domag



  • cshan

    Hi,

    Yes it could keep you from being able to type the other keys. On the example that I posted above, it restricts the user to be able only to type "0-9,-,+,.". If you want to enable other keystrokes just include them in the case statement. If you dont want to display a keystroke then just set the e (event argument) Handled property to true.




    cheers,

    Paul June A. Domag


  • Nick Wang

    Hi,

    You can handle the Keypress Event of the Textbox:
    In your textbox's keypress event try placing this code:

            If e.KeyChar >= "0" And e.KeyChar <= "9" Then
                ' Do something
            ElseIf e.KeyChar = "-" Then
                ' Do something
            ElseIf e.KeyChar = "+" Then
                ' Do something
            ElseIf e.KeyChar = "." Then
                ' Do something
            Else
                e.Handled = True
            End If


    cheers,

    Paul June A. Domag


  • ptah

    what about the textchanged event

  • IDB

    Hi,

    Since you are trying to trap key strokes. Its best that you use the keypress event instead of the textchanged event. Coz keypress event fires before the keystroke is displayed in the textbox. So if you want to block some keys from displaying. Its best to use the keypress event...



    cheers,

    Paul June A. Domag


  • Shy Cohen - MSFT

    Herica!!! I found everything I wanted and it all works the way I wanted it. My TextBox will only accept numbers, no text and the backspace works to delete the last number typed. For anyone that wants the same here is the code I'm using as the keypress handler.

    Public Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If e.KeyChar >= "0" And e.KeyChar <= "9" Then

    TextBox1.Show()

    ElseIf e.KeyChar = Microsoft.VisualBasic.ChrW(8) Then

    TextBox1.ClearUndo()

    Else : e.Handled = True

    End If

    End Sub

    Thank you Paul for all of your help. :-)



  • Kapil Arya

    Hi,

    Hmm. weird. Are you using ASP.net If your using HTML controls instead of Web controls then you couldn't create events for the control. With the exception of creating it in Javascript which also is a very different thing...

    cheers,

    Paul June A. Domag



  • ansii and string problem