Textbox and overwriting WndProc method

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I have a class that inherits the Textbox class. My main goal is to prevent specific keys from being entered into the textfield. Now, I have a solution however I have one issue with it.
VB.NET:
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_KEYDOWN 'on key press
                Select Case DirectCast(m.LParam.ToInt32, SSKeys)
                    Case SSKeys.Enter
                        MsgBox("Enter from Keyboard pressed")
                    Case SSKeys.NumpadEnter
                        MsgBox("Enter from Keypad pressed")
                    Case SSKeys.Numpad_Add
                        MsgBox("add from keypad pressed")
                    Case Else
                        MyBase.WndProc(m)
                End Select
            Case Else
                MyBase.WndProc(m)
        End Select
this works flawlessly, based on my custom enumerators for the Lparam values of the keys this is able to detect the keydown event of specific keys. Now my issue is being able to prevent that key from entering it's char into the textbox. I thought since I never call the parent/super/base WndProc method the rest of the event would not take place, effectively stopping the key from entering it's data into the field.

Using my logic having the code:
VB.NET:
Case SSKeys.Numpad_Add
                        MsgBox("add from keypad pressed")
would prevent the textbox from inserting the "+" into the field.

So to allow it I would just simply need to do this:
VB.NET:
Case SSKeys.Numpad_Add
                        MsgBox("add from keypad pressed")
                        MyBase.WndProc(m) 'call parent method to finish message
but this is *NOT* the case. So this either means the message is made after the textbox has processed the keydown OR there is another event (keyup?) when the textbox adds the keychar into the textfield.

NOTES:
I have chosen to do it this way because the Keydown/Keypress/Keyup events in a textbox do not allow you to check the exact key (ie the Return key vs. the numpad Enter key. etc etc the numpad + vs a normal keyboard +)
 
The numeric text box you linked to doesn't achieve what I need to achieve

VB.NET:
    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim keyChar As Char = e.KeyChar
        'All control characters are allowed.
        If Not Char.IsControl(keyChar) Then
            'Block the input if it would create invalid text.
            e.Handled = Not Me.ValidatePartialText(keyChar)
            If e.Handled Then
                Me.PlayInvalidInputSound()
            End If
        End If
        MyBase.OnKeyPress(e)
    End Sub

I need the control to be able to distinguish between specific keys. Using the native keypress/keydown events/methods do not offer exact keys. Using those methods I am only able to get the keychar or keycode based from the keys enum, which does not have all keys.
My issue is be able to distinguish between the numpad keys and the normal keyboard keys. numpad4 vs. 4, numpadenter vs. enter etc etc. The only way to tell these keys is to look at the lparam value passed when a key is in the down state. I have been able to figure out specific keys that are pressed but I cannot stop that key from entering its data into the textfield. I need to be able to prevent the message which doesn't appear to work.
 
Ah! Yes thank you! My thinking was some what correct, I was looking into the wrong value, I needed to check the keys on Keypress (WM_CHAR) not keydown (WM_KEYDOWN).
Thank you so much for the helpful link!

However, I'm not sure how to fix the issue when the user presses and holds a key, as that is another event. I'm still looking at the article you linked.
 
Last edited:
After looking into my issue further (when a user keeps a key held down) it appears to register a different lparam value for a key when that happens.

example:
a single keypress from the numpad "+" key:
lparam = 5111809
when the user holds down numpad "+" key
lparam = 1078853633
 
Is that actually significant? Wouldn't it be confusing to have different operations if user pressed this + or the other + ? Many computers don't even have a numeric keyboard. And by the way, they do have different Keys values also.
 
My reason for doing this is not so much to prevent the usage of the numpad, but to limit the user input to only the numpad. Using the KeyDown/KeyUp/KeyPress events do not have specific keys for the other numpad keys (plus, minus, mult, divide, decimal) which is what I need it to filter out. Which makes checking the keycode or keychars through that useless to me. Going through the control messages in order to filter out specific keys and raise custom events seems like my only other option.
 
All those keys have different Keys values, just listen to an event and see what values you get when you press different keys.
 
Back
Top