C# Tutorials and offshore development in India

Tutorials Resources Forum Reviews Interview Jobs Projects Training Your Ad Here


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...




Resources » Articles » .NET Framework »

Subclassing a Textbox control to accept only numeric data


Posted Date: 14 Aug 2004    Resource Type: Articles    Category: .NET Framework
Author: Sudip PurkaysthaMember Level: Bronze    
Rating: 1 out of 5Points: 10


Often we may need to restrict users from entering non-numeric data in certain fields. This short code snippets shows how you can sub class a Winforms Textbox control to accept only numbers in a textbox field.



Create a Class named NumbersBox and insert the following code...

Public Class NumbersBox
Inherits System.Windows.Forms.TextBox

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_CHAR
If (m.WParam.ToInt32 > 57 Or m.WParam.ToInt32 < 48) And _
m.WParam.ToInt32 <> 8 Then Return
If Me.ModifierKeys.Control And (m.WParam.ToInt32 = 3 Or m.WParam.ToInt32 = 22 Or _
m.WParam.ToInt32 = 24) Then Return
Case WM_RBUTTONDOWN
Return
End Select
MyBase.WndProc(m)
End Sub
End Class
*******************************************************************************
Create a Module named WindowsMessages and insert the following code...
*******************************************************************************

Module WindowsMessages

