Jump to content
likeag6

[Resolvido] Visual Basic - Ajuda numa textbox

Recommended Posts

likeag6

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

Share this post


Link to post
Share on other sites
vikcch

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 by vikcch

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.