473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Allow only digits plus decimal separator

Hello all,

I'm a VB6 programmer, and I'm starting to change basic functions to .NET.
I've seen an example on how to allow only digits on a textbox control, using
the Edit Control Styles on a specific class DigitTextBox (code example
below).
There is a constant that behaves the way I want except that it misses the
decimal separator.
Is there any way to do this, using the same method (on VB6 I used the
keypressed event to filter the keys)?

Thank you.
Joćo Araśjo
Private Class DigitTextBox

Inherits TextBox

Public Sub New()

' Get the current style.

Dim style As Integer = _

GetWindowLong(Me.Handle, GWL_STYLE)

' Add ES_NUMBER to the style.

SetWindowLong(Me.Handle, GWL_STYLE, _

style Or ES_NUMBER)

End Sub

' Override the WndProc and ignore the

' WM_CONTEXTMENU and WM_PASTE messages.

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Const WM_CONTEXTMENU As Int32 = &H7B

Const WM_PASTE As Int32 = &H302

If (m.Msg <> WM_PASTE) And (m.Msg <> WM_CONTEXTMENU) Then

Me.DefWndProc(m)

End If

End Sub

End Class

Private WithEvents DigitTextBox1 As DigitTextBox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Create the DigitTextBox.

DigitTextBox1 = New DigitTextBox

DigitTextBox1.Name = "DigitTextBox1"

DigitTextBox1.Location = New Point(TextBox1.Left, Label3.Top)

DigitTextBox1.Size = TextBox1.Size

DigitTextBox1.TabIndex = 2

DigitTextBox1.Text = ""

DigitTextBox1.Font = TextBox1.Font

Me.Controls.Add(DigitTextBox1)

' Set the numeric style for TextBox2.

' Get the current style.

Dim style As Integer = _

GetWindowLong(TextBox2.Handle, GWL_STYLE)

' Add ES_NUMBER to the style.

SetWindowLong(TextBox2.Handle, GWL_STYLE, _

style Or ES_NUMBER)

End Sub

End Class
Nov 21 '05 #1
3 6471
"Joćo Araśjo" <jo***************@nospam.pt> schrieb:
I've seen an example on how to allow only digits on a textbox control,
using the Edit Control Styles on a specific class DigitTextBox (code
example below).
There is a constant that behaves the way I want except that it misses the
decimal separator.
Is there any way to do this, using the same method (on VB6 I used the
keypressed event to filter the keys)?


No, there is no style like 'ES_NUMBER' that will allow the user to enter a
comma. However, note that even 'ES_NUMBER' doesn't prevent the user from
pasting other characters into the textbox. Instead, I suggest to follow the
pattern described in the document referenced below:

Validator Controls for Windows Forms
<URL:http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet04082003.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
May be there is an easier way, but you could use regular expressions too:
i.e.
if regex.ismached(yourstring,"[0-9]([0-9]|\.|,)*")....
"Joćo Araśjo" <jo***************@nospam.pt> schrieb im Newsbeitrag
news:u%****************@TK2MSFTNGP14.phx.gbl...
Hello all,

I'm a VB6 programmer, and I'm starting to change basic functions to .NET.
I've seen an example on how to allow only digits on a textbox control,
using the Edit Control Styles on a specific class DigitTextBox (code
example below).
There is a constant that behaves the way I want except that it misses the
decimal separator.
Is there any way to do this, using the same method (on VB6 I used the
keypressed event to filter the keys)?

Thank you.
Joćo Araśjo
Private Class DigitTextBox

Inherits TextBox

Public Sub New()

' Get the current style.

Dim style As Integer = _

GetWindowLong(Me.Handle, GWL_STYLE)

' Add ES_NUMBER to the style.

SetWindowLong(Me.Handle, GWL_STYLE, _

style Or ES_NUMBER)

End Sub

' Override the WndProc and ignore the

' WM_CONTEXTMENU and WM_PASTE messages.

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Const WM_CONTEXTMENU As Int32 = &H7B

Const WM_PASTE As Int32 = &H302

If (m.Msg <> WM_PASTE) And (m.Msg <> WM_CONTEXTMENU) Then

Me.DefWndProc(m)

