wpf textbox restrict input numbers
  • textbox numeric only wpf many programmers use PreviewTextInput event but it take input of white space to and not restrict or limit to textbox to just numbers its just restrict that user cannot input alphabets that's way i suggest to programmer that use PreviewKeyDown Event and see below my source code who will not accept white space to and will just accept numeric only in textbox.

    xaml code

    <TextBox Height="23" Margin="130,59,28,0" Name="textBox1" VerticalAlignment="Top"  PreviewKeyDown="textBox1_PreviewKeyDown"/>





    xaml.cs code
    private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
    {
    if (e.Key.ToString().ToUpper() == "BACK" || e.Key.ToString().ToUpper() == "DELETE"
    || e.Key.ToString().ToUpper() == "LEFT" || e.Key.ToString().ToUpper() == "RIGHT")
    {
    }
    else
    {
    if (e.Key.ToString().ToUpper().IndexOf("D") >= 0 || e.Key.ToString().ToUpper().IndexOf("NUMPAD") >= 0)
    {
    string LastCharOfSrting = e.Key.ToString().Substring(e.Key.ToString().Length - 1);
    try
    {
    int num = Convert.ToInt16(LastCharOfSrting);
    }
    catch
    {
    e.Handled = true;
    }
    }
    else
    {
    e.Handled = true;
    }
    }

    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

    Optimum Creative