• 0

[vb.net] cancel event


Question

is it possible to cancel an event like in vb6? because i have a textbox and i don't want to allow the user to type certain keys, so i'm trying to cancel the action.

thanks in advance

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

well i was trying to make some kind of mask only for numbers i can get when the user types a number or a letter but i can't cancel when he presses the letter... and e.keycode e.keyascii they're all readonly :(

Link to comment
Share on other sites

  • 0

A few minutes in VS.NET, and I came up with.....

Public Class RestrictiveTextBox
    Inherits System.Windows.Forms.TextBox
    Dim mBadChars() As Char = {}

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If mBadChars.Length = 0 Then Exit Sub
        Dim IsBad As Boolean = False
        For Each x As Char In mBadChars
            If x = e.KeyChar Then IsBad = True
        Next
        e.Handled = IsBad
    End Sub

    Property BadCharacters() As Char()
        Get
            Return mBadChars
        End Get
        Set(ByVal Value As Char())
            mBadChars = Value
        End Set
    End Property
End Class

So you'd have to do something like this on the form:

Me.Textbox1.BadCharacters = New Char() {"E"c, "e"c, "1"c}

Edited by Dayon
Link to comment
Share on other sites

  • 0

You are using the override in an inheritted class, like above, and not right on the Form, right?

You have to use it exactly like I did. (Copy and paste into it's own file is best way)

Here's an example, works perfectly.

Test.zip

Edited by Dayon
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.