 |
|
 |
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Input Validation in VB6
It's a textbox isn't it?
Well... you could do something like this (it'd be easier to prevent letters from being entered than to strip/warn the user after the fact)
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 = (-16)
Private Const ES_NUMBER = &H2000&
'/////////////////////////////////
'// MakeNumericTextbox
Public Sub MakeNumericTextbox(ByRef theTextBox As TextBox, Optional ByVal MakeNumeric As Boolean = True)
Dim CurStyle As Long
Dim NewStyle As Long
' Get the current style, so we don't lose anything
CurStyle = GetWindowLong(theTextBox.hwnd, GWL_STYLE)
If MakeNumeric Then
CurStyle = CurStyle Or ES_NUMBER
Else
CurStyle = CurStyle And (Not ES_NUMBER)
End If
' Set the new style
NewStyle = SetWindowLong(theTextBox.hwnd, GWL_STYLE, CurStyle)
' Refresh it... just in case
theTextBox.Refresh
End Sub
'then just:
MakeNumericTextbox Text1
|
textboxes have a lostfocus & validate that you can check for proper values then....
and you can set the max size of the textbox to "2" so that 99 would be the largest number
[Edited by JLRodgers on 19-03-2004 at 01:53 PM GMT]
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
19-03-2004 at 07:52 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Input Validation in VB6
if Text1.Text="" then
MsgBox "Please Enter a number between 1 and 49. Thank you."
Exit Sub
End If |
If a user can enter space in your textbox, then you can use trim function:
| If Trim(Text1.Text)="" then |
..............................
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
21-03-2004 at 02:34 AM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Input Validation in VB6
You could just put a "1" (or whatever "default" value the program would use for the box) in the form_load event
Private Sub Form_Load
Text1.Text = "1"
End Sub
' Then in a lostfocus/validate... or change (but could have weird effects...
Private Sub Text1_LostFocus()
' If Trim(Text1.Text) ="" if you allow the space character but don't think so
If Text1.Text = "" Then
' Enter the default value/whatnot (like given by goran)
End If
End Sub
|
Of course... it could be just as easy if not easier to use a combobox... (the code if you'd want it...)
' Set the combobox style to "2 - Dropdown List" in the properties
Private Sub Form_Load()
Dim i as Integer
Combo1.Clear
For i = 1 to 49
Combo1.AddItem i
Next
Combo1.ListIndex = 0
End Sub
' Combo1.Text = the string for the number
|
[Edited by JLRodgers on 20-03-2004 at 09:23 PM GMT]
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
21-03-2004 at 03:20 AM |
|
|
|
|
 |
 |