# .NET and More > WPF, WCF, WF >  [RESOLVED] Validate textbox for numeric and decimal only

## trevorjeaton

Hi all,

I'm still learning wpf and come from a windows forms background.  I have a textbox called cvalue that i'm trying to validate to only allow numbers, decimals and backspaces - and the tab key to advance to the next control on the form.  I also have a button that is enabled or disabled depending on the status of the textbox - in my winforms code i did the following successfully:



```

 Private Sub cvalue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cvalue.KeyPress
        If Not Char.IsDigit(e.KeyChar) Then e.Handled = True 'allow only numerical entry
        If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
        If e.KeyChar = "." And cvalue.Text.IndexOf(".") = -1 Then e.Handled = False 'allow 1 decimal
        If e.KeyChar = Chr(9) Then measure1cb.Focus() 'allow tab and focus on next field
        If cvalue.Text = "" Or cvalue Is DBNull.Value Then
            calc_btn.Enabled = False
        End If
    End Sub
```

but there is no keypress event for wpf - i have tried the following but it doesn't seem to do anything at all:



```

Private Sub cvalue_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles cvalue.KeyDown
        If Not IsNumeric(e.Key) Then e.Handled = True 'allow only numbers
        If e.Key = Key.Back Then e.Handled = False 'allow backspace
        If e.Key = Key.Decimal And cvalue.Text.IndexOf(".") = -1 Then e.Handled = False 'allow only 1 decimal
        If e.Key = Key.Tab Then measure1cb.Focus() ' allow tab key
        If cvalue.Text = "" Or cvalue.Text Is DBNull.Value Then
            calc_btn.IsEnabled = False
        End If
    End Sub
```

i've also tried the previewkeydown event, the textchanged event, the previewtextchanged event and the previewtextinput event.

any help would be highly appreciated.

----------


## trevorjeaton

okay so i have it working with digits only with the following code using the previewtextinput event:



```

Private Sub cvalue_PreviewTextInput(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs) Handles cvalue.PreviewTextInput
        If Not Char.IsDigit(CChar(e.Text)) Then e.Handled = True
End Sub
```

but now i need to allow only a single decimal.....

----------


## trevorjeaton

okay got it - so for any others that may find this useful, here ya go:



```

Private Sub cvalue_PreviewTextInput(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs) Handles cvalue.PreviewTextInput
        If Not Char.IsDigit(CChar(e.Text)) Then e.Handled = True
        If Not Char.IsSeparator(CChar(e.Text)) And cvalue.Text.IndexOf(".") = -1 Then e.Handled = False
End Sub
```

that allows digits and separators (like commas for 1,000.00 for example) and only one decimal.

----------


## trevorjeaton

okay and last but not least, i added the previewkeydown event to validate if the button was enabled or disabled - for some reason, the code didn't fully work when i had it in the previewtextinput event - it would enable it after text was typed, but it would not disable it when the text was backspaced out and emptied.

I would still love to hear from others if it would be possible to validate on a keypress by keypress event to allow numbers and decimals only.  Here's the final code:



```

Private Sub cvalue_PreviewTextInput(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs) Handles cvalue.PreviewTextInput
        If Not Char.IsDigit(CChar(e.Text)) Then e.Handled = True
        If Not Char.IsSeparator(CChar(e.Text)) And cvalue.Text.IndexOf(".") = -1 Then e.Handled = False
End Sub

Private Sub cvalue_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles cvalue.PreviewKeyDown
        If cvalue.Text = "" Then
            calc_btn.IsEnabled = False
        Else
            calc_btn.IsEnabled = True
        End If
End Sub
```

enjoy! :-)

----------

