验证代码以在文本框中使用空格键

问题描述:

我要验证文本框中使用空格键的代码,它不能仅接受输入空格

I want validate code for using space key in textbox ,It can not accept the the entring space only

Private Sub txtTExtBoxName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtItemCode.KeyPress
    If Asc(e.KeyChar) = Keys.Space Then
        MessageBox.Show("Add some stuff here")
    End If
End Sub


使用以下代码:
Use The Following Code :
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
       Select Case Asc(e.KeyChar)
           Case 32   '32-KeyChar Of Space
               e.Handled = True
               MessageBox.Show("You Can Not Enter Space Here!")
           Case Else
               'Else Code Goes Here
       End Select
   End Sub


否则,如果您只是不想允许在TextBox中输入单个空格,请使用此空格:


Or Else If you Just don''t want to allow to enter Single Space in TextBox Use this one :

IF Trim(TextBox1.Text)="" Then
    MessageBox.Show("Please Enter Data Here!")
Else
   'Else Code Goes Here
End If


希望对您有所帮助!:)


I Hope It Will Help You!:)


正则表达式仅接受字符和空格

正则表达式=/^ [a-zA-Z] +
Regular expression to accept only characters and spaces

regular expression=/^[a-zA-Z ]+