Limit Input to Two Decimal Places in C#

Sometimes you need to restrict the input to two decimal places in C#. The following function returns true if the input is valid, otherwise it returns false.

You will need to listen to a Key_Down event handler for your TextBox and put the following code in it. Setting e.handled to true prevents the current character to be input in the TextBox.