Olá,
Voltei com um assunto simples e muito utilizado.
Como configurar uma caixa de texto para receber somente numérico e “,” (virgula)
Vamos lá!
Para este artigo, vou utilizar o Visual Studio e Linguagem VB.
Adcione uma caixa de texto a um projeto ja criado no Visual Studio 2008, File>>New>>WebSiteAdcione um novo Módulo a digite o texto abaixo:
Código: Módulo
Function SoNumeros(ByVal Keyascii As Short) As Short
If InStr(“1234567890″, Chr(Keyascii)) = 0 Then
SoNumeros = 0
Else
SoNumeros = Keyascii
End If
Select Case Keyascii
Case 8
SoNumeros = Keyascii
Case 13
SoNumeros = Keyascii
Case 32
SoNumeros = Keyascii
Case 44
SoNumeros = Keyascii
End Select
End Function
No formulário acrescente ao TextBox no Evento Keypress ( em propriedades)
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox1.KeyPress
Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
KeyAscii = CShort(SoNumeros(KeyAscii))
If KeyAscii = 0 Then
e.Handled = True
End If
End Sub
Para incrementar o textBox voce pode acrescentar um procedimento para receber o formato Currency ( Moeda)
Adcione o evento LOSTFOCUS ao textBox
Private Sub textBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVlVenda.LostFocus
If IsNumeric(textBox1.Text) Then
Try
TextBox1.text = FormatCurrency(TextBox1.text)
Catch ex As Exception
Throw ex
End Try
End If
End Sub
Agora é só testar