Page 1 of 1

Why does response to Select Case repeat 2 - 3 times?

Posted: Fri Jun 11, 2021 6:59 pm
by Columbo
I am trying to determine which tab has been selected in a PanelGadget. Here is how I am doing it:

Code: Select all

  Repeat
      Select WaitWindowEvent()                  ;Wait for an event 
          Case #PB_Event_CloseWindow        ;Close window if "X" is clicked.
            run = 1                         ;Set run flag To 1             
          
           Case #PB_Event_Gadget      ;Check for gadget event
             Select EventGadget()
                 
               Case #mainPanel
                 tab = GetGadgetState(#mainPanel)
                  Select tab
                    Case 0
                      Debug "tab = 0"                     
                    Case 1
                      Debug "tab = 1"
                     Case 2
                      Debug "tab = 2"                     
                    Case 3
                      Debug "tab = 03"                                  
                 EndSelect                 
It works except it seems to repeat 2 or 3 times. Here is the result of the Debug after selecting the tab 2, tab 3 and tab 1 in the panel.

Code: Select all

tab = 2
tab = 2
tab = 2
tab = 3
tab = 3
tab = 3
tab = 1
tab = 1
tab = 1
Why?

Re: Why does response to Select Case repeat 2 - 3 times?

Posted: Fri Jun 11, 2021 7:04 pm
by helpy
Additonally try to check the EventType() ...

Re: Why does response to Select Case repeat 2 - 3 times?

Posted: Fri Jun 11, 2021 7:06 pm
by Keya
Probably because you're responding to EVERY event that's sent to that window/gadget, which isn't just the "clicked on/activated" message.

Code: Select all

 Case #PB_Event_Gadget      ;Check for gadget event
             Select EventGadget()
You want to go a level deeper and check what TYPE of message it is - EventType()

Re: Why does response to Select Case repeat 2 - 3 times?

Posted: Fri Jun 11, 2021 8:49 pm
by Columbo
Guess I'm doing it wrong. I tried this but now I'm not getting a Debug window.

Code: Select all

Repeat
      Select WaitWindowEvent()                  ;Wait for an event 
          Case #PB_Event_CloseWindow        ;Close window if "X" is clicked.
            run = 1                         ;Set run flag To 1             
          
           Case #PB_Event_Gadget      ;Check for gadget event
             Select EventGadget()                 
               Case #mainPanel
                 Select EventType()
                   Case #PB_EventType_LeftClick
                       tab = GetGadgetState(#mainPanel)
                       Select tab
                          Case 0
                            Debug "tab = 0"                 
                          Case 1
                            Debug "tab = 1" 
                          Case 2
                           Debug "tab = 2"                 
                        Case 3
                           Debug "tab = 3"                                   
                       EndSelect
                   EndSelect

Re: Why does response to Select Case repeat 2 - 3 times?

Posted: Fri Jun 11, 2021 8:56 pm
by cas
Replace #PB_EventType_LeftClick with #PB_EventType_Change

Re: Why does response to Select Case repeat 2 - 3 times?

Posted: Fri Jun 11, 2021 9:02 pm
by Columbo
@cas
That works! Thank you very much.

Re: Why does response to Select Case repeat 2 - 3 times?

Posted: Fri Jun 11, 2021 9:31 pm
by Olli
Hello Colombo !

You can insert a procedure managing your panel object, and it exists, since 2014, a function which links automatically your procedure with the queue what WaitWindowEvent() is treating. This linking function is called :

Code: Select all

BindGadgetEvent(myGadget, @ItsProcedure(), TheEventTypeHappened)
It seems complex, but look the main loop : no If, no Select... Very simple, finally !

Code: Select all

Procedure myPanel()
 Debug GetGadgetState(EventGadget() )
EndProcedure

  If OpenWindow(0, 0, 0, 322, 220, "PanelGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
    PanelGadget     (0, 8, 8, 306, 203)
      AddGadgetItem (0, -1, "Item 1")
        PanelGadget (1, 5, 5, 290, 166)
          AddGadgetItem(1, -1, "SubItem 1")
          AddGadgetItem(1, -1, "SubItem 2")
          AddGadgetItem(1, -1, "SubItem 3")
        CloseGadgetList()
      AddGadgetItem (0, -1,"Item 2")
        ButtonGadget(2, 10, 15, 80, 24,"SubItem 1")
        ButtonGadget(3, 95, 15, 80, 24,"SubItem 2")
    CloseGadgetList()
    
    BindGadgetEvent(1, @myPanel(), #Pb_EventType_Change)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf