Page 1 of 1

Button needs 2 clicks?

Posted: Sun Sep 27, 2009 4:34 pm
by TheCorruptor
When I run this code and click on the button the first time nothing happens..? But when I click on it the 2nd time the pop up window works and then every time I click on it after that, it works. Until I close the program and open it up again. Why is this?

Code: Select all

    If OpenWindow(0, 0, 0, 230, 90, "Event handling example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
      TextGadget(0, 10, 10, 280, 20, "I am a window")
      ButtonGadget  (2, 10, 40, 200, 20, "Check me")
      Repeat
        event = WaitWindowEvent() : #PB_Event_Gadget 
        If  event = #WM_LBUTTONDOWN And EventGadget() = 2
          MessageRequester("Pop up Win", "HELLO WORLD!")
          ;Debug event
        EndIf
      Until event = #PB_Event_CloseWindow
    EndIf
    End

Re: Button needs 2 clicks?

Posted: Sun Sep 27, 2009 4:42 pm
by srod
Yes the EventGadget() will not equal 2 until the button click notification has been sent and this happens only after a button up etc. From there EventGadget() remains at this value until some event arises from another gadget etc. You shouldn't really be using EventGadget() unless your WindowEvent() returns #PB_Event_Gadget.

Why would you be trying to trap a button click that way anyhow? It is not the best way when using a PB event loop.

Re: Button needs 2 clicks?

Posted: Sun Sep 27, 2009 4:45 pm
by ts-soft
Please read the helpfile and don't use API if you a not firm on this

Re: Button needs 2 clicks?

Posted: Sun Sep 27, 2009 4:59 pm
by TheCorruptor
Thanks srod, this was really bugging me. I was trying it this way because I am trying to make a very simple generic event handler. example (psuedo code)

Procedure OnClick(gadgetID)
If button click equals gadget ID
do something
EndProcedure

OnClick(1) MessageRequester("Pop up Win", "HELLO WORLD!")

I am trying to do something simple besides going the EasyVent route and also the standard Select case way. I havent seen any examples like this so I thought I would give it a try. Plus it would be easy to add event handling for other controls if needed. I would just like a simple OnClick command to handle basic controls etc..

Re: Button needs 2 clicks?

Posted: Sun Sep 27, 2009 7:43 pm
by Kaeru Gaman
if you want OnClick(GadgetID), do not use ButtonDown but #PB_Event_Gadget.

as ts said: messing with API when you do not exactly know what you do is a bad idea.