开发者

textbox.clear can somebody check where it is wrong?

开发者 https://www.devze.com 2023-01-23 06:39 出处:网络
Im making a textbox which wont allow the user to use spaces but rather then clear the textbox it simply deleats the last character (which is the space) my code dosnt work can sombody look at it please

Im making a textbox which wont allow the user to use spaces but rather then clear the textbox it simply deleats the last character (which is the space) my code dosnt work can sombody look at it please?

Private Sub Textbox1_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    Dim UNDONE_TEXT As String

    If e.KeyCode = Keys.Space Then

        UNDONE_TEXT = ((TextBox1.Text) - 1)
        TextBox1.Clear()
        TextBox1.Text = UNDONE_TEXT

        MsgBox("Invalid character. No spaces Permited...")
    End If
End Sub开发者_StackOverflow社区

this returns 3????

UPDATE:

    Private Sub Textbox1_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    Dim UNDO_TEXT As String

    If e.KeyCode = Keys.Space Then
        UNDO_TEXT = TextBox1.Text
        TextBox1.Clear()
        TextBox1.Text = UNDO_TEXT
        TextBox1.Text.TrimEnd()
        MsgBox("Invalid character. No spaces Permited...")

    End If
End Sub

only now the focus is on the entire textbox when returned after MSGbox?


UNDONE_TEXT = left(TextBox1.Text, len(TextBox1.text)-1)


I'm no VBA expert, but would TextBox1.Text.Trim() be what you're looking for? This removes spaces at the beginning and at the end of a string. Possibly, you could try TrimEnd(). I don't know if that exists in VBA, it does in VB.NET:

Private Sub Textbox1_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    Dim UNDO_TEXT As String

    If e.KeyCode = Keys.Space Then
        TextBox1.Text = TextBox1.Text.TrimEnd()
        MsgBox("Invalid character. No spaces Permited...")
    End If

End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号