user187573
user187573

Reputation: 171

Visual basic 6 events

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

Upvotes: 2

Views: 2226

Answers (2)

raven
raven

Reputation: 18135

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

Upvotes: 2

C-Pound Guru
C-Pound Guru

Reputation: 16368

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

Upvotes: 5

Related Questions