473,406 Members | 2,954 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,406 software developers and data experts.

prohibit more than one decimal point in text box

Hi all,
C# newbie after migrating from VB. I wish to have only valid numbers
entered into a txtbox . i.e only one decimal point.

///////////////////My VB code for this is:
Private Sub txtGcost_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8
Case 46
If InStr(txtGcost.Text, ".") 0 Then
'Decimal point already there.
KeyAscii = 0
If Left(txtGcost.Text, 1) = "." Then
txtGcost = "0."
End If
End If
Case Else
KeyAscii = 0
End Select
End Sub

///////////////My C# code is:

private void txtCost_KeyPress(object sender, KeyPressEventArgs e)
{
{
const char Delete = (char)8;
const char dot = (char)46;
string check4dot = ".";
string searchstring = txtCost.Text;
int checkit = searchstring.IndexOf(check4dot);

if (checkit >1 )

{

e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar
!= Delete && e.KeyChar != dot;

Which works fine, except for not allowing no more than one decimal
point,

Thank in advance

Aug 20 '06 #1
5 7673
trawlerman wrote:
C# newbie after migrating from VB. I wish to have
only valid numbers entered into a txtbox . i.e only
one decimal point.
There's much more to it than that. Some foreign cultures use ","
instead of "." as their decimal separator, and there's the issue of
negative numbers (again, not always "-"), and apart from symbology
you've got the question of maximum length, e.g. a string of nine
thousand 8s isn't going to produce a number in any acceptable range.
Additionally, a textbox can change in ways that aren't detected by the
KeyPress event: for example, you might paste text from the Clipboard
using only the mouse.

Your best bet is probably to let them type anything in the textbox,
but handle the TextChanged event and disable the actual OK button - or
whatever they use to proceed - until something valid is in there, with
an appropriate warning displayed. (To test for a valid floating-point
number, use Convert.ToSingle: if it throws an exception [which you can
catch], the number is not valid.)

Eq.
Aug 20 '06 #2

Paul E Collins wrote:
Your best bet is probably to let them type anything in the textbox,
but handle the TextChanged event and disable the actual OK button - or
whatever they use to proceed - until something valid is in there, with
an appropriate warning displayed. (To test for a valid floating-point
number, use Convert.ToSingle: if it throws an exception [which you can
catch], the number is not valid.)

Eq.

Yes , i think that sounds like a more logical approach, and i will
probably go down that road.
But for piece of mind i still need to find a solution.

Thanks for the advice

Colin Williams

Aug 20 '06 #3

"trawlerman" <co**************@msn.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
>
Paul E Collins wrote:
>Your best bet is probably to let them type anything in the textbox,
but handle the TextChanged event and disable the actual OK button - or
whatever they use to proceed - until something valid is in there, with
an appropriate warning displayed. (To test for a valid floating-point
number, use Convert.ToSingle: if it throws an exception [which you can
catch], the number is not valid.)

Eq.


Yes , i think that sounds like a more logical approach, and i will
probably go down that road.
But for piece of mind i still need to find a solution.

Thanks for the advice

Colin Williams
For pease of mind purposes only, how about:

private void textBox_TextChanged(object sender, EventArgs e)
{
string strText = textBox.Text;
string decimalPoint =
System.Globalization.CultureInfo.CurrentCulture.Nu mberFormat.NumberDecimalSeparator;

if (strText.IndexOf(decimalPoint) !=
strText.LastIndexOf(decimalPoint))
MessageBox.Show("Oops");
}

Steve
Aug 21 '06 #4
Thanks Steve,

A peace of my mind is now at piece.
Colin

Aug 21 '06 #5
On 21 Aug 2006 05:26:34 -0700, "trawlerman" <co**************@msn.com>
wrote:
>A peace of my mind is now at piece.
You may now rest in peas.
Aug 21 '06 #6

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

Similar topics

7
by: BA | last post by:
Hello, I have a string with a price in: "$14.95" I need to get it into a decimal. So I regex 'd the string and dumped the $. Debug mon shows a clean string "14.95" Then I do a...
8
by: SAN CAZIANO | last post by:
i have to do in the onkeypress or in onchange the float (real) field validation I try something: function ValidaCampo(nomeCampo,TotInteri,TotDecimali) {...
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...
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...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
3
by: kimi | last post by:
hi all, can anyone tell me how to convert a comp-1 floating point number to a decimal in REXX??
4
by: Rob | last post by:
Using VS 2005 I have an application whereby the user clicks on numbered buttons in order to "build" a number.... (kind of like the calulator application). Example... if they click on the 1...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.