Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

prohibit more than one decimal point in text box

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
trawlerman - 20 Aug 2006 21:34 GMT
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
Paul E Collins - 20 Aug 2006 22:02 GMT
> 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.
trawlerman - 20 Aug 2006 22:27 GMT
> 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
[quoted text clipped - 4 lines]
>
> 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
Steve Barnett - 21 Aug 2006 08:24 GMT
>> 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
[quoted text clipped - 12 lines]
>
> 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
trawlerman - 21 Aug 2006 13:26 GMT
Thanks Steve,

A peace of my mind is now at piece.

Colin
Ben Newsam - 21 Aug 2006 15:49 GMT
>A peace of my mind is now at piece.

You may now rest in peas.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.