How can a write a filter for a textbox that only accepts numeric input?
I tried the maskedTextBox, but i did not like it.

--
Arne Garvander
Certified Geek
Professional Data Dude

Re: Numeric textbox by Alex

Alex
Thu Feb 28 15:13:27 CST 2008

Hi Arne,

Like as answer: http://www.telerik.com/support/kb/article/b454K-hce-b454T-a-b454c-cbetemdget.aspx
Or simply check you test box changes for digitals: http://www.dotnetspider.com/code/C-550-Numeric-Text-Box-Accept-only-numbers.aspx
(the OnKeyPress method is enough for instance)

Regards, Alex
[TechBlog] http://devkids.blogspot.com



> textbox
>



Re: Numeric textbox by Claes

Claes
Fri Feb 29 03:47:53 CST 2008

Try this:

Public Class NumericTextBox
Inherits TextBox

Private Const ES_NUMBER As Integer = &H2000

Protected Overrides ReadOnly Property CreateParams() As
System.Windows.Forms.CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
params.Style = params.Style Or Win32.Native.ES_NUMBER
Return params
End Get
End Property

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
Boolean
'Prevent pasting of non-numeric characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
(Keys.Control Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String =
CStr(data.GetData(DataFormats.StringFormat, True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not Char.IsNumber(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
End Class

/claes

"Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in message
news:10F4487E-0D71-461E-9682-7AF10EB0EB1F@microsoft.com...
> How can a write a filter for a textbox that only accepts numeric input?
> I tried the maskedTextBox, but i did not like it.
>
> --
> Arne Garvander
> Certified Geek
> Professional Data Dude



Re: Numeric textbox by ArneGarvander

ArneGarvander
Fri Feb 29 09:10:01 CST 2008

Are you missing an import statement? Win32.native does not compile.
--
Arne Garvander
Certified Geek
Professional Data Dude


"Claes Bergefall" wrote:

> Try this:
>
> Public Class NumericTextBox
> Inherits TextBox
>
> Private Const ES_NUMBER As Integer = &H2000
>
> Protected Overrides ReadOnly Property CreateParams() As
> System.Windows.Forms.CreateParams
> Get
> Dim params As CreateParams = MyBase.CreateParams
> params.Style = params.Style Or Win32.Native.ES_NUMBER
> Return params
> End Get
> End Property
>
> Protected Overrides Function ProcessCmdKey(ByRef msg As
> System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
> Boolean
> 'Prevent pasting of non-numeric characters
> If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
> (Keys.Control Or Keys.V) Then
> Dim data As IDataObject = Clipboard.GetDataObject
> If data Is Nothing Then
> Return MyBase.ProcessCmdKey(msg, keyData)
> Else
> Dim text As String =
> CStr(data.GetData(DataFormats.StringFormat, True))
> If text = String.Empty Then
> Return MyBase.ProcessCmdKey(msg, keyData)
> Else
> For Each ch As Char In text.ToCharArray
> If Not Char.IsNumber(ch) Then
> Return True
> End If
> Next
> Return MyBase.ProcessCmdKey(msg, keyData)
> End If
> End If
> Else
> Return MyBase.ProcessCmdKey(msg, keyData)
> End If
> End Function
> End Class
>
> /claes
>
> "Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in message
> news:10F4487E-0D71-461E-9682-7AF10EB0EB1F@microsoft.com...
> > How can a write a filter for a textbox that only accepts numeric input?
> > I tried the maskedTextBox, but i did not like it.
> >
> > --
> > Arne Garvander
> > Certified Geek
> > Professional Data Dude
>
>
>

Re: Numeric textbox by Mr

Mr
Sat Mar 01 17:10:39 CST 2008


"Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in message
news:10F4487E-0D71-461E-9682-7AF10EB0EB1F@microsoft.com...
> How can a write a filter for a textbox that only accepts numeric input?
> I tried the maskedTextBox, but i did not like it.
>

http://msdn2.microsoft.com/en-us/library/ms229644(VS.80).aspx


Re: Numeric textbox by Claes

Claes
Mon Mar 03 04:01:11 CST 2008

Sorry about that, just remove it (ES_NUMBER is defined in the same class
anyway)

/claes

"Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in message
news:942B6B8C-51B5-4A28-91EC-7071B705D472@microsoft.com...
> Are you missing an import statement? Win32.native does not compile.
> --
> Arne Garvander
> Certified Geek
> Professional Data Dude
>
>
> "Claes Bergefall" wrote:
>
>> Try this:
>>
>> Public Class NumericTextBox
>> Inherits TextBox
>>
>> Private Const ES_NUMBER As Integer = &H2000
>>
>> Protected Overrides ReadOnly Property CreateParams() As
>> System.Windows.Forms.CreateParams
>> Get
>> Dim params As CreateParams = MyBase.CreateParams
>> params.Style = params.Style Or Win32.Native.ES_NUMBER
>> Return params
>> End Get
>> End Property
>>
>> Protected Overrides Function ProcessCmdKey(ByRef msg As
>> System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys)
>> As
>> Boolean
>> 'Prevent pasting of non-numeric characters
>> If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
>> (Keys.Control Or Keys.V) Then
>> Dim data As IDataObject = Clipboard.GetDataObject
>> If data Is Nothing Then
>> Return MyBase.ProcessCmdKey(msg, keyData)
>> Else
>> Dim text As String =
>> CStr(data.GetData(DataFormats.StringFormat, True))
>> If text = String.Empty Then
>> Return MyBase.ProcessCmdKey(msg, keyData)
>> Else
>> For Each ch As Char In text.ToCharArray
>> If Not Char.IsNumber(ch) Then
>> Return True
>> End If
>> Next
>> Return MyBase.ProcessCmdKey(msg, keyData)
>> End If
>> End If
>> Else
>> Return MyBase.ProcessCmdKey(msg, keyData)
>> End If
>> End Function
>> End Class
>>
>> /claes
>>
>> "Arne Garvander" <ArneGarvander@discussions.microsoft.com> wrote in
>> message
>> news:10F4487E-0D71-461E-9682-7AF10EB0EB1F@microsoft.com...
>> > How can a write a filter for a textbox that only accepts numeric input?
>> > I tried the maskedTextBox, but i did not like it.
>> >
>> > --
>> > Arne Garvander
>> > Certified Geek
>> > Professional Data Dude
>>
>>
>>