Why do I have to use Onkeypress to take control over the Keypress event

fia

New member
Joined
May 7, 2005
Messages
1
Location
Sweden
Programming Experience
1-3
Hi when I'm creating a numeric textbox I have to override the method
Onkeypress to prevent a user of the control to use the control's Keypress
event. For example the users of the control could write code like this.

Private Sub NumericTextbox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles NumericTextbox1.KeyPress
Me.NumericTextbox1.Text = e.KeyChar
End Sub

And this spoils the use of the NumericTextbox

To prevent this code to happen I have to use the code below in the NumericTextbox's class. I'm not sure why I have to do it like this, so if
anyone can explain that for me I'll be greatful. What I meen is why I have to use the method Onkeypress to take control of the Keypress event.

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
If Asc(e.KeyChar) = 8 Then
ElseIf Not IsNumeric(e.KeyChar) Then
e.Handled = True
End If
End Sub

In some books they say I have to have this code also and I don't know why. I
don't notice any difference if I have this line or not.

Public Shadows Event KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs)

And finally I don't understand why I have to override the method Onkeypress,
because when I'm looking at the textbox class in WinCV.exe, I can't find the
method Onkeypress.
To make the NumericTextbox I inherit from System.Windows.Forms.TextBox.

Can anyone help me with these questions.

Thanks for your answers

Fia
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,330
Location
Sydney, Australia
Programming Experience
10+
Each class on which events are raised has to posess methods to process those events. The TextBox class uses OnKeyPress to process the KeyPress event. By overriding that procedure in your inherited class you change the default behaviour from that of the parent class. Without overriding that method your class would respond to the KeyPress event just like a normal TextBox.

When you add an instance of your class to a form, that form can contain an event handler for that particular instance of your class, named by default as something like NumericTextBox1_KeyPress. This will process the KeyPress event for that instance only, after which the class level event handler (OnKeyPress) is called. In the case of the KeyPress event, if the KeyPressEventArgs.Handled property is set to True the class level event handler is never called. The user cannot do this, only the developer. If a developer using your class chooses to do this then they may as well have just used a regular TextBox in the first place.

Adding the line Public Shadows Event Keypress(...) declares your inherited class as responding to the KeyPress event. Because the TextBox class already responds to the KeyPress event and accepts an Object and a KeyPressEventArgs object in the event handler you will not see a difference. If you wanted to add a new event, or change the arguments of an existing event you would have to include that line declaring the event.
 
Top Bottom