Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. I want to enter only Negative values,Positive values,Decimal values in TextBox

I want to enter only Negative values,Positive values,Decimal values in TextBox

Scheduled Pinned Locked Moved C#
csharp
10 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    g_hemanth17
    wrote on last edited by
    #1

    Hi friends, I am using C# in windows based application and i am using one text box in which i want to validate the text box when user enters any Positive Values(+),Negative Values(-) and Decimal Values(.)(only Numeric Values).Can any one send me the code for validating the text box in windows based application using C#.

    hemanth@919866357142

    C X S 3 Replies Last reply
    0
    • G g_hemanth17

      Hi friends, I am using C# in windows based application and i am using one text box in which i want to validate the text box when user enters any Positive Values(+),Negative Values(-) and Decimal Values(.)(only Numeric Values).Can any one send me the code for validating the text box in windows based application using C#.

      hemanth@919866357142

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      There's at least one article on this site that does that, there's also the maskedtextbox if you prefer. You just handle the keypress event, and set it's handled property to reject keys you don't want. Don't forget to check for Char.IsControl.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • G g_hemanth17

        Hi friends, I am using C# in windows based application and i am using one text box in which i want to validate the text box when user enters any Positive Values(+),Negative Values(-) and Decimal Values(.)(only Numeric Values).Can any one send me the code for validating the text box in windows based application using C#.

        hemanth@919866357142

        X Offline
        X Offline
        Xmen Real
        wrote on last edited by
        #3

        Why dont you use "NumericUpDown" control, its what, you want from a textbox

        Becoming Programmer...

        G 1 Reply Last reply
        0
        • X Xmen Real

          Why dont you use "NumericUpDown" control, its what, you want from a textbox

          Becoming Programmer...

          G Offline
          G Offline
          g_hemanth17
          wrote on last edited by
          #4

          Thanks xmen_xwk for your message..but i cannot use NumericUpDown Control here because i want to enter any value in the text box(negative,Positive,Decimal Values from 1 to.... so i cannot specify all these numbers in the NumericUpDown control)..

          hemanth

          S 1 Reply Last reply
          0
          • G g_hemanth17

            Thanks xmen_xwk for your message..but i cannot use NumericUpDown Control here because i want to enter any value in the text box(negative,Positive,Decimal Values from 1 to.... so i cannot specify all these numbers in the NumericUpDown control)..

            hemanth

            S Offline
            S Offline
            Sri_3464
            wrote on last edited by
            #5

            Use the code below. Pass the textbox.Text to this method from the validating or validated or leave event of the textbox public bool IsNumeric(object expression) { double retNum; return Double.TryParse(Convert.ToString(expression), System.Globalization.NumberStyles.Integer,System.Globalization.NumberFormatInfo.InvariantInfo,out retNum ); }

            Thanks, Srikanth

            G 1 Reply Last reply
            0
            • S Sri_3464

              Use the code below. Pass the textbox.Text to this method from the validating or validated or leave event of the textbox public bool IsNumeric(object expression) { double retNum; return Double.TryParse(Convert.ToString(expression), System.Globalization.NumberStyles.Integer,System.Globalization.NumberFormatInfo.InvariantInfo,out retNum ); }

              Thanks, Srikanth

              G Offline
              G Offline
              g_hemanth17
              wrote on last edited by
              #6

              Thanks Srikanth for your code.i tryed it but i didn't get the correct result from your code.can u send me the project file which you did to my mail id .I will check it out..my mail id is g.hemanth17@gmail.com

              Thanks,Hemanth

              X 1 Reply Last reply
              0
              • G g_hemanth17

                Thanks Srikanth for your code.i tryed it but i didn't get the correct result from your code.can u send me the project file which you did to my mail id .I will check it out..my mail id is g.hemanth17@gmail.com

                Thanks,Hemanth

                X Offline
                X Offline
                Xmen Real
                wrote on last edited by
                #7

                buddy you can also write any value in NumericUpDown

                Becoming Programmer...

                1 Reply Last reply
                0
                • G g_hemanth17

                  Hi friends, I am using C# in windows based application and i am using one text box in which i want to validate the text box when user enters any Positive Values(+),Negative Values(-) and Decimal Values(.)(only Numeric Values).Can any one send me the code for validating the text box in windows based application using C#.

                  hemanth@919866357142

                  S Offline
                  S Offline
                  shaz jazz
                  wrote on last edited by
                  #8

                  hi! i have used this chunk of code for taking only positive decimal and interger values, it worked successfully for me. you can modify it according to your need. it is basically keypress event of textbox... private void onKeyPress(object sender, KeyPressEventArgs e) { char[] myNum = new char[12] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\b','.' }; bool blnNumFnd = false; //for keeping track of decimal foreach (char c in myNum) { if (e.KeyChar == myNum[11]) { String txt = ((TextBox)sender).Text; if (!txt.Contains(".")) { blnNumFnd = true; //means its decimal value and user can't enter another decimal again! break; } } else if (e.KeyChar == c) { blnNumFnd = true; break; } } if (!blnNumFnd) e.KeyChar = '\0'; } hope it will work for you!

                  Regards, shazjazz

                  G 2 Replies Last reply
                  0
                  • S shaz jazz

                    hi! i have used this chunk of code for taking only positive decimal and interger values, it worked successfully for me. you can modify it according to your need. it is basically keypress event of textbox... private void onKeyPress(object sender, KeyPressEventArgs e) { char[] myNum = new char[12] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\b','.' }; bool blnNumFnd = false; //for keeping track of decimal foreach (char c in myNum) { if (e.KeyChar == myNum[11]) { String txt = ((TextBox)sender).Text; if (!txt.Contains(".")) { blnNumFnd = true; //means its decimal value and user can't enter another decimal again! break; } } else if (e.KeyChar == c) { blnNumFnd = true; break; } } if (!blnNumFnd) e.KeyChar = '\0'; } hope it will work for you!

                    Regards, shazjazz

                    G Offline
                    G Offline
                    g_hemanth17
                    wrote on last edited by
                    #9

                    Thank you shazjazz..its helped me out...

                    hemanth

                    1 Reply Last reply
                    0
                    • S shaz jazz

                      hi! i have used this chunk of code for taking only positive decimal and interger values, it worked successfully for me. you can modify it according to your need. it is basically keypress event of textbox... private void onKeyPress(object sender, KeyPressEventArgs e) { char[] myNum = new char[12] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\b','.' }; bool blnNumFnd = false; //for keeping track of decimal foreach (char c in myNum) { if (e.KeyChar == myNum[11]) { String txt = ((TextBox)sender).Text; if (!txt.Contains(".")) { blnNumFnd = true; //means its decimal value and user can't enter another decimal again! break; } } else if (e.KeyChar == c) { blnNumFnd = true; break; } } if (!blnNumFnd) e.KeyChar = '\0'; } hope it will work for you!

                      Regards, shazjazz

                      G Offline
                      G Offline
                      g_hemanth17
                      wrote on last edited by
                      #10

                      Hi Shanjazz can u send me the code for entering Positive values and negative values also..i tryed by changing the code but i didn't get the result correctly.I am waiting for your code for entering Negetive,Positive,Decimal values in textbox

                      hemanth

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups