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

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

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

Post 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?
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

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

Post by helpy »

Additonally try to check the EventType() ...
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

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

Post 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()
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

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

Post 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
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

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

Post by cas »

Replace #PB_EventType_LeftClick with #PB_EventType_Change
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

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

Post by Columbo »

@cas
That works! Thank you very much.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

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

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