|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have 2 textboxes both which you have to enter a temperature which includes minus numbers. I am new to VB6 and have been advised of validation and need to know what I can do to validate outsidetemp.text and insidetemp.text so only numbers can be entered. If the user tries to enter a character a pop up message should appear?
Thanks ![]() |
|
#2
|
||||
|
||||
|
Can't remmber exactly, but for now try something like
Code:
If Not IsNumeric(outsidetemp.text) Then
MsgBox ("This is not a valid number.")
End If
If Not IsNumeric(insidetemp.text) Then
MsgBox ("This is not a valid number.")
End If
You need to make sure to do something whe this happens, like setting the focus to the TextBox and then allowing the user to enter the number again before you use it. I think you need to use Code:
outsidetemp.focus ' set focus textbox. I'm hoping someone is gonna correct me if I'm wrong. |
|
#3
|
||||
|
||||
|
AFAIK, VB6 doesn't have validation events, instead you need to use the lose_focus event.
Create an eventhandler for when either textbox loses focus, then write the validation code yourself and display an error if nessacery. Note that unlike VB7, there is not "e.Cancel = False" to prevent the control losing focus. For example: Code:
Private Sub outsidetemp_losefocus()
TempValidate(outsidetemp)
End Sub
Private Sub insidetemp_losefocus()
TempValidate(insidetemp)
End Sub
Private Sub TempValidate(textbox)
If textbox.Text Like "[A-zA-Z]" Then
Msgbox("Please enter a valid number please")
End If
End Sub
Something like that. |
|
#4
|
||||
|
||||
|
Quote:
This docuemnt might help you. you need to have PDF reader/viewer though. http://www.hitmill.com/programming/vb/validation.pdf |
|
#5
|
|||
|
|||
|
thanks guys...I am going to try the stuff you have given and get back to you!
|
|
#6
|
|||
|
|||
|
hi
Just wanted to inform you that i read al the offered solutions, even the pdf file solution. I have to tell you that all of them come pretty close but none of them solves the problem completely. Th most important remaining problem is the decimal separator. If you use . as decimal separator the following value 45,7 is still a valid nummer with all the solutions that were offered. the actual value will then be truncated to 45 which isn't what you want is it ? The code beneath takes care of a few extra possible errors, namely : - removing leading and trailing blancs - conversing , to . as decimal symbol - removing eventual blancs between the minus sign and the valuie itself Here is the code....wish you good luck Private Sub TxtTemperature_LostFocus() Dim TemperatureValue As Double 'Remove the leading and trailing blancs TxtTemperature.Text = Trim(TxtTemperature.Text) 'Replace the , with a . (depending which decimal sign you are using) TxtTemperature.Text = Replace(TxtTemperature.Text, ",", ".") 'Replace eventual blanks between the minus sign and the value TxtTemperature.Text = Replace(TxtTemperature.Text, " ", "") 'Check if the return value of the control is false If CheckValue(TxtTemperature.Text) = False Then 'Give error message MsgBox "Not a correct value", vbCritical, "No valid temperature" 'Select the typed in text so you can start retyping directly, replacing what as there before TxtTemperature.SelStart = 0 TxtTemperature.SelLength = Len(TxtTemperature.Text) 'Put the focus back onto the temperature field TxtTemperature.SetFocus End If End Sub Private Function CheckValue(ControlNumber As String) As Boolean Dim Position As Integer Dim CaracterCode As Integer 'Initialize the return code CheckValue = True 'Initialize the Position to the first position in the string or the second depending on the fact 'if the leftmost caracter is a minus sign or not If Left(ControlNumber, 1) = "-" Then Position = 2 Else Position = 1 End If 'Check if the remaining caracters are numbers or the decimal separator . Do While Position < Len(ControlNumber) Position = Position + 1 CaracterCode = Asc(Mid(ControlNumber, Position, 1)) 'Illegal caracter, quit the loop and set the return code to false If Not ((CaracterCode >= Asc("0") And CaracterCode <= Asc("9")) Or CaracterCode = Asc(".")) Then CheckValue = False Exit Do End If Loop End Function |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > HELP!!!! Texbox validation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|