Sorry, my mind has gone blank

Just starting out? Need help? Post your questions and find answers here.
SniffTheGlove
Enthusiast
Enthusiast
Posts: 122
Joined: Sat Nov 19, 2011 6:51 pm

Sorry, my mind has gone blank

Post by SniffTheGlove »

Hi,

I usually write console apps or simple windowed apps where every routine is run from a button press.

Now I am doing this simple window app where once Start button is pressed the routine with continue forever.
I have tried various things but none successful.
I tried searching these forums but just got page after page of other stuff so I am not able to make the search query more precise.

Now how do I detect the stop button being pressed once the start is running.
I have a simple example to illustrate. Press Start, then press stop.

Code: Select all

If OpenWindow(0, 100, 200, 80, 60, "Test...")
  ButtonGadget(2, 10, 1,  75, 25, "Start")
  ButtonGadget(3, 10, 30,  75, 25, "Stop")
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Gadget
      If EventGadget() = 2
        Repeat
          Debug Date() 
          ForEver
        EndIf
        If EventGadget() = 3
        Event = #PB_Event_CloseWindow
      EndIf
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf
End
Please help a a blind dummy to see the light :-)
Thanks
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

Re: Sorry, my mind has gone blank

Post by HanPBF »

Only a fix.

Repeat ... ForEver is not an event loop but an endless loop in itself ("forEver").

Code: Select all

If OpenWindow(0, 100, 200, 80, 60, "Test...")
  ButtonGadget(2, 10, 1,  75, 25, "Start")
  ButtonGadget(3, 10, 30,  75, 25, "Stop")
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Gadget
      If EventGadget() = 2
        Repeat
          Debug Date()
          
	          if WaitWindowEvent()=#PB_Event_Gadget and EventGadget()=3
	          	break
                      Event = #PB_Event_CloseWindow
	          endif
	          
          ForEver
        EndIf
        If EventGadget() = 3
        Event = #PB_Event_CloseWindow
      EndIf
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf
End
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Sorry, my mind has gone blank

Post by RASHAD »

Code: Select all

If OpenWindow(0, 100, 200, 80, 60, "Test...",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  ButtonGadget(2, 10, 1,  75, 25, "Start")
  ButtonGadget(3, 10, 30,  75, 25, "Stop")
  
  AddWindowTimer(0,100,1000)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #PB_Event_Timer
        If Flag = 1
          Debug Date()
        EndIf
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 2
            Flag = 1
          
          Case 3
            Flag = 0
            
        EndSelect
    EndSelect
  Until Quit = 1
EndIf
End
Egypt my love
SniffTheGlove
Enthusiast
Enthusiast
Posts: 122
Joined: Sat Nov 19, 2011 6:51 pm

Re: Sorry, my mind has gone blank

Post by SniffTheGlove »

Thank you all for your help :-)
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sorry, my mind has gone blank

Post by infratec »

Golden Rule:

Use only on WaitWindowEvent() or WindowEvent() in your hole program :!:

Else you come into trouble. (Sooner or later)
Post Reply