Results 1 to 3 of 3

Thread: How do I cancel unwanted keystrokes for a control

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Location
    Woodstock, NB Canada
    Posts
    6

    How do I cancel unwanted keystrokes for a control

    I have textboxes that require only 0123456789 <decimal> <backspace> and <negative> keystrokes.

    I have coded the following but when I press any key within the textbox it does not fire the event.

    And what statement do I write to cancel the keystroke from executing?


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Public Sub txtFahrenheit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFahrenheit.KeyPress
    'Only accept the following keyboard buttons
    ' [0123456789] ; Asc 48-57 respectively
    ' [<backspace>] ; Asc 8
    ' [<decimal>] ; Asc 46
    ' [<negative>] ; Asc 45
    If (((Asc(e.KeyChar) < 45) Or (Asc(e.KeyChar) > 57)) _
    And (Asc(e.KeyChar) <> 8)) _
    Or (Asc(e.KeyChar) = 47) Then Exit Sub

    If Asc(e.KeyChar) = 45 Then
    'Only allow one negative symbol in a textbox (and only in the first position)
    If Len(txtCelsius.Text) <> 0 Then Exit Sub
    End If

    If Asc(e.KeyChar) = 46 Then
    'Only allow one decimal in a textbox
    If InStr(txtCelsius.Text, ".", CompareMethod.Text) <> 0 Then Exit Sub
    End If
    End Sub
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


  2. #2
    New Member
    Join Date
    Oct 2002
    Location
    Cochin,India
    Posts
    2
    Hi
    use 'e.handled = true' instead of 'exit sub'

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

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