0

Hi!
Since my last thread hasn't been answered and I simply cannot wait until it gets answered, how can I validade what is being written on a textbox?

I need to know when the user types a "-" (minus) char. And when he do so, an message appears (preferably an ballon or a tooltip) and says that is illegal to type this char in particular.

How can I catch it?

Thanks.

5
Contributors
9
Replies
13
Views
5 Years
Discussion Span
Last Post by codeorder
0

Put this in the TextChanged event of the textbox.

If TextBox1.Text.Contains("-") Then
Msgbox ("Invalid!")
End If

Or if you need something Immediate, look into the KeyDown Event

0
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        Dim tTip As New ToolTip
        If TextBox2.Text = "-" Then
            tTip.Show("invalid", TextBox2, 2000)
        End If
    End Sub
0

Yep, I got it working. I'll just use the tooltip bLuEmEzzy posted.
Just one more thing, I'm using a SendKeys.Send("{BS}") to automatically clear that char, there is another way?

0
TextBox2.Text = String.Empty

But, if the user already has another chars into the forms, it would clear all. Is that that I don't want.

0

please try, this will only delete the last char of the textbox hope it can help..

TextBox2.Text = TextBox2.Text.Substring(0, TextBox2.Text.Length - 1)

Edited by bLuEmEzzy: n/a

0

It is working, but seems that it starts from beggining. Well, nevermind, the SendKeys is fine, I also think that if you can do the same thing with less code, better.

Thanks!

0

See if this helps to block a key from being typed into a TextBox.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Chr(AscW("-")) Then '// check if "-" key has been pressed.
            '// ToolTip code here.
            e.Handled = True '// Cancel the key pressed.
        End If
    End Sub

Edited by codeorder: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.