Page 1 of 1

gadgets seem to work twice

Posted: Wed May 17, 2023 10:34 am
by MrCor
Hi guys and dolls,

I am not an advanced programmer, so every now and then I get a simple problem that still seems to be too simple for me. And so... help.

I have written the following code:

Code: Select all

;- hoofdlus
Repeat

  Event = WaitWindowEvent(20)
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case 11 ; document geselecteerd
          Gosub SelectieGadget11
          
        Case 12 ; reset gadget 11 - documenten
          Gosub ResetGadget11
          
        Case 21 ; contact geselecteerd
          Gosub SelectieGadget21
          
        Case 22 ; reset gadget 21 - contacten
          Gosub ResetGadget21
          
        Case 23 ; zoeken contacten
          Gosub ZoekenContacten  
          
        Case 31 ; dossier geselecteerd
          Gosub SelectieGadget31
          
        Case 32 ; reset gadget 31 - dossiers
          Gosub ResetGadget31
          
        Case 33 ; zoeken dossiers
          Gosub ZoekenDossiers
          
        Case 41 ; omschrijving geselecteerd
          Gosub SelectieGadget41
          
        Case 42 ; reset gadget 41 - omschrijvingen
          Gosub ResetGadget41
          
        Case 43 ; zoeken omschrijving
          Gosub ZoekenOmschrijving
          
        Case 61 ; selectie bestanden in tmp
          Gosub SelectieGadget61
          
      EndSelect
    
  EndSelect

ForEver
Now so far there where no problems I noticed. But they was there all the time, I just had not noticed it.
I did notice it when in one of the subroutines something needed to be added. And there it was: the thing that had to be added, was added twice.
It looks as if the subroutine is executed twice... And no, nothing in the subroutine causes that.
So it looks as if this loop above executes the subroutine twice.

Is it a known problem? Don't think so, I must be doing something wrong, but what? Thanks in advance for your help and suggestions. I'm pretty shure it will be something simple I have overlooked.

Cheers, Cor



// Code Tags added (Kiffi)

Re: gadgets seem to work twice

Posted: Wed May 17, 2023 11:17 am
by mk-soft
1. Gosub is not a good method (outdated, better to write and call procedures).
2. Missed depending on the gadget the function EventType() after gadget deliver different events. #PB_EventType_... See PB-Help of Gadget

Re: gadgets seem to work twice

Posted: Wed May 17, 2023 11:21 am
by Mesa
Yes, it's known if you don't use the EventType() function inside the loop, for some gadgets.

Do you still program the "spagghetti" way, with gosub/Return?

Code: Select all

Goto spaghetti

clic_spaghetti:
 Debug "clic spaghetti"
 Return
 
spaghetti:
If OpenWindow(0, 0, 0, 220, 100, "Exemple...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    CanvasGadget(1, 10, 10, 200, 80)

   Repeat
     Event = WaitWindowEvent()
     
     Select Event
     
       Case #PB_Event_Gadget
         Select EventGadget()
           Case 1 
           	Debug "ho no, it triggers all the time !"
;            	Gosub clic_spaghetti
              Select EventType()
              	Case #PB_EventType_LeftClick
;               		Debug "clic"
              		Gosub clic_spaghetti
              EndSelect
              
         EndSelect
            
     EndSelect
   Until Event = #PB_Event_CloseWindow
 EndIf

M.

Re: gadgets seem to work twice

Posted: Wed May 17, 2023 11:34 am
by MrCor
Thanks for the suggestions, guys.

Using subs and returns... is that a technical or an ethical issue?

Re: gadgets seem to work twice

Posted: Wed May 17, 2023 11:42 am
by jacdelad
MrCor wrote: Wed May 17, 2023 11:34 am Using subs and returns... is that a technical or an ethical issue?
Depends on whether you're ok with us hating you or not. :wink:
But seriously: Use procedures. Otherwise you will come to a point where noone knows why this or that piece of code is called. Also you can use "temporary" resources in procedures, they are freed when leaving the procedure.

Re: gadgets seem to work twice

Posted: Wed May 17, 2023 11:48 am
by MrCor
I'm ok with you guys hating me... :wink:

You know, I am old-school. In the very past past I used to program in 3rd generation languages. Hard not to stick with what you know. That is, until things don't work out the way of those old-school things anymore.
I promise I will get familiar with procedures. Please don't hate me anymore :wink:
jacdelad wrote: Wed May 17, 2023 11:42 am
MrCor wrote: Wed May 17, 2023 11:34 am Using subs and returns... is that a technical or an ethical issue?
Depends on whether you're ok with us hating you or not. :wink:
But seriously: Use procedures. Otherwise you will come to a point where noone knows why this or that piece of code is called. Also you can use "temporary" resources in procedures, they are freed when leaving the procedure.

Re: gadgets seem to work twice

Posted: Wed May 17, 2023 1:14 pm
by mk-soft
We can reintroduce line numbers again. :mrgreen:
(Was great for jumps when you had inserted lines)

Re: gadgets seem to work twice

Posted: Wed May 17, 2023 3:23 pm
by jacdelad
mk-soft wrote: Wed May 17, 2023 1:14 pm We can reintroduce line numbers again. :mrgreen:
(Was great for jumps when you had inserted lines)
Jesus...
I only 38, but I also remember line numbers and jumps. I guess I'm old too...