Solved

Can someone explain how it this keypress event works ?

Posted on 2008-10-20
4
719 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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