Cancel Key press event

后端 未结 7 2031
别那么骄傲
别那么骄傲 2021-01-05 07:37

How can I return the key?, mean if I want to allow only integer values in the textbox, how can I don\'t allow user to not enter non-integers, regarding, KeyPress

7条回答
  •  清酒与你
    2021-01-05 08:36

    For WPF, use the PreviewTextInput event with code like:

    // Filter out non-numeric keys.
    private void MyApp_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
    String sKeys = "1234567890";
    if (!sKeys.Contains(e.Text))
        e.Handled = true;
    }
    

提交回复
热议问题