prohibit more than one decimal point in text box

T

trawlerman

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
 
P

Paul E Collins

trawlerman said:
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.
 
T

trawlerman

Paul said:
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
 
S

Steve Barnett

trawlerman said:
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.NumberFormat.NumberDecimalSeparator;

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

Steve
 

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

Similar Threads


Top