Page 1 of 1
StringGadget & onchange event
Posted: Tue Feb 24, 2026 3:43 pm
by captain_skank
OK, this is probably obvious but the anser is avoiding me.
I'm using a barcode scanner to populate a string gadget, none of the barcodes have end of line indicators so i have to go on length.
I'm using the on change event but the barcodes can be of varying lengths ( usualy between 7 and 10 characters ).
So how do/can i indicate that the barcode scan is completed ?
Any pointers or help appreciated.
Captain S.
Re: StringGadget & onchange event
Posted: Tue Feb 24, 2026 4:03 pm
by ebs
Depending on the type of scanner you're using, there is usually an option to set an "end of scan" character that the scanner adds for just this purpose.
Re: StringGadget & onchange event
Posted: Tue Feb 24, 2026 4:31 pm
by Mindphazer
To complete ebs's answer, usually you can set an option on the scanner to produce a LF or/and CRLF at the end of the scan
Re: StringGadget & onchange event
Posted: Tue Feb 24, 2026 4:37 pm
by captain_skank
Unfortunately this does not have the expected result.
In notepad it's fine but in a string gadget the CR LF are stripped out at some point.
Re: StringGadget & onchange event
Posted: Tue Feb 24, 2026 4:55 pm
by mk-soft
I have set my hand scanner to send me an end of text signal. (#ETX$)
Code: Select all
;-TOP
Procedure UpdateWindow()
Protected dx, dy
dx = WindowWidth(0)
dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
; Resize Gadgets
EndProcedure
Procedure Main()
Protected dx, dy, temp.s
#WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
; MenuBar
CreateMenu(0, WindowID(0))
MenuTitle("File")
; StatusBar
CreateStatusBar(0, WindowID(0))
AddStatusBarField(#PB_Ignore)
; Gadgets
dx = WindowWidth(0)
dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
StringGadget(0, 10, 10, 200, 25, "")
SetGadgetText(0, #ETX$)
; Bind Events
BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
; Main Loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Select EventWindow()
Case 0
Break
EndSelect
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case 0
Select EventType()
Case #PB_EventType_Change
temp = GetGadgetText(0)
If Right(temp, 1) = #ETX$
Debug "End Scan"
EndIf
EndSelect
EndSelect
EndSelect
ForEver
EndIf
EndProcedure : Main()
Re: StringGadget & onchange event
Posted: Tue Feb 24, 2026 5:14 pm
by Piero
viewtopic.php?t=51634
…or perhaps it will work with FindString() or Right() and/or by using an (hidden?) EditorGadget…
Re: StringGadget & onchange event
Posted: Tue Feb 24, 2026 6:01 pm
by Matheos
captain_skank wrote: Tue Feb 24, 2026 3:43 pm
I'm using a barcode scanner to populate a string gadget, none of the barcodes have end of line indicators so i have to go on length. I'm using the on change event but the barcodes can be of varying lengths ( usualy between 7 and 10 characters ). So how do/can i indicate that the barcode scan is completed ?
You could just use a timeout period, during which you don't consider the string input as being yet complete. Only following the timeout do you regard the string as complete, in the below case 750ms. You're giving the barcode reader plenty of time to transmit all its characters, regardless of the length of input. Hope it helps, it's the way I've often written serial I/O comms systems.
Code: Select all
;
; Example of barcode input with 750ms timeout to trigger complete code
; Run with debugger
EnableExplicit
Define event.i, evgadget.i, evtype.i, inpflag.i
Define t1.q
#TIMEOUT = 750 ; Desired ms timeout for barcode reader
If OpenWindow(0, 0, 0, 500, 200, "Barcode input change timeout", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(1, 15, 15, 100, 25, "Barcode input")
StringGadget(2, 160, 12, 306, 20, "")
SetActiveGadget(2)
Repeat
event.i = WaitWindowEvent(50) ; Loop timeout required, to allow us to continue to detect no further input
If event.i
evgadget.i = EventGadget()
evtype.i = EventType()
If evgadget.i = 2 And evtype.i = #PB_EventType_Change
t1.q = ElapsedMilliseconds() ; Set the last input time
inpflag.i = #True ; Received at least some input
EndIf
EndIf
If inpflag.i And ElapsedMilliseconds() > t1.q + #TIMEOUT
Debug "Complete barcode : " + GetGadgetText(2)
t1.q = 0 ; Reset time, accommodating next input
inpflag.i = #False ; Don't keep repeating
EndIf
Until Event = #PB_Event_CloseWindow
EndIf