NUMEROS
como eu faço para um textbox receber somente numeros ?
obrigado
obrigado
Tente:
Public Function OnlyNumbers(TextBox As TextBox, ByVal KeyAscii As Integer) As Integer
Dim Char As String
OnlyNumbers = KeyAscii
Char = Chr(KeyAscii)
If KeyAscii = vbKeyBack Then Exit Function
If Char = "." Then KeyAscii = Asc(",")
Char = Chr(KeyAscii)
If Char = "-" Then
If InStr(TextBox, "-") <> 0 Or Len(Trim(TextBox)) = 0 Then
OnlyNumbers = 0
Else
If InStr(TextBox, "-") = 0 Then TextBox = "-" & TextBox
OnlyNumbers = 0
Exit Function
End If
End If
If Char = "," Then
If InStr(TextBox, ",") <> 0 Or Len(Trim(TextBox)) = 0 Then
OnlyNumbers = 0
Else
OnlyNumbers = Asc(",")
Exit Function
End If
End If
If Not IsNumeric(Char) Then
OnlyNumbers = 0
End If
End Function
'Teste
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = OnlyNumbers(Text1, KeyAscii)
End Sub
lyNumbers(Text1, KeyAscii)
End Sub
Tem esse tambem:
Cara, antes de perguntar é aconselhavel dar uma busca no site pois pode ser que haja algum código no site que lhe atenda.
Se sua dúvida foi sanada favor finalizar o Tópico.
Fico grato.
Permitindo somente números e backspace numa text box via API:
Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE As Long = &HFFFFFFF0
Private Const ES_NUMBER As Long = &H2000
Private defstyle As Long
Private Sub Form_Load()
defstyle = GetWindowLong(Text1.hwnd, GWL_STYLE)
SetWindowLong Text1.hwnd, GWL_STYLE, defstyle Or ES_NUMBER
End Sub
Permitindo somente números e backspace numa text box sem API:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Is = 8 ' Backspace
Case 48 To 57 ' Numeros de 0 a 9
Case Else
KeyAscii = 0
End Select
End SubCara, antes de perguntar é aconselhavel dar uma busca no site pois pode ser que haja algum código no site que lhe atenda.
Se sua dúvida foi sanada favor finalizar o Tópico.
Fico grato.
Coloque no evento "change" do text o seguinte código.
Supondo que o nome do text seja "text1"
Private Sub Text1_Change()
If Val(Text1.Text) = 0 Then
Text1.Text = ""
Else
Text1.Text = Val(Text1.Text)
End If
Text1.SelStart = Len(Text1.Text)
End Sub
Caminhos diferentes q levam pro mesmo fim tenta ese tanbem
Private Sub txtcodimo_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
Private Sub txtcodimo_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
Caso você queira que o usuário não digite de jeito nenhum, sem validãção ou nada... você pode usar a ferramenta Microsoft MaskEdit Box, e alterar a propriedade mask para:
### - o usuário pode digitar 3 números
#### - 4 números e assim por diante...
para adicionar a ferramenta aperte o ctrl+t... e procure poe Microsoft MaskEdit Box...
### - o usuário pode digitar 3 números
#### - 4 números e assim por diante...
para adicionar a ferramenta aperte o ctrl+t... e procure poe Microsoft MaskEdit Box...
Tópico encerrado , respostas não são mais permitidas