Numeric Textbox - getting warmer

K

Keith

The following control code - only allows for the entry of
numbers, backspace, and decimals (or periods). The
problem lies in the last statement - it allows multiple
decimals or periods. How would I modify the code to only
allow 1 decimal? In other words - once a user entered one
period - no others could be entered?

Private Overloads Sub TextBox1_TextChanged(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
TextBox1.KeyPress

Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
Dim isDecimal As Boolean = e.KeyChar.ToString = "."
Dim MyChar As String = Chr(8)
Dim isBackspace As Boolean = e.KeyChar.ToString =
MyChar
If Not isKey And Not isDecimal And Not isBackspace
Then
e.Handled = True
End If
End Sub

thank you.
 
C

Cor Ligthert

Herfried,

Is it really necessary to answer questions from yxq, he is now consequently
sending messages with wrong dates. Solex did correct that yesterday with him
and because there were enough messages in that thread which showed that his
date was wrong, that this can be in my opinion not be anymore an accident.

I still am not successfol in changing his date, what is as well for others I
saw, I am currious how Solex did that.

Sorry Keith, I did not wanted this message to stay on top so I used your
thread.

Cor
 
C

Claes Bergefall

If isDecimal AndAlso TextBox1.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If

You do know that not all countries use "." as
the decimal separator, right? Use this constant instead:
System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSe
parator

/claes
 
C

Cor Ligthert

Hi Keith,

I did give you a complete working sample on about 8 days ago did that not
work?

With this you are starting again at the start.

Cor
 
K

Ken Tucker [MVP]

Hi,

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
Dim strDecimal As String
strDecimal =
System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
Dim isDecimal As Boolean = e.KeyChar.ToString = strDecimal
Dim MyChar As String = Chr(8)
Dim isBackspace As Boolean = (e.KeyChar.ToString = MyChar)
Dim boolValidDecimal As Boolean =
(TextBox1.Text.IndexOf(strDecimal) >= 0) And isDecimal
If (Not isKey And Not isDecimal And Not isBackspace) Or
boolValidDecimal Then
e.Handled = True
End If
End Sub

Ken
------------------
 
S

scorpion53061

Is it really necessary to answer questions from yxq, he is now
consequently
sending messages with wrong dates. Solex did correct that yesterday with him
and because there were enough messages in that thread which showed that his
date was wrong, that this can be in my opinion not be anymore an accident.

Cor

This is unnecessary. Herfried does not need you to reprimand him.

If you feel you need to take him to task over a perceived grievance do it
off list.

He is not hurting anyone by responding in this manner.
 
J

Jay B. Harlow [MVP - Outlook]

Keith,
In addition to the other comments, you do know that KeyChar is a Char data
type, that comparing Chars is far more efficient than comparing strings?
Dim isDecimal As Boolean = (e.KeyChar = "."c)
Const Backspace As Char = ChrW(8)
Dim isBackspace As Boolean = (e.KeyChar = Backspace)

The "."c indicates that it is a char literal of a period, not a string
literal of a period. Chr & ChrW returns Chars, Chr is for ANSI character
code points (0 to 255), while ChrW is for Unicode character code points (0
to 64K)

Also I agree with Ken, I would use the
CultureInfo.CurrentCulture.NumberFormat instead of hard coding the decimal
separator (the period)...

Hope this helps
Jay
 
C

Cor Ligthert

Hi Keith,

So much messages than here again the one I putted last last week in a
message and you did not give any reaction anymore on that thread after you
sand the message, it keeps track as well Herfrieds problem about the paste
and about ending with an error. Which was a big discussion in that thread.


Cor
\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.MaxLength = 10
End Sub
Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(TextBox1.Text) Then
If TextBox1.Text.Length > 0 Then
MessageBox.Show("Only numeric is allowed")

TextBox1.SelectionStart = TextBox1.Text.Length - 1
TextBox1.SelectionLength = 1
End If

End If
End If
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("There was an error pasting")
TextBox1.Focus()
e.Cancel = True
End If
End If
End Sub
///
 
C

Cor Ligthert

Hi Scorpion,

The way you send a message is a reprimand. My message is only asking him.
Whatever you think of it, Herfried, knows that. When I real had arguments
with him, I would do that offline.

In addition, especially when I had felt that it was only personally to
Herfried, I can assure you that I did that offline.

By the way, the possibility exist that Herfried did sent the message before
he saw it, because after that he sand a message about the time. This was a
good way to show xyd that this should not be done.

Who gives you the right by the way to reprimand somebody in this newsgroup?

Cor
 
S

scorpion53061

Who gives you the right by the way to reprimand somebody in this newsgroup?

You seem to think you have the right to go after people when you choose. I
don't want to see it or hear about it anymore.

I have stood by and watched you dance on top of people for perceived
violations of etiquette for quite some time now. It is not your place nor is
it needed. Reprimanding someone like Herfried is just plain silly and
unnecessary.
 
C

Cor Ligthert

You seem to think you have the right to go after people when you choose. I
don't want to see it or hear about it anymore.
I am not the captain of this newsgroup, so I cannot command you not to do
that, however there is nobody that told somewhere that you are the captain
of this newsgroup.

When we are talking about rude than I know one, I still remember me you
calling me an amateur in a message although I tried to forget that.
 
S

scorpion53061

I am not the captain of this newsgroup, so I cannot command you not to do
that, however there is nobody that told somewhere that you are the captain
of this newsgroup.

When we are talking about rude than I know one, I still remember me you
calling me an amateur in a message although I tried to forget that.

I never called you an ameatuer at anything. In fact, anyone who has ever
said anything bad of you as a programmer I have defended you to the hilt.

You are correct. No one is the "captain".

What do you mean by "I am not the captain of this newsgroup, so I cannot
command you not to do
that"?

I do not want or need to be captain nor do I need to tell rag on other
posters unnecessarily. I am asking you to do the same...
 
C

Cor Ligthert

I do not want or need to be captain nor do I need to tell rag on other
posters unnecessarily. I am asking you to do the same...

Although I do not understand why you take this initiative to be commander of
this newsgroup.

Show me where I did that with the exception from one person who has done
that so often to me that I told him to pay back with his own methods. (And
that is not in this newsgroup)

Cor
 
S

scorpion53061

Forget it Cor.

You are not "getting it". I am too busy to try and explain it anymore.

Do you know how to make the intellisense in a namespace show the text you
want it to?
 
C

Cor Ligthert

H

Herfried K. Wagner [MVP]

* "scorpion53061 said:
This is unnecessary. Herfried does not need you to reprimand him.

I don't have time to check the date when a message was sent. That's
hard in my newsreader. BTW: I don't remember the names of people
posting to the group too.

Just my 2 Euro cents.
 
H

Herfried K. Wagner [MVP]

* "scorpion53061 said:
Forget it Cor.

You are not "getting it". I am too busy to try and explain it anymore.

Mhm... Both of you post with valid mail addresses, so there should no
problem to discuss that per private mail.
 
C

Cor Ligthert

Hi Herfried,
Mhm... Both of you post with valid mail addresses, so there should no
problem to discuss that per private mail.

You are right with that, why did you not send this to our email by the way.

:)))))))))))))))))))

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top