likeag6 Posted December 8, 2015 at 08:38 PM Report Share #590489 Posted December 8, 2015 at 08:38 PM Olá gente, bem estou a fazer uma calculadora simples e o código está a ir lindamente, mas eu agora preciso de saber como ponho a minha textbox a apenas aceitar números e virgulas. Já pesquisei e aliás já encontrei algumas soluções, mas todas elas ou aceitam letras ou aceitam números e eu acho que preciso que ele apenas aceite números e apenas uma tecla da parte das letras que é a virgula. Vou deixar aqui o meu código caso alguém me possa ajudar, agradecia 🙂 Obrigado, LikeAG6 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 Link to comment Share on other sites More sharing options...
vikcch Posted December 8, 2015 at 09:36 PM Report Share #590493 Posted December 8, 2015 at 09:36 PM (edited) Olá, abriste o tópico na secção errada, aqui é para versões anteriores ao .net podes usar o evento KeyPress da textbox: 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 Edited December 8, 2015 at 09:36 PM by vikcch Link to comment Share on other sites More sharing options...
likeag6 Posted December 8, 2015 at 11:15 PM Author Report Share #590499 Posted December 8, 2015 at 11:15 PM Muito obrigado amigo vikcch Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now