Solved

Can someone explain how it this keypress event works ?

Posted on 2008-10-20
4
718 Views
Last Modified: 2013-12-17
Hello EE,

This is this key press event code which is used to validate the integer and decimal numbers.
Could anyone explain to me like which line of code does what ?


public static void ValidateForDecimal(KeyPressEventArgs e, TextBox txtBox, int integerpart, int decimalpart)
        {
            int intpart, decpart;
            intpart = ++integerpart ;
            decpart = decimalpart;
 
            try
            {
 
                if (txtBox.Text.Trim().IndexOf(".") > -1)
                {
                    // Prevents user from entering more than one '.'
 
                    if ((e.KeyChar != (char)8) && (e.KeyChar == '.'))
                    {
                        e.Handled = true;
                    }
 
                    // Prevents user from entering special characters
                    else if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 32 && e.KeyChar != (char)8 && e.KeyChar != '.')
                    {
                        e.Handled = true;
                    }
 
                    else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length == intpart) && (txtBox.Text.Trim().Substring(txtBox.Text.Trim().IndexOf(".") + 1).Length == decpart))
                    {
                        e.Handled = true;
                    }
                    else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length <= --intpart) && (txtBox.Text.Trim().Substring(txtBox.Text.Trim().IndexOf(".") + 1).Length == decpart))
                    {
                        if ((txtBox.SelectionStart <= txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length <= --intpart))
                        {
                            e.Handled = false;
                        }
                        else
                        {
                            e.Handled = true;
                        }
                    }
                    
                    else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length <= --intpart) && (txtBox.Text.Trim().Substring(txtBox.Text.Trim().IndexOf(".") + 1).Length <= --decpart))
                    {
                        e.Handled = false;
                    }
                    else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length == intpart) && (txtBox.Text.Trim().Substring(txtBox.Text.Trim().IndexOf(".") + 1).Length <= --decpart) && (txtBox.SelectionStart >= intpart++))
                    {
                        e.Handled = false;
                    }
                    else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length == intpart) && (txtBox.SelectionStart <= intpart))
                    {
                        e.Handled = true;
                    }
                    else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length <= --intpart) && (txtBox.Text.Trim().Substring(txtBox.Text.Trim().IndexOf(".") + 1).Length == decpart) && (txtBox.SelectionStart == 0))
                    {
                        e.Handled = false;
                    }
                    else if ((txtBox.Text.Trim().IndexOf(".") > -1) && (e.KeyChar != (char)8) && (txtBox.Text.Trim().Substring(txtBox.Text.Trim().IndexOf(".") + 1).Length == decpart) && (txtBox.SelectionStart > intpart) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length <= intpart))
                    {
                        e.Handled = true;
                    }
                }
                else
                {
                    if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Length <= intpart) && (e.KeyChar == '.'))
                    {
                        e.Handled = false;
                    }
                    else if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 32 && e.KeyChar != (char)8 && e.KeyChar != '.')
                    {
                        e.Handled = true;
                    }
                    else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Length <= --intpart))
                    {
                        e.Handled = false;
                    }
                    if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Length == intpart) && (e.KeyChar != '.'))
                    {
                        e.Handled = true;
                    }
                    else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Length > intpart) && (e.KeyChar == '.'))
                    {
                        e.Handled = true;
                    }
                }
 
 
            }
            catch { throw; }
        }

Open in new window

0
Comment
Question by:cooljeba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:keustermans
keustermans earned 500 total points
ID: 22765541
Hi,

Everything her revolves araund the KeypressEvent Argument  e.Handled.

When ever you set e.Handled = true then the character will not be added to the text box

I am only going to the first few lines of code and you should be able to deduct the others from there.  If you still do not understand then set a break point in the event handler and step through the code and it will become clear waht is going on.

if (txtBox.Text.Trim().IndexOf(".") > -1)// this indicates that a decimal point has been typed
                {
                    // Prevents user from entering more than one '.'
 
                    if ((e.KeyChar != (char)8) && (e.KeyChar == '.'))//here we check if the user pressed
                                                                                                 // the  '.'      button on the keyboard
                    {
                        e.Handled = true;// if yes then set the handled event to true which will prevent the '.'
                                                      //tobe added to the text box
                    }

 // Prevents user from entering special characters
else if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 32 && e.KeyChar != (char)8 && e.KeyChar != '.')
{
       e.Handled = true;//if the caharacter is an ivalid charected as in the statment above do not add it to  
                                  //the text box.
}


0
 

Author Comment

by:cooljeba
ID: 22766759
Hi keustermans,

Thanks for your reply.. Ya that much I was able to make out.. if we look more deeper the keypress events  go many itterations
Like what it is checking for ??
something this code does


else if ((e.KeyChar != (char)8) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length <= --intpart) && (txtBox.Text.Trim().Substring(txtBox.Text.Trim().IndexOf(".") + 1).Length == decpart))
                    {
                        if ((txtBox.SelectionStart <= txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length) && (txtBox.Text.Trim().Substring(0, (txtBox.Text.Trim().IndexOf(".") == 0 ? 0 : txtBox.Text.Trim().IndexOf("."))).Length <= --intpart))
                        {
                            e.Handled = false;
                        }
                        else
                        {
                            e.Handled = true;
                        }
                    }

Open in new window

0
 
LVL 7

Accepted Solution

by:
keustermans earned 500 total points
ID: 22774140
Over here they are making sure that the key pressed is not a back space and that the decimalpoint is after the intergerpart of the string i.e it does not allow you to type .12
then they check if anything was selected, it will not let you overrite the your selection if the length of the number is equal to the sum  integer part +1 (for the deciaml point )and decimal  part values passed to the function. if the string is the maximum allowed length as set in the passed values then you can only backspace and not overwrite.  If you have not reached the total allowed number of digits for the integer and the decimal then you can overwrite.

0
 

Author Comment

by:cooljeba
ID: 22774325
Thank you :)
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question