Jump to content

Textbox!


jviana
 Share

Recommended Posts

Podes usar uma MaskedTextBox como o jpaulino disse ou então se por alguma razão tiveres mesmo de usar uma textbox o que podes fazer é validar a tecla.

    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyData = Keys.Back Then
            Exit Sub
        End If

        If e.KeyData < 48 OrElse e.KeyData > 57 Then 
            e.SuppressKeyPress = True
        End If
    End Sub

Mas assim tens o problema do copy & paste que podes resolver fazendo:

TextBox1.ShortcutsEnabled = False

no load da form.

Link to comment
Share on other sites

ou então fazes uma coisa simples no evento KeyPress como:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        If Not Char.IsNumber(e.KeyChar) And Not Convert.ToInt32(e.KeyChar) = Keys.Back And Not Convert.ToInt32(e.KeyChar) = Keys.Delete Then

            e.Handled = True

        End If

    End Sub

cumps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.