Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 721
  • Last Modified:

Can someone explain how it this keypress event works ?

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
cooljeba
Asked:
cooljeba
  • 2
  • 2
2 Solutions
 
keustermansCommented:
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
 
cooljebaAuthor Commented:
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
 
keustermansCommented:
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
 
cooljebaAuthor Commented:
Thank you :)
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now