Page 1 of 2

Event Handling: Buttons vs. StringGadgets

Posted: Thu Apr 07, 2016 4:42 am
by PB2004
I am testing a very simple framework for a basic knowledge tester. I have an outer loop to begin and end the program, and an inner loop to pose questions and evaluate answers. If the inner event loop uses buttons to indicate answers, I get the behavior I want. But if I want to have the user click directly on a stringGadget, my approach fails. It seems the stringGadgets generate more or different events and my code can't isolate left clicks properly.

Below are both code samples. Both run but only the button code works right. I would sure appreciate it if anybody can point out what I am doing wrong with the stringGadgets. Thanks for looking!

Here is the working code using buttons:

Code: Select all

;postwithbuttons.pb

;Test of an outer and inner event loop
;Click on begin button, then randomly on the other buttons. The textGadgets will report which button was pressed.

;- Window Constants
Enumeration
  #Window_mainForm
  #beginbutton
  #buttonA
  #buttonB
  #buttonC
  #text1
  #text2
EndEnumeration


Procedure.i Window_mainForm()
  If OpenWindow(#Window_mainForm,0,0,800,250,"Test with Buttons",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered|#PB_Window_Invisible)
    ButtonGadget(#beginbutton,0,0,60,20,"Begin")
    ButtonGadget(#buttonA, 100, 30, 150, 60, "A")
    ButtonGadget(#buttonB, 300, 30, 150, 60, "B")
    ButtonGadget(#buttonC, 500, 30, 150, 60, "C")
    TextGadget(#text1, 225,150,300,20,"",#PB_Text_Border|#PB_Text_Center)
    TextGadget(#text2, 225,180,300,20,"",#PB_Text_Border|#PB_Text_Center)
    
    HideWindow(#Window_mainForm,0)
    ProcedureReturn WindowID(#Window_mainForm)
  EndIf
EndProcedure


If Window_mainForm()
  
  Define quitmainForm=0
    SetGadgetText(#text2,"hit Begin button to start")

  Repeat  ;main outer loop

    Select WaitWindowEvent()
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #beginbutton
            DisableGadget(#beginbutton,1)
                        SetGadgetText(#text1, "")
                        SetGadgetText(#text2, "Left Click on a button (you get 4 tries)")
            For i = 1 To 4
              proceed = 0
              Repeat    ;inner loop for begin button behavior
                
                Select WaitWindowEvent() 
                  Case #PB_Event_Gadget
                    Select EventGadget()
                        
                      Case #buttonA
                        proceed = 1
                        SetGadgetText(#text1, "on try " + Str(i))
                        SetGadgetText(#text2, "You pressed the A button")
                        
                      Case #buttonB
                        proceed = 1
                        SetGadgetText(#text1, "on try " + Str(i))
                        SetGadgetText(#text2, "You pressed the B button")
                        
                      Case #buttonC
                        proceed = 1
                        SetGadgetText(#text1, "on try " + Str(i))
                        SetGadgetText(#text2, "You pressed the C button")
                        
                    EndSelect  ;EventGadget()  inner
                EndSelect  ;WaitWindowEvent()  inner
              Until proceed
              
            Next i
            Delay(1000)
            DisableGadget(#beginbutton,0)
            SetGadgetText(#text1, "Yer done!")
            SetGadgetText(#text2, "Press the Begin button for even more thrills.")
            
        EndSelect  ;EventGadget()  outer
        
      Case #PB_Event_CloseWindow
        If WindowID=#Window_mainForm
          quitmainForm=1
        EndIf
        
    EndSelect  ;WaitWindowEvent()  outer
  Until quitmainForm
  
  CloseWindow(#Window_mainForm)
EndIf  ;window


Here is the non-working code using stringGadgets:

Code: Select all

;postwithstringgadgets.pb

;Test of an outer and inner event loop
;Click on begin button, then randomly on the stringGadgets. 
;The textGadgets will report which stringGadget was clicked on.
;Unlike buttons, the stringGadgets seem to generate additional events.
;The event processing in the code below does not handle them properly.


;- Window Constants
Enumeration
  #Window_mainForm
  #beginbutton
  #stringA
  #stringB
  #stringC
  #text1
  #text2
EndEnumeration


Procedure.i Window_mainForm()
  If OpenWindow(#Window_mainForm,0,0,800,250,"Test with StringGadgets",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered|#PB_Window_Invisible)
    
    ButtonGadget(#beginbutton,0,0,60,20,"Begin")

    StringGadget(#stringA,100, 30, 150, 60, "A",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)    
    StringGadget(#stringB,300, 30, 150, 60, "B",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)    
    StringGadget(#stringC,500, 30, 150, 60, "C",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)    
    
    TextGadget(#text1, 225,150,300,20,"",#PB_Text_Border|#PB_Text_Center)
    TextGadget(#text2, 225,180,300,20,"",#PB_Text_Border|#PB_Text_Center)
    
    HideWindow(#Window_mainForm,0)
    ProcedureReturn WindowID(#Window_mainForm)
  EndIf
EndProcedure


If Window_mainForm()
  
  Define quitmainForm=0
  

  Repeat  ;main outer loop
    SetGadgetText(#text2,"hit Begin button to start")

    Select WaitWindowEvent()
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #beginbutton
            DisableGadget(#beginbutton,1)
                        SetGadgetText(#text1, "")
                        SetGadgetText(#text2, "Left Click on a string gadget (you get 4 tries)")
            For i = 1 To 4
              proceed = 0
              Repeat    ;inner loop for begin button behavior
                
                Select WaitWindowEvent() 
                  Case #PB_Event_Gadget
                    Select EventGadget()
                      Case #stringA
                        proceed = 1
                        SetGadgetText(#text1, "on try " + Str(i))
                        SetGadgetText(#text2, "You pressed the A string gadget")
                        
                      Case #stringB
                        proceed = 1
                        SetGadgetText(#text1, "on try " + Str(i))
                        SetGadgetText(#text2, "You pressed the B string gadget")
                        
                      Case #stringC
                        proceed = 1
                        SetGadgetText(#text1, "on try " + Str(i))
                        SetGadgetText(#text2, "You pressed the C string gadget")
                        
                    EndSelect  ;EventGadget()  inner
                EndSelect  ;WaitWindowEvent()  inner
              Until proceed
              
            Next i
            Delay(1000)
            DisableGadget(#beginbutton,0)
            SetGadgetText(#text1, "Yer done!")
            SetGadgetText(#text2, "Press the Begin button again for even more thrills.")
            
        EndSelect  ;EventGadget()  outer
        
      Case #PB_Event_CloseWindow
        If WindowID=#Window_mainForm
          quitmainForm=1
        EndIf
        
    EndSelect  ;WaitWindowEvent()  outer
  Until quitmainForm
  
  CloseWindow(#Window_mainForm)
EndIf  ;window



Re: Event Handling: Buttons vs. StringGadgets

Posted: Thu Apr 07, 2016 5:00 am
by collectordave
In your innerinner loop try

Code: Select all

  Case #stringA
    If EventType() = #PB_EventType_Focus  
      proceed = 1
      SetGadgetText(#text1, "on try " + Str(i))
      SetGadgetText(#text2, "You pressed the A string gadget")
    EndIf
It is all to do with what events each gadget reacts to.

Re: Event Handling: Buttons vs. StringGadgets

Posted: Thu Apr 07, 2016 7:06 am
by infratec
Btw.:

use something like that:

Code: Select all

Event = WaitWindowEvent()
And use the variable for further handling.
Because I think you are waiting for the next event and don't handle the current one.

Bernd

Re: Event Handling: Buttons vs. StringGadgets

Posted: Thu Apr 07, 2016 3:33 pm
by PB2004
Thanks CollectorDave! The code below now works. I did add a SetActiveGadget(-1) to lose focus so that the same stringGadget could be clicked in succession.

Infratec, Looking thru the forums, I have seen it done both ways, but it did not seem to have any effect when I substituted "event = WaitWindowEvent()".

Code: Select all

;postwithstringgadgets - fix1.pb

;Test of an outer and inner event loop
;Click on begin button, then randomly on the stringGadgets. 
;The textGadgets will report which stringGadget was clicked on.
;StringGadgets don't respond to leftclicks (see help). 
;They do generate events for the focus that a leftclick provides.  Added a "lose focus" line.


;- Window Constants
Enumeration
  #Window_mainForm
  #beginbutton
  #stringA
  #stringB
  #stringC
  #text1
  #text2
EndEnumeration


Procedure.i Window_mainForm()
  If OpenWindow(#Window_mainForm,0,0,800,250,"Test with StringGadgets",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered|#PB_Window_Invisible)
    
    ButtonGadget(#beginbutton,0,0,60,20,"Begin")
    
    StringGadget(#stringA,100, 30, 150, 60, "A",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)    
    StringGadget(#stringB,300, 30, 150, 60, "B",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)    
    StringGadget(#stringC,500, 30, 150, 60, "C",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)    
    
    TextGadget(#text1, 225,150,300,20,"",#PB_Text_Border|#PB_Text_Center)
    TextGadget(#text2, 225,180,300,20,"",#PB_Text_Border|#PB_Text_Center)
    
    HideWindow(#Window_mainForm,0)
    ProcedureReturn WindowID(#Window_mainForm)
  EndIf
EndProcedure


If Window_mainForm()
  
  Define quitmainForm=0
  
  
  Repeat  ;main outer loop
    SetGadgetText(#text2,"hit Begin button to start")
    
    Select WaitWindowEvent()
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #beginbutton
            DisableGadget(#beginbutton,1)
            SetGadgetText(#text1, "")                      
            SetGadgetText(#text2, "Left Click on a string gadget (you get 4 tries)")            
            For i = 1 To 4
              proceed = 0
              Repeat    ;inner loop for begin button behavior
                
                Select WaitWindowEvent() 
                  Case #PB_Event_Gadget
                    Select EventGadget()
                        
                      Case #stringA
                        If EventType() = #PB_EventType_Focus  
                          proceed = 1
                          SetGadgetText(#text1, "on try " + Str(i))
                          SetGadgetText(#text2, "You pressed the A string gadget")
                          SetActiveGadget(-1)
                        EndIf                                            
                        
                      Case #stringB
                        If EventType() = #PB_EventType_Focus  
                          proceed = 1
                          SetGadgetText(#text1, "on try " + Str(i))
                          SetGadgetText(#text2, "You pressed the B string gadget")
                          SetActiveGadget(-1)
                        EndIf
                        
                      Case #stringC
                        If EventType() = #PB_EventType_Focus  
                          proceed = 1
                          SetGadgetText(#text1, "on try " + Str(i))
                          SetGadgetText(#text2, "You pressed the C string gadget")
                          SetActiveGadget(-1)
                        EndIf
                    EndSelect  ;EventGadget()  inner
                EndSelect      ;WaitWindowEvent()  inner
              Until proceed
              
            Next i
            Delay(500)
            DisableGadget(#beginbutton,0)
            SetGadgetText(#text1, "Yer done!")                      
            SetGadgetText(#text2, "Press the Begin button again for even more thrills.")
            
        EndSelect  ;EventGadget()  outer
        
      Case #PB_Event_CloseWindow
        If WindowID=#Window_mainForm
          quitmainForm=1
        EndIf
        
    EndSelect  ;WaitWindowEvent()  outer
  Until quitmainForm
  
  CloseWindow(#Window_mainForm)
EndIf  ;window



Re: Event Handling: Buttons vs. StringGadgets

Posted: Thu Apr 07, 2016 3:50 pm
by infratec
You have simply to read the help for WaitWindowEvent().

It waits for an event.
If it comes back, an event happend.
If you call it again, it waits for the next event.

So if you call it twice you get the next event, but not the current again.

I never so a code which calls WaitWindowEvent() twice to handle one event.

You can call EventGadget() or EventType() more then once, but not WaitWindowEvent()

Bernd

Re: Event Handling: Buttons vs. StringGadgets

Posted: Thu Apr 07, 2016 3:55 pm
by infratec
Oh,

I see now what you have done:
An outer and an inner loop.

Very bad solution.

You stuck in one of the loops.

A golden rule for windo/PB programming:

Only one WaitWindowEvent() loop in your whole program.

Else you run into trouble.

You see this if you press 'Begin' and than try to close your window.


Bernd

Re: Event Handling: Buttons vs. StringGadgets

Posted: Thu Apr 07, 2016 4:07 pm
by infratec
Your example in a non blocking version:

Code: Select all

EnableExplicit

Enumeration
  #Window_mainForm
  #beginbutton
  #stringA
  #stringB
  #stringC
  #text1
  #text2
EndEnumeration




Define quitmainForm, Event, Tries


If OpenWindow(#Window_mainForm,0,0,800,250,"Test with StringGadgets",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
  
  ButtonGadget(#beginbutton,0,0,60,20,"Begin")
  
  StringGadget(#stringA,100, 30, 150, 60, "A",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)   
  DisableGadget(#stringA, #True)
  StringGadget(#stringB,300, 30, 150, 60, "B",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)   
  DisableGadget(#stringB, #True)
  StringGadget(#stringC,500, 30, 150, 60, "C",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)   
  DisableGadget(#stringC, #True)
  
  
  
  TextGadget(#text1, 225,150,300,20,"",#PB_Text_Border|#PB_Text_Center)
  TextGadget(#text2, 225,180,300,20,"hit Begin button to start",#PB_Text_Border|#PB_Text_Center)
  
  
  Repeat
    
    Event = WaitWindowEvent()
    
    Select Event
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #beginbutton
            DisableGadget(#beginbutton, #True)
            SetGadgetText(#text1, "")                     
            SetGadgetText(#text2, "Left Click on a string gadget (you get 4 tries)")
            DisableGadget(#stringA, #False)
            DisableGadget(#stringB, #False)
            DisableGadget(#stringC, #False)
            
          Case #stringA
            If EventType() = #PB_EventType_Focus 
              Tries + 1
              SetGadgetText(#text1, "on try " + Str(Tries))
              SetGadgetText(#text2, "You pressed the A string gadget")
              SetActiveGadget(-1)
            EndIf                                           
            
          Case #stringB
            If EventType() = #PB_EventType_Focus
              Tries + 1
              SetGadgetText(#text1, "on try " + Str(Tries))
              SetGadgetText(#text2, "You pressed the B string gadget")
              SetActiveGadget(-1)
            EndIf
            
          Case #stringC
            If EventType() = #PB_EventType_Focus 
              Tries + 1
              SetGadgetText(#text1, "on try " + Str(Tries))
              SetGadgetText(#text2, "You pressed the C string gadget")
              SetActiveGadget(-1)
            EndIf
            
        EndSelect
        
        If Tries = 4
          DisableGadget(#beginbutton, #False)
          DisableGadget(#stringA, #True)
          DisableGadget(#stringB, #True)
          DisableGadget(#stringC, #True)
          SetGadgetText(#text1, "")
          SetGadgetText(#text2, "hit Begin button To start")
          Tries = 0
        EndIf
        
      Case #PB_Event_CloseWindow
        quitmainForm = #True
        
    EndSelect  ;WaitWindowEvent()  outer
  Until quitmainForm
  
EndIf  ;window
Bernd

Re: Event Handling: Buttons vs. StringGadgets

Posted: Fri Apr 08, 2016 2:17 pm
by PB2004
Thank you infratech for the insights and especially the single loop example. This encourages me.

I have to say the third block code "fix" worked there but not on my larger program. Not sure what the difference is yet.

I come from a DarkBasicPro background and I never encountered any difficulties with events. Still, I want to move up to PureBasic. What i read in the help was "WWE() can only be called once per event loop", so I just used more loops. But you are saying only one event loop per program. What about using child windows - can they have their own event loops? Or is that a bad idea also?

I do not want to distort the intended use of WWE() but I am curious. While traversing the inner loop, the code, of course, never touches the outer WWE(). Was the outer WWE() still active and "listening" (and grabbing events)? If so I see how that could be a problem.

Re: Event Handling: Buttons vs. StringGadgets

Posted: Sat Apr 09, 2016 5:27 am
by collectordave
I can only relate my experience. I moved up to Pure Basic last year from VB where the event loop is handled for you.

As the event loop is how your programme communicates with the user and with the OS you really have to keep an eye on what is happening there. If you fail to pick up the events for a time your programme can be seen to be unresponsive and you get the dreaded (Not Responding) message. You can use more than one loop but you have to be aware that when you are in one loop the main loop is not picking up events and so does not deal with them your "inner" loop must deal with all events in the way you want your programme to deal with them. So if you open a child window with it's own event loop the main window does not get a look in until the child window closes. Better if possible to use main event loop and route events to the correct event handling routine.

It is not recommended as you can get into a right old mess but allowing you access to the event loop is one of the powerfull features of PB.

Hope this helps

Re: Event Handling: Buttons vs. StringGadgets

Posted: Sat Apr 09, 2016 7:34 am
by PB2004
Thanks collectordave! Every post helped here. I never realized my previous experience was so managed! Seems like a single loop is the way to go.

Re: Event Handling: Buttons vs. StringGadgets

Posted: Sat Apr 09, 2016 5:07 pm
by netmaestro
Put that down before you hurt yourself. A modern PureBasic program should have one event loop: Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow and everything else is handled with BindEvent() and BindGadgetEvent(). Much better structure, readability and maintainability, not to mention performance.

Re: Event Handling: Buttons vs. StringGadgets

Posted: Mon Apr 11, 2016 8:53 pm
by PB2004
Umm, I think I am already bleeding....but thanks anyway netmaestro.

This approach does look promising. My first newbile attempt below has a couple of problems. The first is that I can't get to a fourth try. Am I supposed to unbind somehow? The second is that I can't unfocus to register a successive click (focus) on the same stringGadget. I'd be grateful, Maestro, if you could show me how it is done. Does anybody have some cool techniques to unfocus an object? I can't seem to find any.

Thanks in advance, despite the blood loss.


Code: Select all

;StringGadgets binding solution2

EnableExplicit

Enumeration
  #Window_mainForm 
  #string1
  #string2
  #string3

EndEnumeration


Define Event
Global Tries = 0


Procedure StringGadgetHandler()
  Tries + 1
  Debug "You clicked on gadget " + EventGadget() + " on try " + Str(Tries)
  SetActiveGadget(-1)
EndProcedure

If OpenWindow(#Window_mainForm,0,0,800,250,"Test with StringGadgets",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)

  
  StringGadget(#string1,100, 30, 150, 60, "stringGadget 1",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string1,@StringGadgetHandler(),#PB_EventType_Focus)  

  StringGadget(#string2,300, 30, 150, 60, "stringGadget 2",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string2,@StringGadgetHandler(),#PB_EventType_Focus)  

  StringGadget(#string3,500, 30, 150, 60, "stringGadget 3",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string3,@StringGadgetHandler(),#PB_EventType_Focus)  



  
  Repeat
    
    Event = WaitWindowEvent()

        If Tries = 4
          MessageRequester("","That was your fourth try. Resetting.")
          Tries = 0
        EndIf
        

  Until Event = #PB_Event_CloseWindow
  
EndIf  ;window

Re: Event Handling: Buttons vs. StringGadgets

Posted: Mon Apr 11, 2016 10:39 pm
by infratec
Yes, that's an other pitfall of this Bind stuff.

Code: Select all

EnableExplicit

Enumeration
  #Window_mainForm
  #string1
  #string2
  #string3
EndEnumeration


Enumeration #PB_Event_FirstCustomValue
  #Own_Event_Deselect
EndEnumeration


Define Event
Global Tries


Procedure StringGadgetHandler()
  Tries + 1
  Debug "You clicked on gadget " + Str(EventGadget()) + " on try " + Str(Tries)
  PostEvent(#Own_Event_Deselect)
EndProcedure


If OpenWindow(#Window_mainForm,0,0,800,250,"Test with StringGadgets",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
  
  StringGadget(#string1,100, 30, 150, 60, "stringGadget 1",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string1,@StringGadgetHandler(),#PB_EventType_Focus) 
  
  StringGadget(#string2,300, 30, 150, 60, "stringGadget 2",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string2,@StringGadgetHandler(),#PB_EventType_Focus) 
  
  StringGadget(#string3,500, 30, 150, 60, "stringGadget 3",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string3,@StringGadgetHandler(),#PB_EventType_Focus) 
  
  Repeat
    
    Event = WaitWindowEvent()
    
    If Event = #Own_Event_Deselect
      
      SetActiveGadget(-1)
      
      If Tries = 4
        MessageRequester("","That was your fourth try. Resetting.")
        Tries = 0
      EndIf
      
    EndIf
    
  Until Event = #PB_Event_CloseWindow
  
EndIf
Bind is like a thread.
Next golden rule:
Never do GUI stuff in a thread or bind procedure.
It can block.

Bernd

Re: Event Handling: Buttons vs. StringGadgets

Posted: Wed Apr 13, 2016 2:21 pm
by PB2004
Thanks infratech! Your golden rules are appreciated. The postevent might be getting a little exotic but good to see. I tried to simplify and found that, indeed, the GUI statements cause problems in the binding procedure.

Code: Select all

;StringGadgets binding solution1simple2.pb

EnableExplicit
Enumeration
  #Window_mainForm 
  #string1
  #string2
  #string3
  
EndEnumeration
Define Event
Global Tries = 0

Procedure StringGadgetHandler()
  
  Tries + 1
  Debug "You clicked on gadget " + EventGadget() + " on try " + Str(Tries)
  
  ;MessageRequester("","You clicked on gadget " + EventGadget() + " on try " + Str(Tries))    ;generates extra events
  ;SetActiveGadget(-1)    ;doesn't work here
EndProcedure

If OpenWindow(#Window_mainForm,0,0,800,250,"Test with StringGadgets",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
  
  StringGadget(#string1,100, 30, 150, 60, "stringGadget 1",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string1,@StringGadgetHandler(),#PB_EventType_Focus)  
  
  StringGadget(#string2,300, 30, 150, 60, "stringGadget 2",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string2,@StringGadgetHandler(),#PB_EventType_Focus)  
  
  StringGadget(#string3,500, 30, 150, 60, "stringGadget 3",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string3,@StringGadgetHandler(),#PB_EventType_Focus)  
  
  
  Repeat

    Event = WaitWindowEvent()
    
    SetActiveGadget(-1)    ;works here

    If Tries = 4
      MessageRequester("","No more clicks for you. Resetting.")
      Tries = 0
    EndIf

  Until Event = #PB_Event_CloseWindow

EndIf

Re: Event Handling: Buttons vs. StringGadgets

Posted: Thu May 12, 2016 1:55 pm
by PB2004
Believe it would be better to bracket the SetActiveGadget(-1) in the main loop with event type checking...

Code: Select all

EnableExplicit
Enumeration
  #Window_mainForm 
  #string1
  #string2
  #string3
EndEnumeration

Define Event

Global Tries = 0

Procedure StringGadgetHandler()
  Tries + 1
  Debug "You clicked on gadget " + EventGadget() + " on try " + Str(Tries)
EndProcedure

If OpenWindow(#Window_mainForm,0,0,800,250,"Test with StringGadgets",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
  
  StringGadget(#string1,100, 30, 150, 60, "stringGadget 1",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string1,@StringGadgetHandler(),#PB_EventType_Focus)  
  
  StringGadget(#string2,300, 30, 150, 60, "stringGadget 2",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string2,@StringGadgetHandler(),#PB_EventType_Focus)  
  
  StringGadget(#string3,500, 30, 150, 60, "stringGadget 3",#ES_MULTILINE|#ES_AUTOVSCROLL|#ESB_DISABLE_LEFT|#ESB_DISABLE_RIGHT|#ES_READONLY)
  BindGadgetEvent(#string3,@StringGadgetHandler(),#PB_EventType_Focus)  
  
  
  Repeat

    Event = WaitWindowEvent()
    
    If EventType() = #PB_EventType_Focus
      SetActiveGadget(-1)
    EndIf

    If Tries = 4
      MessageRequester("","No more clicks for you. Resetting.")
      Tries = 0
    EndIf

  Until Event = #PB_Event_CloseWindow

EndIf