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.

KeyPress event on the TextBox.

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

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

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?

TextBox2.Text = String.Empty
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.

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)

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!

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.