vba 防止在文本框中输入空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4088220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:15:43  来源:igfitidea点击:

Prevent spaces in textbox being enterd

vbaspaces

提问by John mc Niel

Got a textbox in VBA and dont want the usser to be able to type spaces in it can i prevent the usser from doing this with programing?

在 VBA 中有一个文本框,并且不希望用户能够在其中键入空格,我可以防止用户通过编程来执行此操作吗?

回答by Mr Shoubs

Handle the keypress/changeevent, check for " " then set to string.empty.

处理按键/更改事件,检查“”,然后设置为 string.empty。

Alternativly use string.replace(" ", string.empty) after the data has been entered

或者在输入数据后使用 string.replace(" ", string.empty)

回答by Viper

Maybe thiswill help you:

也许会帮助你:

You can allow/disallow specific buttons in the KeyPress-Event

您可以允许/禁止 KeyPress-Event 中的特定按钮

Private Sub TextBox1_KeyPress(KeyAscii As Integer)
  Select Case KeyAscii
    Case asc("0") To asc("9"), 8, 32, asc(",")
      'allow signs
    Case Else
       KeyAscii = 0 'forbid everything else
  End Select
End Sub

NOTE: This example allows only numbers to be entered. You need to adapt it for your case.

注意:此示例只允许输入数字。你需要根据你的情况调整它。

Here is another page with nearly the same example but in english: LINK

这是另一个具有几乎相同示例但英文的页面:LINK

回答by Daniel G. Wilson

' Disable Space in TextBox
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 32 Then
        KeyAscii = 0
    End If
End Sub 

http://www.vbforums.com/showthread.php?t=447978

http://www.vbforums.com/showthread.php?t=447978

From google :D

来自谷歌:D