Cancel Key press event

后端 未结 7 2027
别那么骄傲
别那么骄傲 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:24

    Create a string with the characters you what to allow the user to enter.

    Use KeyDown or KeyUp to handle Special keys

    private void tbN1_KeyPress(object sender, KeyPressEventArgs e)
    {
        String sKeys = "1234567890ABCDEF";
        if (!sKeys.Contains(e.KeyChar.ToString().ToUpper()))
            e.Handled = true;
    }
    

提交回复
热议问题