End If

End Sub

End Class

Private WithEvents DigitTextBox1 As DigitTextBox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Create the DigitTextBox.

DigitTextBox1 = New DigitTextBox

DigitTextBox1.Name = "DigitTextBox1"

DigitTextBox1.Location = New Point(TextBox1.Left, Label3.Top)

DigitTextBox1.Size = TextBox1.Size

DigitTextBox1.TabIndex = 2

DigitTextBox1.Text = ""

DigitTextBox1.Font = TextBox1.Font

Me.Controls.Add(DigitTextBox1)

' Set the numeric style for TextBox2.

' Get the current style.

Dim style As Integer = _

GetWindowLong(TextBox2.Handle, GWL_STYLE)

' Add ES_NUMBER to the style.

SetWindowLong(TextBox2.Handle, GWL_STYLE, _

style Or ES_NUMBER)

End Sub

End Class

Nov 21 '05 #3
Thank you for the answers.
This is very good way to me so I can follow.
Joćo Araśjo

"Joćo Araśjo" <jo***************@nospam.pt> wrote in message
news:u%****************@TK2MSFTNGP14.phx.gbl...
Hello all,

I'm a VB6 programmer, and I'm starting to change basic functions to .NET.
I've seen an example on how to allow only digits on a textbox control,
using the Edit Control Styles on a specific class DigitTextBox (code
example below).
There is a constant that behaves the way I want except that it misses the
decimal separator.
Is there any way to do this, using the same method (on VB6 I used the
keypressed event to filter the keys)?

Thank you.
Joćo Araśjo
Private Class DigitTextBox

Inherits TextBox

Public Sub New()

' Get the current style.

Dim style As Integer = _

GetWindowLong(Me.Handle, GWL_STYLE)

' Add ES_NUMBER to the style.

SetWindowLong(Me.Handle, GWL_STYLE, _

style Or ES_NUMBER)

End Sub

' Override the WndProc and ignore the

' WM_CONTEXTMENU and WM_PASTE messages.

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Const WM_CONTEXTMENU As Int32 = &H7B

Const WM_PASTE As Int32 = &H302

If (m.Msg <> WM_PASTE) And (m.Msg <> WM_CONTEXTMENU) Then

Me.DefWndProc(m)

End If

End Sub

End Class

Private WithEvents DigitTextBox1 As DigitTextBox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Create the DigitTextBox.

DigitTextBox1 = New DigitTextBox

DigitTextBox1.Name = "DigitTextBox1"

DigitTextBox1.Location = New Point(TextBox1.Left, Label3.Top)

DigitTextBox1.Size = TextBox1.Size

DigitTextBox1.TabIndex = 2

DigitTextBox1.Text = ""

DigitTextBox1.Font = TextBox1.Font

Me.Controls.Add(DigitTextBox1)

' Set the numeric style for TextBox2.

' Get the current style.

Dim style As Integer = _

GetWindowLong(TextBox2.Handle, GWL_STYLE)

' Add ES_NUMBER to the style.

SetWindowLong(TextBox2.Handle, GWL_STYLE, _

style Or ES_NUMBER)

End Sub

End Class

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
4
by: John Bowman | last post by:
Hi, A couple Q's , so I've come to the experts<g>. 1) I need a definitive answer to the following debate. I've got a couple developers who disagree on the following question. We've got an XML...
4
by: italia | last post by:
I changed the Fieldsize Property from text to Long Integer and Decimal Places = 6. I had decimals in the original field. But after the transfer, the digits after the decimals are gone. Now...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
15
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in...
18
by: Kuljit | last post by:
I am doing Engineering(B.Tech) in Computer Science. I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the...
3
by: DustWolf | last post by:
Hello, I am wondering, what is the standard for including decimal numbers in XML code? What determines what is the decimal delimiter and what can be the grouping symbol? I have just realized...
17
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
Hello , Does someone knows a simple way of how to get the nr of fraction digits ? example : 1.23 would give a result of 2 1.234 would give a result of 3 Yes
3
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I have a table column of SQL smallmoney type which I am updating via a form input field defined as decimal. If I enter, say, 51.09 via the decimal input field (representing $51.09), this is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.