vb.net 问题
REM按钮下的事件显示出本人想要的TextBox1.TextPrivateSubButton1_Click(ByValsenderAsSystem.Object,ByVa...
REM 按钮下的事件显示出本人想要的TextBox1.Text
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Temp As String = TextBox1.Text
While InStr(1, Temp, Space(2), CompareMethod.Binary) > 0
Temp = Replace(Temp, Space(2), Space(1))
End While
TextBox1.Text = Temp
End Sub
REM 我在文本框“TextBox1”上,连续按2个空格没反应。当我连续按3个空格,有反应了,但是“TextBox1.Text”异常了
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim Temp As String = TextBox1.Text
While InStr(1, Temp, Space(2), CompareMethod.Binary) > 0
Temp = Replace(Temp, Space(2), Space(1))
End While
TextBox1.Text = Temp
End Sub
REM 不知道为什么?希望高手解释一下我错在哪里? 展开
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Temp As String = TextBox1.Text
While InStr(1, Temp, Space(2), CompareMethod.Binary) > 0
Temp = Replace(Temp, Space(2), Space(1))
End While
TextBox1.Text = Temp
End Sub
REM 我在文本框“TextBox1”上,连续按2个空格没反应。当我连续按3个空格,有反应了,但是“TextBox1.Text”异常了
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim Temp As String = TextBox1.Text
While InStr(1, Temp, Space(2), CompareMethod.Binary) > 0
Temp = Replace(Temp, Space(2), Space(1))
End While
TextBox1.Text = Temp
End Sub
REM 不知道为什么?希望高手解释一下我错在哪里? 展开
1个回答
展开全部
VB.NET2005或以上版本
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim Temp As String = TextBox1.Text
While InStr(1, Temp, Space(2), CompareMethod.Binary) > 0
Temp = Replace(Temp, Space(2), Space(1))
End While
TextBox1.Text = Temp
My.Computer.Keyboard.SendKeys("{end}", True)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim Temp As String = TextBox1.Text
While InStr(1, Temp, Space(2), CompareMethod.Binary) > 0
Temp = Replace(Temp, Space(2), Space(1))
End While
TextBox1.Text = Temp
My.Computer.Keyboard.SendKeys("{end}", True)
End Sub
更多追问追答
追问
我目的是要【TextBox1.Text】不允许出现连续的空格。加了
My.Computer.Keyboard.SendKeys("{end}", True)
结果是“不允许超过3个空格”。加了这句代码,有个严重的问题。那就是我没法在【TextBox1.Text】的中间输入字符,每次输入都会自动跳到字符串末尾。
追答
InStr(1, Temp, Space(2), CompareMethod.Binary) > 0 就是有2个以上的空格才处理空格呀!
你要是说【TextBox1.Text】不允许出现连续的空格也是非常简单
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim a As Integer = InStr(TextBox1.Text, " ")
If a > 0 Then
TextBox1.Text = System.Text.RegularExpressions.Regex.Replace(TextBox1.Text, " {2,}", " ")
TextBox1.SelectionStart = a
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询