 |
|
 |
Thanks for the code, works a treat
But it now will not allow me to paste data into that textbox. I would like to maintain the same rules but paste information into the textbox, any ideas ... ANYONE.... Please
|
|
|
|
 |
|
|
 |
|
 |
Very useful and errorless
Can you add limitation for positive numbers? it's can be a useful feature.
thanks
|
|
|
|
 |
|
 |
Is your code very useful, but is it necessary to control that alone a decimal point is introduced 23.3443 and not 23.34.43 like one can make it?
greetings
|
|
|
|
 |
|
 |
Well I thought that way. However, you may want to change the code to meet your needs.
|
|
|
|
 |
|
 |
Thanks for this, very useful but I have a smallish issue.
The example from the downloaded project does not allow you to enter a decimal the format like .xx but instead 0.xx. Small point I know but many users will input a fraction this way when entering large volumes od data.
|
|
|
|
 |
|
 |
Did you know you can do the following.....
1. Press the 2 number key.
2. Open notepad and press the - key.
3. Copy the "-" text into the clipboard.
4. Past the "-" text just after the 2 in the number edit box...
Chris
|
|
|
|
 |
|
 |
Thanks for notifying. I'm not actively following this project, but I will try to upload an update as soon as I can... by the way, did you find a solution to it yourself?
|
|
|
|
 |
|
 |
As a newbie to VB.net can I just say a BIG thank you for this code. It does exactly what I need.
I had attempted to write a similar routine but mine had loads of code and didn't work very well
I now understand how it can be done much better and simpler by taking a completely different approach.
Thank you so much.
Jem
|
|
|
|
 |
|
 |
Thanks for the complements. I'm glad I've helped you.
Hojjat
|
|
|
|
 |
|
 |
Hi Hojjat
I have a question...
I would like to use this code on a program that has quite a few different forms. On each form there are multiple textboxes, one of the forms has 12 textboxes all requiring the same numeric validation.
Would it be possible to have this code apply globally to all the textboxes on the form rather than having to list each one separately in the sub header?
If so how could I do this.
Even better would be to put the code into a module and have it apply to ALL textboxes in my program as none of them require anything other than numeric input.
Thank you for any help or advice you can give me.
Cheers
Jem
|
|
|
|
 |
|
 |
As I said before, my solution is for use when programmers decide to add this facility to their TextBoxes after they have added several TextBox controls, and added many other lines of code to their project based on these controls names.
However, if you are at the beginning of the design of your forms, it is more appropriate to use a user control which is a textbox accepting numeric values, rather than using the normal textbox control and adding the feature to it as I do in this article.
You can find such controls by searching in the internet, and even in the same codeproject web site:
http://www.codeproject.com/cs/miscctrl/HDNumericTextBox.asp
http://www.codeproject.com/vb/net/2gs_txtbx.asp
|
|
|
|
 |
|
 |
There are some errors when I input minus or paste a string including minus symbol, for example, I can paste "123-456-789" into the control.
|
|
|
|
 |
|
 |
I will fix it as soon as I can.
|
|
|
|
 |
|
 |
But I thought this control was only representing NUMERIC entries.
That kind of number, although made up of numeric symbols is not really a number.
That entry is more akin to needing something like a masked edit control.
My Blog[^] FFRF[^]
-- modified at 17:22 Saturday 25th February, 2006
|
|
|
|
 |
|
 |
>> That kind of number, although made up of numeric symbols is not really a number.
I'm not sure what you mean by the above statement. Afterall, TextBox.Text is always a string, and what I offer here, is to limit this string to contain numeric values only.
|
|
|
|
 |
|
 |
If the aim of your text box is to allow only numeric values only then you have to consider exactly what IS a number.
For example:
$4.00 is a number
50% is a number
2 is a number
5:00pm could also be considered a number
All these above examples are 'numbers' because they represent a numeric AMOUNT type of a construct.
The example that the other person gave of '123-456-789' really does not represent a numeric amount, it represents a pattern of characters that just in this case happen to all be characters that are allowed in numeric display.
Maybe I am over thinking this a bit, and if I am I am sorry to muddy the discussion here, but I think if your controls focus is to only allow numeric values then you have to consider what really IS a numeric value. Not just the individual characters but what they represent as a whole when entered in. This has a larger meaning than just creating a control that only allows numeric type characters in.
If you are only allowing numeric type characters then you have to take into account ALL the different numeric characters and I am not that sure of the overall usefulness of the control at that point.
As one further example... IPV4 addresses allow for 4 groups of numbers, each separated by a '.'. Each characters is considered a numeric type character but as whole and IP address is not considered a numeric value. Should your control allow and validate this as a proper value? If it does then it has moved from being a numeric value text box into the realm of an IP address text box.
Just trying to push home the fact that once you widen the scope of your control very wide then you open yourself up to tons of problems. If a person wants to enter in an IP address then they should use a control specifically for that type of entry. If a person wants to enter a numeric value (dollars, pounds, percents, etc...) then they use yours here. If they want to end in something else then maybe they use another. Just trying to say that you need to keep your scope on point here and be ready and willing to tell people requesting features and pointing out what 'they' think are bugs, that their request doe snot fit the scope of your control and for what reasons you feel that way. This is one reason why there are so many controls out there that end up no good. They try to be all things to all people and in the end never do anything very well.
My Blog[^] FFRF[^]
-- modified at 13:19 Sunday 26th February, 2006
|
|
|
|
 |
