Button needs 2 clicks?

Just starting out? Need help? Post your questions and find answers here.
TheCorruptor
New User
New User
Posts: 9
Joined: Fri Aug 28, 2009 12:35 am
Location: PA>USA

Button needs 2 clicks?

Post 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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Button needs 2 clicks?

Post 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.
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Button needs 2 clicks?

Post by ts-soft »

Please read the helpfile and don't use API if you a not firm on this
TheCorruptor
New User
New User
Posts: 9
Joined: Fri Aug 28, 2009 12:35 am
Location: PA>USA

Re: Button needs 2 clicks?

Post 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..
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Button needs 2 clicks?

Post 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.
oh... and have a nice day.
Post Reply