Our screens need to validate at the points of field input, most likely as a warning only.
The below code can cause an infinite loop, due to #PB_EventType_LostFocus repeatedly being triggered.
For example :
1. Type ABC as the group and then click the cost input (or press TAB)
The application shows the group warning
2. Cancel the warning
3. Type 1000 as the cost and then click the group input (or press SHIFT-TAB)
The application shows the cost warning
4. Cancel the warning
Group warnings then continue indefinitely. I found that I can prevent this by discarding the events, as shown in the commented-out lines.
I'm trying to understand why discarding the events should be necessary, given that group has already lost focus, so why does it continuously lose focus? Is there a cleaner method? It doesn't seem like an unusual requirement, so I wonder if there are better ways. Thanks.
Code: Select all
;-**
;-** ===============================================================================================================
;-** Field validation warnings, at the point of input, or as hard error on saving record
;-** ===============================================================================================================
;-**
Procedure.i Validate_Group(validstr.s, title.s)
Define valid.i = #True
If validstr.s <> "" And Len(validstr.s) <> 4
MessageRequester(title, "Group code must be four characters", #PB_MessageRequester_Error)
; While WindowEvent() : Wend
valid.i = #False
EndIf
ProcedureReturn valid.i
EndProcedure
Procedure.i Validate_Cost(validstr.s, title.s)
Define valid.i = #True
Define validdbl.d
validdbl.d = ValD(validstr.s)
If validdbl.d < 0 Or validdbl.d > 999.99
MessageRequester(title, "Cost must be positive and not more than 999.99", #PB_MessageRequester_Error)
; While WindowEvent() : Wend
valid.i = #False
EndIf
ProcedureReturn valid.i
EndProcedure
win.i = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 475, 210, "Test", #PB_Window_MinimizeGadget | #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(#PB_Any, 30, 30, 160, 25, "Item ref.")
TextGadget(#PB_Any, 30, 65, 160, 25, "Group")
TextGadget(#PB_Any, 340, 65, 160, 25, "(4 characters)")
TextGadget(#PB_Any, 30, 100, 160, 25, "Cost")
TextGadget(#PB_Any, 340, 100, 160, 25, "(0 - 999.99)")
pcode.i = StringGadget(#PB_Any, 150, 25, 140, 25, "")
group.i = StringGadget(#PB_Any, 150, 60, 100, 25, "")
cost.i = StringGadget(#PB_Any, 150, 95, 100, 25, "")
savebutton.i = ButtonGadget(#PB_Any, 30, 160, 100, 25, "Save")
exitbutton.i = ButtonGadget(#PB_Any, 150, 160, 100, 25, "Exit")
SetActiveGadget(pcode.i)
Repeat
event = WaitWindowEvent()
eventtype = EventType()
Select event
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget ; Gadget event
Select EventGadget()
Case savebutton ; Save
If Validate_Group(GetGadgetText(group), "ERROR") ; Validate group before saving record
If Validate_Cost(GetGadgetText(cost), "ERROR") ; ... and validate cost
; **
; ** Save record here
; **
SetGadgetText(pcode, "")
SetGadgetText(group, "")
SetGadgetText(cost, "")
EndIf
EndIf
Case exitbutton ; Exit application
Break
Case group
If eventtype = #PB_EventType_LostFocus ; On lost focus of group input, validate (warning only)
Validate_Group(GetGadgetText(group), "Warning")
EndIf
Case cost
If EventType = #PB_EventType_LostFocus ; On lost focus of cost input, validate (warning only)
Validate_Cost(GetGadgetText(cost), "Warning")
EndIf
EndSelect
EndSelect
ForEver
CloseWindow(win.i)
End
