Allow only digits in textbox

0.00 avg. rating (0% score) - 0 votes

The following code apply ES_NUMBER to allow only digits to be typed into the edit control.


Public Const GWL_STYLE As Int32 = (-16)
Public Const ES_NUMBER As Int32 = &H2000


<DllImport(“User32.dll”, EntryPoint:=”GetWindowLong“)> _ Public Function GetWindowLong(ByVal window As IntPtr, ByVal index As Int32) As Int32
End Function


<DllImport(“User32.dll”, EntryPoint:=”GetWindowLong“)> _ Public Function SetWindowLong(ByVal window As IntPtr, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32
End Function

‘accept only digit in the specified textbox
Public Sub SetNumericInputMode(ByVal txtBox As TextBox)
Dim style As Int32 = GetWindowLong(txtBox.Handle, GWL_STYLE)
SetWindowLong(txtBox.Handle, GWL_STYLE, style
Or ES_NUMBER)
End Sub

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>