Cool4 Posted September 13, 2004 Share Posted September 13, 2004 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 More sharing options...
0 alpha_omega Posted September 13, 2004 Share Posted September 13, 2004 I guess there is a something like a key-listener, which monitors key-events like key-pressed or key-released. Search for it a bit in the .NET-Documentation ;) Link to comment Share on other sites More sharing options...
0 Cool4 Posted September 13, 2004 Author Share Posted September 13, 2004 im trying in keydown, keyup and keypress but the eventargs don't have a cancel method Link to comment Share on other sites More sharing options...
0 John Veteran Posted September 13, 2004 Veteran Share Posted September 13, 2004 No, you can't cancel an event. You can capture keypresses and selectively add them to a textbox or something :huh: Link to comment Share on other sites More sharing options...
0 idbuythatforadollar Posted September 13, 2004 Share Posted September 13, 2004 you have to set the ascii = 0 or something Link to comment Share on other sites More sharing options...
0 Bearded Kirklander Posted September 13, 2004 Share Posted September 13, 2004 Can't you use input restrictions? Like masks? Or a conditional to test for ascii values after each keypress? Link to comment Share on other sites More sharing options...
0 Cool4 Posted September 13, 2004 Author Share Posted September 13, 2004 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 More sharing options...
0 Mordkanin Posted September 13, 2004 Share Posted September 13, 2004 (edited) 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 September 13, 2004 by Dayon Link to comment Share on other sites More sharing options...
0 Cool4 Posted September 14, 2004 Author Share Posted September 14, 2004 the e.handled seems to do nothing... so it is still working with other keys :( Link to comment Share on other sites More sharing options...
0 Mordkanin Posted September 14, 2004 Share Posted September 14, 2004 (edited) 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 September 14, 2004 by Dayon Link to comment Share on other sites More sharing options...
0 Cool4 Posted September 15, 2004 Author Share Posted September 15, 2004 well i was using it in a costum control i made.. maybe that's why it didn't work... :s Anyway i'm going to do just what you said. thanks Dayon Link to comment Share on other sites More sharing options...
0 Mordkanin Posted September 15, 2004 Share Posted September 15, 2004 No problem :) Link to comment Share on other sites More sharing options...
Question
Cool4
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