Visual basic 6 events

Tag: vb6 Author: symale Date: 2009-09-28

How can i restrict an event to occur? Suppose i don't want textbox change event to occur when i press backspace.

Other Answer1

Setting KeyAscii=0 in the KeyPress event will cause the keypress to be ignored.

Private Sub myTextBox_KeyPress(KeyAscii As Integer)
   If KeyAscii = vbKeyBack Then KeyAscii = 0
End Sub

comments:

This is the simplest way to deactivate a key for a textbox control

Other Answer2

Since the Change event doesn't pass you the code of the last key pressed, you'll have to store that in the KeyPress event, then you can immediately exit the Change event whenever the backspace key is pressed.

Private keyCode As Integer

Private Sub Text1_Change()

  If (keyCode = vbKeyBack) Then
    Exit Sub
  Else
    // do whatever it is you want to do in this event
    // P.S.: I know this is the wrong comment syntax,
    // but this code prettifier has a problem with 
    // VB6 comments 
  End If

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

  keyCode = KeyAscii

End Sub