StringGadget & onchange event

Just starting out? Need help? Post your questions and find answers here.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 646
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

StringGadget & onchange event

Post 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.
ebs
Enthusiast
Enthusiast
Posts: 566
Joined: Fri Apr 25, 2003 11:08 pm

Re: StringGadget & onchange event

Post 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.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 532
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: StringGadget & onchange event

Post 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
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 26.1 - Iphone 17 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 646
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: StringGadget & onchange event

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6595
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: StringGadget & onchange event

Post 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()
My Projects EventDesigner V3 / ThreadToGUI / OOP-BaseClass / Windows: Module ActiveScript
PB v3.30 / v5.75 - OS Mac Mini - VM Window Pro / Linux Ubuntu
Downloads on my OneDrive
User avatar
Piero
Addict
Addict
Posts: 1204
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: StringGadget & onchange event

Post by Piero »

viewtopic.php?t=51634

…or perhaps it will work with FindString() or Right() and/or by using an (hidden?) EditorGadget…
Matheos
User
User
Posts: 11
Joined: Sat Dec 13, 2025 9:23 pm

Re: StringGadget & onchange event

Post 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
Post Reply