NumericUpDown_TextChanged and _KeyPress events never fires

  • Thread starter Thread starter Robert
  • Start date Start date
R

Robert

I am trying to stop users from typing in non-numeric characters into the
NumericUpDown control in both above mentioned events, for example"

Private Sub updQt_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles updQt.TextChanged

if Not IsNumeric (updQt.Text) then updQt.Text = ""

End Sub

But when I used the on-screen keyboard typing in letters the event did not
never fire at all. The same happened in the KeyPress event.



Any ideas or better way to accomplish that?
 
Well, that's true the TextChanged event never fires. To tell the truth,
it's only supported for TextBoxes (I mean WM_TEXTBOX_TEXTCHANGED).
Keypress event does not work for you because NumericUpDown control
indeed consists of two native controls: TextBox and UpDown Control. And
all keypresses leave in the internal TextBox. What I could suggest you
is to use IMessageFilter from SDF [1] to catch WM_KEYDOWN messages and
just filter it. How it could be done for TextBox control see example
[2]. In your case you have to change base class from TextBox to
NumericUpDown control and modify this code snippet from:

this.Capture = true;
_hwnd = GetCapture();
....

to

this.Capture = true;
_hwnd = GetCapture();
_hwnd = Win32Window.GetWindow(_hwnd,
Win32Window.GetWindowParam.GW_CHILD); // gets child window
....


[1] http://www.opennetcf.org/sdf/
[2] http://www.sergeybogdanov.com/Samples/TextBoxKeyDownFilter.zip

Hope this help,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Back
Top