Solved

Can someone explain how it this keypress event works ?

Posted on 2008-10-20
4
716 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
  • 2
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:keustermans
keustermans earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 3 35
C# Export DataGridView 4 37
display data in text field from data base for updating 6 24
Code enhancement 5 12
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now