Question about using Events.

Just starting out? Need help? Post your questions and find answers here.
jphoarau
Posts: 22
Joined: Thu Dec 28, 2023 4:30 am

Question about using Events.

Post by jphoarau »

Good morning,

I'm trying to create a small webapp and I'm having trouble when I try to use events on a small window that launches from the main window. I put the code here:

Code: Select all

;EnableExplicit

#Window = 0
#window1 = 1
#Menu = 0

Enumeration
  #btn
  #btn2
  #btn3
	#Str1
	#Str2
	#sound1
EndEnumeration

Global ww, hh

Define desktop = ExamineDesktops()
ww = DesktopWidth(desktop)
hh = DesktopHeight(desktop)

If ww < 400 Or hh < 400 : End : EndIf

Declare Handler2()

Procedure open_window_login()
  
  OpenWindow(#Window1, 0, 0, 130, 110, "login test", #PB_Window_ScreenCentered | #PB_Window_TitleBar)
  StringGadget(#Str1, 5, 5, 120, 30, "Nickname", #PB_String_PlaceHolder)
  StringGadget(#Str2, 5, 40, 120, 30, "Password", #PB_String_PlaceHolder)
  ButtonGadget(#btn2, 5, 75, 60, 30, "Ok")
  ButtonGadget(#btn3, 65, 75, 60, 30, "Cancel")
  
  BindGadgetEvent(#PB_Event_Gadget, @Handler2())

EndProcedure

Procedure Handler2()
  Debug "#btn2 = " + #btn2
  Debug "#btn3 = " + #btn3
  Event = EventGadget()
  Debug "Event = " + Event;This is where things go wrong. The gadget never equals 2 and I don't understand why.
  
  Select Event
    Case #btn2
        MessageRequester("Button click event on gadget #btn2")
    Case #btn3
        MessageRequester("Button click event on gadget #btn3");It never happens that way...    
    EndSelect
    
EndProcedure

Procedure Handler()
	DisplayPopupMenu(#Menu, WindowID(#Window))
EndProcedure

Procedure EventsMenu()
	Select EventMenu()
		Case 0 To 2
		  MessageRequester("Menu")
		Case 3
		  open_window_login();It is this choice that must be tested.
		Case 4
		  PlaySound(#sound1)
	EndSelect
EndProcedure


If  ww < hh
  OpenWindow(#Window, 0, 0, ww, hh, "test.sb", #PB_Window_Background)
Else
  OpenWindow(#Window, 0, 0, 500, 770, "test.sb", #PB_Window_ScreenCentered | #PB_Window_TitleBar)
EndIf

ButtonGadget(#btn, 5, 5, 30, 30, Chr($2630))
GadgetToolTip(#btn, "Menu")

If CreatePopupMenu(#Menu)
  MenuItem(0, "New")
  MenuItem(1, "Open")
  MenuItem(2, "Save as")
  MenuItem(3, "login");It is this choice that must be tested.
  MenuItem(4, "Play sound")
EndIf

If InitSound()
  LoadSound(#sound1, "./res/Recording_10.mp3")
EndIf

BindGadgetEvent(#btn, @Handler())
BindEvent(#PB_Event_Menu, @EventsMenu())

It's in the Handler2() procedure that things are wrong and I don't know why?

I would like to point out that before I had already programmed some small utilities in Purebasic and that I used the EventDesigner tool to manage my Events. And so, I don't really know how it works and it poses a problem for me in Spiderbasic because I no longer have either Formdesigner or Eventdesigner.

If you have a link to a tutorial on Events, I'm interested. Or a fairly complete code example that would help me understand?

Thank you in advance for your answers.
munfraid
Posts: 135
Joined: Sat Mar 24, 2018 1:33 pm

Re: Question about using Events.

Post by munfraid »

In line 34 use

Code: Select all

BindEvent(#PB_Event_Gadget, @Handler2())
instead of

Code: Select all

BindGadgetEvent(#PB_Event_Gadget, @Handler2())
because this is just for a specific gadget.
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Question about using Events.

Post by Peter »

Code: Select all

BindEvent(#PB_Event_Gadget, @Handler2())

;)

// Edit: munfraid has won the race :)
User avatar
bembulak
Posts: 95
Joined: Wed Feb 26, 2014 9:53 am

Re: Question about using Events.

Post by bembulak »

As an Addition:
The help file provides a nice multi-window example using the module-feature.
Kind regards,

bembulak
jphoarau
Posts: 22
Joined: Thu Dec 28, 2023 4:30 am

Re: Question about using Events.

Post by jphoarau »

munfraid wrote: Thu Jan 04, 2024 2:58 pm In line 34 use

Code: Select all

BindEvent(#PB_Event_Gadget, @Handler2())
instead of

Code: Select all

BindGadgetEvent(#PB_Event_Gadget, @Handler2())
because this is just for a specific gadget.
Thank you very much. It works fine now.
jphoarau
Posts: 22
Joined: Thu Dec 28, 2023 4:30 am

Re: Question about using Events.

Post by jphoarau »

bembulak wrote: Thu Jan 04, 2024 3:05 pm The help file provides a nice multi-window example using the module-feature.
Thank you for the info. I'm taking a look at that. It seems very interesting. Thanks.
Post Reply