antiblock
https://i.imgur.com/aJ17bf7.gif
  • Chatbox

    You don't have permission to chat.
    Load More
Sign in to follow this  
RedM

[Help] Vb 2010 Textbox

2 posts in this topic

Bem gente eu estou a fazer um calculadora por causa de um trabalho lá pá escolha, já tenho tudo a funcionar correctamente, mas agora gostaria de por algo que bloquea-se a entrada de letras ou símbolos  na minha textbox, de forma a se o utilizador meter lá algo desse genero surja uma mensagem a dizer que apenas aceita numeros.

Já procurei andei a procurar em varios sites e até encontrei algumas coisas mas o problema é que alguns aceitam somente letras e outros somente numeros, e eu quero que o programa aceite numeros e virgulas.

 

Se alguém me poder ajudar, agradecia se precisarem do meu codigo digam que eu posto 

Public Class Form1

    Dim raiz As String = ""

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BSOMA.Click

        If TB1.Text = "" Or TB2.Text = "" Then          'Teste para ver se as caixas de texto estao vazias

            MsgBox("ERRO 02: Insira os numeros em falta")   'Mensagem de erro

        Else
            TBR.Text = CDbl(TB1.Text) + CDbl(TB2.Text)  'Operacao de soma

            TB1.Clear() 'Limpa as caixas de texto

            TB2.Clear()

            TBRA.Clear()
        End If

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BMULTI.Click

        If TB1.Text = "" Or TB2.Text = "" Then

            MsgBox("ERRO 02: Insira os numeros em falta")

        Else

            TBR.Text = CDbl(TB1.Text) * CDbl(TB2.Text)

            TB1.Clear()

            TB2.Clear()

            TBRA.Clear()

        End If

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BDIV.Click

        If TB1.Text = "" Or TB2.Text = "" Then

            MsgBox("ERRO 02: Insira os numeros em falta")

        Else
            If TB2.Text = "0" Then

                MsgBox("ERRO 01: O numero 2 deve ser diferente de 0")

                TB1.Clear()

                TB2.Clear()

                TBRA.Clear()

            Else
                TBR.Text = CDbl(TB1.Text) / CDbl(TB2.Text)
                TB1.Clear()

                TB2.Clear()

                TBRA.Clear()
            End If

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BSUB.Click

        If TB1.Text = "" Or TB2.Text = "" Then

            MsgBox("ERRO 02: Insira os numeros em falta")

        Else
            TBR.Text = CDbl(TB1.Text) - CDbl(TB2.Text)

            TB1.Clear()

            TB2.Clear()

            TBRA.Clear()

        End If

    End Sub
    Private Sub BR2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BR2.Click

        If TBRA.Text = "" Then

            MsgBox("ERRO 03: Deve introduzir um numero na caixa da Raiz Quadrada")

        Else
            raiz = Math.Sqrt(Convert.ToDouble(TBRA.Text))

            TBR.Text = raiz
            TB1.Clear()

            TB2.Clear()

            TBRA.Clear()

        End If

    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub N1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles N1.Click

       

    End Sub
    Private Sub TB1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB1.TextChanged
    End Sub
    Private Sub TB2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB2.TextChanged
    End Sub
    Private Sub TB3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub
    Private Sub TBR_TextChanged(sender As System.Object, e As System.EventArgs) Handles TBR.TextChanged
    End Sub

End Class

Share this post


Link to post
Share on other sites
antiblock
Elveron

Encontrei a solução noutro forum,

se alguém tiver este problema fica aqui a solução

 

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
                If e.KeyChar <> vbBack And "01234567890,".Contains(e.KeyChar) = False Then
                        e.Handled = True
                End If
        End Sub

Share this post


Link to post
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
Sign in to follow this