|
 |
Well in a mathematical point of view (as far as I know), $4.00 is not a number, but a number and a symbol to its left! Or an IP address is not "a" number, but a set of "four" numbers separated by a decimal sign.
However, in order to make sure such confusion is not going to happen again, I'm modifying the text of article to clearly define what the textboxes are going to do.
I hope my article to be a good start point for others to create their own controls meeting their specific needs.
Thanks for your attention
|
|
|
|
 |
|
 |
Just to add my 2 penneth worth....
$4.00 isnt a number its an amount of currency. 4.00 is the numeric part
Good code piece, perhaps it would be an idea to create a currencyTextBox, percentageTextBox etc??
Anyways, thanks for the code..
|
|
|
|
 |
|
 |
That is a point of view too. Thanks for your complements. I think those kinds of textboxes are made by other people before as seperate user controls. There are numeric textbox user controls doing what I offered here too. What I meant to offer here, was a way to prevent using a user control.
|
|
|
|
 |
|
 |
The source solution won't open in vs2003.
Gary Minor
|
|
|
|
 |
|
 |
VS2005 solutions won't open in 2003. You may still be able to use the classes tho.
Christian Graus - Microsoft MVP - C++
|
|
|
|
 |
|
 |
Exactly. I wrote the code in VB2005 but the class can be used in VS2003 as well. It uses features available in .NET framework 1.0
I'm aware of no 2005 to 2003 convertor, or I'd post a link to it here.
|
|
|
|
 |
|
 |
It works fine but what's happen if there is a coma in the pasted text? You don't get the right number...
can we have theese changes ?
Public Class TextBoxOnPaste
Inherits NativeWindow
Private tb As TextBox
Private Sub New()
End Sub
Public Sub New(ByVal tb As TextBox)
Me.tb = tb
Me.AssignHandle(tb.Handle)
End Sub
Private Const WM_PASTE As Integer = &H302
Protected Overrides Sub WndProc(ByRef m As Message)
Dim SeparateurDecimal As String = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
'With every key press, click, key-combination press, etc a Message is sent to the
' window. You need to read about Win32 programming if you don't understand it.
Select Case m.Msg
Case WM_PASTE
'User has tried a way to paste something, like SHIFT+INSERT or
' right-click context menu...
If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) Then
'What user has tried to paste is a piece of text...
Dim str As String = Clipboard.GetDataObject.GetData(DataFormats.Text)
'si on colle un nombre décimal avec une virgule : ça marche aussi !
If InStr(str, ",") <> 0 Then
Replace(str, ",", SeparateurDecimal)
ElseIf InStr(str, ".") <> 0 Then
Replace(str, ".", SeparateurDecimal)
End If
'si ça commence par le séparateur on ajoute 0
If InStr(str, ",") = 1 Or InStr(str, ".") = 1 Then
str = "0" & str
End If
Dim NewVal As String
NewVal = Mid(tb.Text, 1, tb.SelectionStart) & str & Mid(tb.Text, tb.SelectionStart + tb.SelectionLength + 1, Len(tb.Text))
'NewVal will contain the future value of the textbox, if we would let
' what user wanted to paste to actually paste there...
If IsNumeric(NewVal) Then
' The result will be a numeric value. So go ahead and paste it!
tb.SelectedText = str
End If
' We're done, so we exit the sub (not letting the default WndProc() to
' paste the original string user tried to paste.
Exit Sub
End If
End Select
'In situations other than what we handled, the default WndProc() needs to be run
MyBase.WndProc(m)
End Sub
End Class
et pour la form1 where there are 2 txtbox and one button and one label
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Add onPaste handles when the form loads
Dim onPaste1 As New TextBoxOnPaste(Me.TextBox1)
Dim onPaste2 As New TextBoxOnPaste(Me.TextBox2)
End Sub
Private Sub numericTextboxKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress
Dim tb As TextBox = CType(sender, TextBox)
Dim SeparateurDecimal As String = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
If IsNumeric(e.KeyChar) Then
'If adding the character to the end of the current TextBox value results in
' a numeric value, go on. Otherwise, set e.Handled to True, and don't let
' the character to be added.
e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
ElseIf e.KeyChar = "." Or e.KeyChar = "," Then
e.KeyChar = ChrW(Asc(SeparateurDecimal))
'For the decimal character (.) we need a different rule:
'If adding a decimal to the end of the current value of the TextBox results
' in a numeric value, it can be added. If not, this means we already have a
' decimal in the TextBox value, so we only allow the new decimal to sit in
' when it is overwriting the previous decimal.
'If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then ==> replaced by
If Not (tb.SelectedText = SeparateurDecimal Or IsNumeric(tb.Text & e.KeyChar)) Then
e.Handled = True
End If
ElseIf Not Char.IsControl(e.KeyChar) Then
'IsControl is checked, because without that, keys like BackSpace couldn't
' work correctly.
e.Handled = True
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim result As Double
Dim Nbre1 As Double
Dim Nbre2 As Double
If TextBox1.Text <> "" And TextBox2.Text <> "" Then
Nbre1 = TextBox1.Text
Nbre2 = TextBox2.Text
result = Nbre1 * Nbre2
Label2.Text = result
End If
End Sub
End Class
quisnam igitur sanus ?
|
|
|
|
 |
|
 |
Thanks for your attention.
In order to make the class usage easier for everyone, I changed the class code to remove comas.
|
|
|
|
 |