0

Hi friends,
I wanted to restrict my text box to have only 10 characters.I have used the following code

Private Sub txtSpouseWorkPhone_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSpouseWorkPhone.KeyDown
        If txtSpouseWorkPhone.TextLength > 10 Then
            MsgBox("Only 10 characters are alowed", MsgBoxStyle.Critical, "Invalid PAN No")
            txtSpouseWorkPhone.Text = txtSpouseWorkPhone.Text.Substring(0, (txtSpouseWorkPhone.TextLength - 1))
            txtSpouseWorkPhone.Focus()
        End If

But I have a small problem ie.
once the focus leaves the text box,I am not able to make further chances in it.ie.the message box appears irrespective of the keys we press, after 10 characters.

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by deepanbecse
Featured Replies
  • Why don't you raise the event on TextChanged instead? [CODE]Private Sub txtSpouseWorkPhone_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSpouseWorkPhone.TextChanged[/CODE] Read More

  • 1

    Why cant u set the MAXLength propery of textbox to 10? Read More

1

Why don't you raise the event on TextChanged instead?

Private Sub txtSpouseWorkPhone_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSpouseWorkPhone.TextChanged

Edited by Techyon: fixed code

1

Why cant u set the MAXLength propery of textbox to 10?

Edited by Pgmer: 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.