Public Const WM_USER As Integer = &H400
Public Const WM_NULL As Integer = &H0
Public Const WM_CREATE As Integer = &H1
Public Const WM_DESTROY As Integer = &H2
Public Const WM_MOVE As Integer = &H3
Public Const WM_SIZE As Integer = &H5
Public Const WM_ACTIVATE As Integer = &H6
Public Const WM_SETFOCUS As Integer = &H7
Public Const WM_KILLFOCUS As Integer = &H8
Public Const WM_ENABLE As Integer = &HA
Public Const WM_SETREDRAW As Integer = &HB
Public Const WM_SETTEXT As Integer = &HC
Public Const WM_GETTEXT As Integer = &HD
Public Const WM_GETTEXTLENGTH As Integer = &HE
Public Const WM_PAINT As Integer = &HF
Public Const WM_CLOSE As Integer = &H10
Public Const WM_QUERYENDSESSION As Integer = &H11
Public Const WM_QUIT As Integer = &H12
Public Const WM_QUERYOPEN As Integer = &H13
Public Const WM_ERASEBKGND As Integer = &H14
Public Const WM_SYSCOLORCHANGE As Integer = &H15
Public Const WM_ENDSESSION As Integer = &H16
Public Const WM_SHOWWINDOW As Integer = &H18
Public Const WM_WININICHANGE As Integer = &H1A
Public Const WM_DEVMODECHANGE As Integer = &H1B
Public Const WM_ACTIVATEAPP As Integer = &H1C
Public Const WM_FONTCHANGE As Integer = &H1D
Public Const WM_TIMECHANGE As Integer = &H1E
Public Const WM_CANCELMODE As Integer = &H1F
Public Const WM_SETCURSOR As Integer = &H20
Public Const WM_MOUSEACTIVATE As Integer = &H21
Public Const WM_CHILDACTIVATE As Integer = &H22
Public Const WM_QUEUESYNC As Integer = &H23
Public Const WM_GETMINMAXINFO As Integer = &H24
Public Const WM_PAINTICON As Integer = &H26
Public Const WM_ICONERASEBKGND As Integer = &H27
Public Const WM_NEXTDLGCTL As Integer = &H28
Public Const WM_SPOOLERSTATUS As Integer = &H2A
Public Const WM_DRAWITEM As Integer = &H2B
Public Const WM_MEASUREITEM As Integer = &H2C
Public Const WM_DELETEITEM As Integer = &H2D
Public Const WM_VKEYTOITEM As Integer = &H2E
Public Const WM_CHARTOITEM As Integer = &H2F
Public Const WM_SETFONT As Integer = &H30
Public Const WM_GETFONT As Integer = &H31
Public Const WM_SETHOTKEY As Integer = &H32
Public Const WM_GETHOTKEY As Integer = &H33
Public Const WM_QUERYDRAGICON As Integer = &H37
Public Const WM_COMPAREITEM As Integer = &H39
Public Const WM_GETOBJECT As Integer = &H3D
Public Const WM_COMPACTING As Integer = &H41
Public Const WM_OTHERWINDOWCREATED As Integer = &H42
Public Const WM_OTHERWINDOWDESTROYED As Integer = &H43
Public Const WM_COMMNOTIFY As Integer = &H44
Public Const WM_WINDOWPOSCHANGING As Integer = &H46
Public Const WM_WINDOWPOSCHANGED As Integer = &H47
Public Const WM_POWER As Integer = &H48
Public Const WM_COPYDATA As Integer = &H4A
Public Const WM_CANCELJOURNAL As Integer = &H4B
Public Const WM_NOTIFY As Integer = &H4E
Public Const WM_INPUTLANGCHANGEREQUEST As Integer = &H50
Public Const WM_INPUTLANGCHANGE As Integer = &H51
Public Const WM_TCARD As Integer = &H52
Public Const WM_HELP As Integer = &H53
Public Const WM_USERCHANGED As Integer = &H54
Public Const WM_NOTIFYFORMAT As Integer = &H55
Public Const WM_CONTEXTMENU As Integer = &H7B
Public Const WM_STYLECHANGING As Integer = &H7C
Public Const WM_STYLECHANGED As Integer = &H7D
Public Const WM_DISPLAYCHANGE As Integer = &H7E
Public Const WM_GETICON As Integer = &H7F
Public Const WM_SETICON As Integer = &H80
Public Const WM_NCCREATE As Integer = &H81
Public Const WM_NCDESTROY As Integer = &H82
Public Const WM_NCCALCSIZE As Integer = &H83
Public Const WM_NCHITTEST As Integer = &H84
Public Const WM_NCPAINT As Integer = &H85
Public Const WM_NCACTIVATE As Integer = &H86
Public Const WM_GETDLGCODE As Integer = &H87
Public Const WM_SYNCPAINT As Integer = &H88
Public Const WM_NCMOUSEMOVE As Integer = &HA0
Public Const WM_NCLBUTTONDOWN As Integer = &HA1
Public Const WM_NCLBUTTONUP As Integer = &HA2
Public Const WM_NCLBUTTONDBLCLK As Integer = &HA3
Public Const WM_NCRBUTTONDOWN As Integer = &HA4
Public Const WM_NCRBUTTONUP As Integer = &HA5
Public Const WM_NCRBUTTONDBLCLK As Integer = &HA6
Public Const WM_NCMBUTTONDOWN As Integer = &HA7
Public Const WM_NCMBUTTONUP As Integer = &HA8
Public Const WM_NCMBUTTONDBLCLK As Integer = &HA9
Public Const WM_KEYFIRST As Integer = &H100
Public Const WM_KEYDOWN As Integer = &H100
Public Const WM_KEYUP As Integer = &H101
Public Const WM_CHAR As Integer = &H102
Public Const WM_DEADCHAR As Integer = &H103
Public Const WM_SYSKEYDOWN As Integer = &H104
Public Const WM_SYSKEYUP As Integer = &H105
Public Const WM_SYSCHAR As Integer = &H106
Public Const WM_SYSDEADCHAR As Integer = &H107
Public Const WM_KEYLAST As Integer = &H108
Public Const WM_INITDIALOG As Integer = &H110
Public Const WM_COMMAND As Integer = &H111
Public Const WM_SYSCOMMAND As Integer = &H112
Public Const WM_TIMER As Integer = &H113
Public Const WM_HSCROLL As Integer = &H114
Public Const WM_VSCROLL As Integer = &H115
Public Const WM_INITMENU As Integer = &H116
Public Const WM_INITMENUPOPUP As Integer = &H117
Public Const WM_MENUSELECT As Integer = &H11F
Public Const WM_MENUCHAR As Integer = &H120
Public Const WM_ENTERIDLE As Integer = &H121
Public Const WM_CTLCOLORMSGBOX As Integer = &H132
Public Const WM_CTLCOLOREDIT As Integer = &H133
Public Const WM_CTLCOLORLISTBOX As Integer = &H134
Public Const WM_CTLCOLORBTN As Integer = &H135
Public Const WM_CTLCOLORDLG As Integer = &H136
Public Const WM_CTLCOLORSCROLLBAR As Integer = &H137
Public Const WM_CTLCOLORSTATIC As Integer = &H138
Public Const WM_MOUSEFIRST As Integer = &H200
Public Const WM_MOUSEMOVE As Integer = &H200
Public Const WM_LBUTTONDOWN As Integer = &H201
Public Const WM_LBUTTONUP As Integer = &H202
Public Const WM_LBUTTONDBLCLK As Integer = &H203
Public Const WM_RBUTTONDOWN As Integer = &H204
Public Const WM_RBUTTONUP As Integer = &H205
Public Const WM_RBUTTONDBLCLK As Integer = &H206
Public Const WM_MBUTTONDOWN As Integer = &H207
Public Const WM_MBUTTONUP As Integer = &H208
Public Const WM_MBUTTONDBLCLK As Integer = &H209
Public Const WM_MOUSELAST As Integer = &H209
Public Const WM_PARENTNOTIFY As Integer = &H210
Public Const WM_ENTERMENULOOP As Integer = &H211
Public Const WM_EXITMENULOOP As Integer = &H212
Public Const WM_MDICREATE As Integer = &H220
Public Const WM_MDIDESTROY As Integer = &H221
Public Const WM_MDIACTIVATE As Integer = &H222
Public Const WM_MDIRESTORE As Integer = &H223
Public Const WM_MDINEXT As Integer = &H224
Public Const WM_MDIMAXIMIZE As Integer = &H225
Public Const WM_MDITILE As Integer = &H226
Public Const WM_MDICASCADE As Integer = &H227
Public Const WM_MDIICONARRANGE As Integer = &H228
Public Const WM_MDIGETACTIVE As Integer = &H229
Public Const WM_MDISETMENU As Integer = &H230
Public Const WM_DROPFILES As Integer = &H233
Public Const WM_MDIREFRESHMENU As Integer = &H234
Public Const WM_CUT As Integer = &H300
Public Const WM_COPY As Integer = &H301
Public Const WM_PASTE As Integer = &H302
Public Const WM_CLEAR As Integer = &H303
Public Const WM_UNDO As Integer = &H304
Public Const WM_RENDERFORMAT As Integer = &H305
Public Const WM_RENDERALLFORMATS As Integer = &H306
Public Const WM_DESTROYCLIPBOARD As Integer = &H307
Public Const WM_DRAWCLIPBOARD As Integer = &H308
Public Const WM_PAINTCLIPBOARD As Integer = &H309
Public Const WM_VSCROLLCLIPBOARD As Integer = &H30A
Public Const WM_SIZECLIPBOARD As Integer = &H30B
Public Const WM_ASKCBFORMATNAME As Integer = &H30C
Public Const WM_CHANGECBCHAIN As Integer = &H30D
Public Const WM_HSCROLLCLIPBOARD As Integer = &H30E
Public Const WM_QUERYNEWPALETTE As Integer = &H30F
Public Const WM_PALETTEISCHANGING As Integer = &H310
Public Const WM_PALETTECHANGED As Integer = &H311
Public Const WM_HOTKEY As Integer = &H312
Public Const WM_PRINT As Integer = &H317
Public Const WM_PRINTCLIENT As Integer = &H318
Public Const WM_PENWINFIRST As Integer = &H380
Public Const WM_PENWINLAST As Integer = &H38F

