?
Solved

Can someone explain how it this keypress event works ?

Posted on 2008-10-20
4
Medium Priority
?
720 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 2000 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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