Showing posts with label textbox. Show all posts
Showing posts with label textbox. Show all posts

Sunday, 31 August 2014

Simple numeric textbox

A simple way to make a textbox which accepts only numbers and a decimal number separator.




First of all we need to declare some properties.

C#
private List<char> ValidCharacters { getset; }
This property would contain the list of valid characters. We'll set its values later in the constructor of the class.

private byte NumberOfDecimalPlaces { getset; }
This property would determine the number of places after decimal separator.

private char DecimalSeparator { getset; }
This property would determine the decimal separator which can be a dot "." or a comma "," in some countries it can be something else.
Lets set the values to these properties. this can be set into the constructor or the form load event.
if (ValidCharacters == null)
{
        ValidCharacters = new List<char>(); // Initializes the property.
}

NumberOfDecimalPlaces = 4; // Sets the number of places after the decimal separator.
DecimalSeparator = ',';// Determines the value for decimal separator.
ValidCharacters.AddRange("1234567890"); // Add valid characters into the list.
ValidCharacters.Add(DecimalSeparator); // Adds decimal separator into the valid characters list.

After setting all the properties into the relevant area just paste the code below into the KeyPress event of the textbox.
C#
    private void NumericTextBoxKeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == (char)Keys.Back)
        return;

      var cursorLocation = NumericTextBox.SelectionStart;

      if (e.KeyChar == DecimalSeparator && NumericTextBox.Text.Contains(DecimalSeparator))
      {
        e.Handled = true;
      }
      else
      {
        if (!ValidCharacters.Contains(e.KeyChar))
        {
          e.Handled = true;
        }
        else
        {
          if (NumericTextBox.Text.Contains(DecimalSeparator))
          {
            var locationOfDot = NumericTextBox.Text.IndexOf(DecimalSeparator);
            var textLengthAfterDecimal = NumericTextBox.Text.Substring(locationOfDot, NumericTextBox.Text.Length - (locationOfDot + 1)).Length;
            if (cursorLocation > locationOfDot)
            {
              if (textLengthAfterDecimal >= NumberOfDecimalPlaces)
              {
                e.Handled = true;
              }
            }
          }
        }
      }
    }


VB
Private m_ValidCharacters As List(Of Char)
Private Property ValidCharacters() As List(Of Char)
                Get
                                Return m_ValidCharacters
                End Get
                Set
                                m_ValidCharacters = Value
                End Set
End Property

Private m_NumberOfDecimalPlaces As Byte
Private Property NumberOfDecimalPlaces() As Byte
       Get
              Return m_NumberOfDecimalPlaces
       End Get
       Set
              m_NumberOfDecimalPlaces = Value
       End Set
End Property

Private m_DecimalSeparator As Char
Private Property DecimalSeparator() As Char
                Get
                                Return m_DecimalSeparator
                End Get
                Set
                                m_DecimalSeparator = Value
                End Set
End Property
If ValidCharacters Is Nothing Then
       ValidCharacters = New List(Of Char)()
End If

NumberOfDecimalPlaces = 4 ' Sets the number of places after the decimal separator.
DecimalSeparator = ","C' Determines the value for decimal separator.
ValidCharacters.AddRange("1234567890") ' Add valid characters into the list.
ValidCharacters.Add(DecimalSeparator) ' Adds decimal separator into the valid


VB
                                Private Sub NumericTextBoxKeyPress(sender As Object, e As KeyPressEventArgs)
                                                If e.KeyChar = CChar(Keys.Back) Then
                                                                Return
                                                End If

                                                Dim cursorLocation = NumericTextBox.SelectionStart

                                                If e.KeyChar = DecimalSeparator AndAlso NumericTextBox.Text.Contains(DecimalSeparator) Then
                                                                e.Handled = True
                                                Else
                                                                If Not ValidCharacters.Contains(e.KeyChar) Then
                                                                                e.Handled = True
                                                                Else
                                                                                If NumericTextBox.Text.Contains(DecimalSeparator) Then
                                                                                                Dim locationOfDot = NumericTextBox.Text.IndexOf(DecimalSeparator)
                                                                                                Dim textLengthAfterDecimal = NumericTextBox.Text.Substring(locationOfDot, NumericTextBox.Text.Length - (locationOfDot + 1)).Length
                                                                                                If cursorLocation > locationOfDot Then
                                                                                                                If textLengthAfterDecimal >= NumberOfDecimalPlaces Then
                                                                                                                                e.Handled = True
                                                                                                                End If
                                                                                                End If
                                                                                End If
                                                                End If
                                                End If
                                End Sub
Please leave a comment if this helps you.