End Module

*********************************************************************************
*****Finally Create a form.. and insert the Numeric control and enjoy.......





Responses to the resource: "Subclassing a Textbox control to accept only numeric data"
Author: Debasish Bose    15 Aug 2004Member Level: Bronze   Points : 0
The WndProc that .NET provides is just an OOP wrapper around classic WindowProcedure in Win32 development. It's a very low-level or crude way to get the job done. May be your event is executing(although it's checking only WM_CHAR messages) 100 times/sec for all those stupid WM_ messages.


A better way is to override the function OnKeyPress

Improvement : Modify it for double(s) and expose property for "Integer Precesion" and "Decimal Precesion"

Take care.
Bye


Author: Sudip Purkaystha    16 Aug 2004Member Level: Bronze   Points : 0
Debasish, U r sometime true, when u develop a control solely dedicated to your application. But when u r on development of Controls, you need to trap only this WndProc only because, its provides the main thread to handle windows message..Hope u dont like all those WM_messages. But these r the core part of windows messaging.

Anyway i ll look into your valuable suggestion.. Thanks



Author: Debasish Bose    18 Aug 2004Member Level: Bronze   Points : 0
I know all those WM_ messages flowing into a infinite loop that every win32 program establishes and I'm in core win32 development for 4 years. But that's not the issue. In .net developing well-architected controls living in separate assemblies are nothing didfferent than those which are 'internal' controls to a specific project. This point I couldn't understand.
I have published several sophisticated .NET control libraries on .net but I've never overrided a single WndProc. Due to the VM-like runtime host all .NET programs are slower than their C++ conterpart and this simple technique will make them more slower. When you have 30 such numeric textboxes on a typical GUI screen you got a problem.


Author: Prasad    21 May 2005Member Level: Bronze   Points : 0
Nice Article.

Here is simple Technique...

Use JavaScript to-do same job in few lines.

string checkNums ="javascript:if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false; ";
TextBoxName.Attributes.Add("onKeypress",checkNums);

put this code in Page_Event().

you are all set to go.

Thanks,
Prasad


Author: Prasad    21 May 2005Member Level: Bronze   Points : 0
Nice Article.

Here is a simple Technique...

Use JavaScript to-do same job in few lines.

string checkNums ="javascript:if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false; ";
TextBoxName.Attributes.Add("onKeypress",checkNums);

put this code in Page_Event().

you are all set to go.

Thanks,
Prasad


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: A function that Guides you how to execute stored procedure in SQL server 2000 using VB.NET
Previous Resource: How to Display Video files using Vb.Net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources




About Us    Contact Us    Privacy Policy    Terms Of Use