6 Replies - 10726 Views - Last Post: 06 August 2011 - 12:54 AM Rate Topic: -----

#1 beg.saiful  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 11

Keypress event in TextBox for Decimal with precision two

Posted 04 August 2011 - 04:55 AM

i tried to entered a decimal number with precision two. like i want to enter 12.36 so initially in the textbox value is 0.00 now when i'm pressing the numbers 12.36 it should take the number accordingly.. i validate also for not allowing more than two dot's. the problem i'm facing is when i'm using number pad i.e.s. numpad0---numpad9 then that number is not taking in the text box if i press the number above the "qwerty" keys in the keyboard its taking...

i'm giving my code below...let me know where i missed the things

string str="";
        private void textBox31_KeyDown(object sender, KeyEventArgs e)
        {
           
    

            int KeyCode = e.KeyValue;

            

            if (!IsNumeric(KeyCode))
            {
                e.Handled = true;
                return;
            }
            else
            {
                e.Handled = true;
            }
            if (((KeyCode == 8) || (KeyCode == 46)) && (str.Length > 0))
            {
                str = str.Substring(0, str.Length - 1);
            }
            else if (!((KeyCode == 8) || (KeyCode == 46)))
            {
                str = str + Convert.ToChar(KeyCode);
            }
            if (str.Length == 0)
            {
                textBox31.Text = "0.00";
            }
            if (str.Length == 1)
            {
                textBox31.Text = "0.0" + str;
            }

            else if (str.Length == 2)
            {
                textBox31.Text = "0." + str;
            }
            else if (str.Length > 2)
            {
                textBox31.Text = str.Substring(0, str.Length - 2) + "." + str.Substring(str.Length - 2);
            }
        }
        private bool IsNumeric(int Val)
        {
            return ((Val >= 48 && Val <= 57)  || (Val == 8) || (Val == 46));
           
        }

        private void textBox31_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
         
    

        }


This post has been edited by stayscrisp: 04 August 2011 - 06:31 AM
Reason for edit:: Use code tags!!


Is This A Good Question/Topic? 0
  • +

Replies To: Keypress event in TextBox for Decimal with precision two

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 296
  • View blog
  • Posts: 1,537
  • Joined: 07-April 08

Re: Keypress event in TextBox for Decimal with precision two

Posted 04 August 2011 - 05:31 AM

Did you check to make sure your numlock was on? I don't see anything obvious other then numlock that would stop you from entering in numbers with the keypad.
Was This Post Helpful? 0
  • +
  • -

#3 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Keypress event in TextBox for Decimal with precision two

Posted 04 August 2011 - 05:33 AM

Take a look at MaskedTextBox:
http://msdn.microsof...kedtextbox.aspx
Different Masks:
http://msdn.microsof...xtbox.mask.aspx

It might be what you are looking for. This mask might suit your needs:
99.99
Or if the input can be bigger than 12.34 like 12345.67, then:
99999999.99
Was This Post Helpful? 2
  • +
  • -

#4 beg.saiful  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 11

Re: Keypress event in TextBox for Decimal with precision two

Posted 04 August 2011 - 05:35 AM

View Postrgfirefly24, on 04 August 2011 - 05:31 AM, said:

Did you check to make sure your numlock was on? I don't see anything obvious other then numlock that would stop you from entering in numbers with the keypad.



numlock is on.. but still not working


if i used the same code in key press event then...

its taking ..but there is also an another problem like if i delete the enetered value and put a new value then the old value will come first then the new one


code is as follow...
 private bool IsDigit(char Val)
        {


            return ((Val >= 48 && Val <= 57) || (Val == 8) || (Val == 46) || Val ==127);

        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
         char  KeyChar = e.KeyChar ;

            if (!char.IsDigit (KeyChar))
            {
                e.Handled = true;
                return;
            }

            
            else
            {
                e.Handled = true;
            }
            if (((KeyChar == 8) || (KeyChar == 46)) && (str.Length > 0))
            {
                str = str.Substring(0, str.Length - 1);
            }
            else if (!((KeyChar == 8) || (KeyChar == 46)))
            {
                str = str + Convert.ToChar(KeyChar);
            }
            if (str.Length == 0)
            {
                textBox1.Text = "0.00";
            }
            if (str.Length == 1)
            {
                textBox1.Text = "0.0" + str;
            }

            if (str.Length == 2)
            {
                textBox1.Text = "0." + str;
            }
            else if (str.Length > 2)
            {

                textBox1.Text = str.Substring(0, str.Length - 2) + "." + str.Substring(str.Length - 2);
            }




}


This post has been edited by stayscrisp: 04 August 2011 - 06:31 AM
Reason for edit:: Use code tags!!

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,214
  • Joined: 02-June 10

Re: Keypress event in TextBox for Decimal with precision two

Posted 04 August 2011 - 06:19 AM

First use the code tags as we have posted everywhere including in the ghosted text in the textbox where you created your post.
:code:

Second. Textboxes are for text. Sure you can gerry-rig it to work for your purposes but it's not the right tool for the job. Just like you can go grocery shopping in an 18-wheeler. It will get the job done, but hardly the best choice.


I suggest you use a NumericUpDown control with the .DecimalPlaces property set to 2.

Attached Image

This post has been edited by tlhIn`toq: 04 August 2011 - 06:20 AM

Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,214
  • Joined: 02-June 10

Re: Keypress event in TextBox for Decimal with precision two

Posted 04 August 2011 - 06:52 AM

View Postbeg.saiful, on 04 August 2011 - 06:35 AM, said:

its taking ..but there is also an another problem like if i delete the enetered value and put a new value then the old value will come first then the new one


You don't seem to be reacting to the backspace key, by deleting the last digit. So, yeah your value still contains it. Then of course someone could use the arrow keys to cursor left a couple places then hit the delete key taking out a digit in the middle. So you have to handle that as well.

Then of course it isn't just keypress you have to deal with. What happens when someone copy/pastes a number in the box? In addition to trying to strip out every keypress that isn't a number, you will still have to re-evaluate the context when focus leaves the textbox, for this reason.

Making a numeric-only box isn't quite as simple as you might have first thought. I recommend changing from a textbox to either a maskedtextbox or a numericupdown as earlier suggested.
Was This Post Helpful? 1
  • +
  • -

#7 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Keypress event in TextBox for Decimal with precision two

Posted 06 August 2011 - 12:54 AM

@beg.saiful
You do like this as well:
(At the very end of textBox1_KeyPress)
textBox1.Text = string.format("{0:f2}",textBox1.Text); 

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1