validate a textbox in vb.net

后端 未结 3 812
误落风尘
误落风尘 2021-01-22 10:43

How could i validate a textbox in vb.net, so that it gives error message if i enter anything apart from alphabets

3条回答
  •  离开以前
    2021-01-22 11:29

    If it's a standard textbox in a WinForms app you can validate every typed character by handling the KeyPressed event and have the following code in the event handler:

    e.Handled = Not Char.IsLetter(e.KeyChar)
    

    The user could still use the mouse to paste something in there though so you might need to handle that as well.

    Another option is to handle the Validating event and if the textbox contains any non alphabetic characters you set e.Cancel to true.

提交回复
热议问题