BindGadgetEvent() With Parameter to Procedure

Just starting out? Need help? Post your questions and find answers here.
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

BindGadgetEvent() With Parameter to Procedure

Post by C87 »

Hello PB_All,
In BindGadgetEvnt() I appear to be unable to pass a Parameter to the Procedure.
I would like to do BindEventGadget(#PB_Any, @ProcedureName(VarName))
But doing that falls over. I show a code snippit of how I would like to do and follow it with the code that works.

Am I right in my conclusion that I cannot pass a variable in a BindGadgetEvent(), or have I made a mistake in my code?

Code: Select all

;WHAT I WOULD LIKE TO DO

DeclareModule ANewModule
  Declare DoWindowProc()
  ....
  .....
  Declare.s ProcSettingOnOff(AESC.s)
EndDeclareModule ; ANewModule
;
Module ANewModule
  Enumeration
    #Btn1
    #Btn2
    #Btn3
    ....
    ....
  EndEnumeration
  ;
  Global.s AESC
  .....
  ....
  Procedure DoWindowProc()
    ...
    If OpenWindow(.....)
      ....
      ....
      BindGadgetEvent(#Btn1, @ProcSetting("A"))
      BindGadgetEvent(#Btn2, @ProcSetting("E"))
      ....
      ;
    EndIf ; OpenWidow()
    ;
    Procedure.s ProcSettingOnOff(AESC.s)
      If AESC = "A" Or AESC = "E"
        ....
      Else
        ....
     EndIf
     ; 
    EndProcedure  ; ProcSetingOnOff()
    ;
  EndProcedure  ; DoWindowProc()
  ...
  ;
EndModule  ; ANewModule
________________________________________________
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
________________________________________________
WHAT I HAVE TO DO TO GET IT TO WORK

DeclareModule ANewModule
  Declare DoScreenProc()
  ....
  Declare DoBtn1Action()
  Declare DoBtn2Action()
  Declare DoBtn3Action()
  .....
  Declare.s ProcSettingOnOff(AESC.s)
EndDeclareModule ; ANewModule
;
Module ANewModule	
  Enumeration
    #Btn1
    #Btn2
    #Btn3
    ....
    ....
  EndEnumeration
  ;
  Global.s AESC
  .....
  ;
  Procedure DoScreenProc()
    ...
    If OpenWindow(.....)
      ....
      ....
      BindGadgetEvent(#Btn1, @DoBtn1Action())
      BindGadgetEvent(#Btn2, @DoBtn2Action())
      ....
    EndIf ; OpenWidow
    ;
    Procedure DoBtn1Action()
      AESC = "A"
      ProcSettingOnOff(AESC)
    EndProcedure
    ;
    Procedure DoBtn2Action()
      AESC = "E"
      ProcSettingOnOff(AESC)
    EndProcedure
    ;
    Procedure.s ProcSettingOnOff(AESC.s)
      If AESC = "A" Or AESC = "E"
        ....
      Else
        ....
     EndIf
     ; 
    EndProcedure ; ProcSettingOnOff()
    ;
  EndProcedure  ; DoScreenProc()
  ...
  ;
EndModule  ; ANewModule
If it's falling over......just remember the computer is never wrong!
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: BindGadgetEvent() With Parameter to Procedure

Post by Cyllceaux »

A dirty way is doing this

Code: Select all


Global NewMap gadgetresult.s()
gadgetresult(Str(btn1))="A"
gadgetresult(Str(btn2))="B"

Procedure ANewEvent()
	Protected result.s=gadgetresult(Str(EventGadget()))
	
EndProcedure

or

Code: Select all

SetGadgetData(btn1,@"A")
SetGadgetData(btn1,@"B")

Procedure ANewEvent()
	Protected result.s=PeekS(GetGadgetData(EventGadget()))
	
EndProcedure
or

Code: Select all

Procedure ANewEvent(result.s)
	
EndProcedure


Macro MM
	"
EndMacro

Macro MNE(w)
	Procedure ANewEvent#w()
		ANewEvent(MM#w#MM)
	EndProcedure
EndMacro

MNE(A)
MNE(B)
BindGadgetEvent(btn1,ANewEventA())
BindGadgetEvent(btn2,ANewEventB())
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: BindGadgetEvent() With Parameter to Procedure

Post by jacdelad »

It's not possible to pass a parameter to the function. Can you describe in a short what you need that parameter for? Maybe there's a better way.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: BindGadgetEvent() With Parameter to Procedure

Post by C87 »

@jacdelad
It is only a standard and very simple process that is in all my software. ( some of which I'm converting some to PureBasic )
I have around ten buttons on the screen to allow the user to move around the data, and open other screens, plus an Exit/Close Screen button.
If a user clicks Edit, or Add, I will switch all Fields/StringGadgets() from Locked to Unlocked for amending. I will also switch all buttons that allow a user to move from the current record, or Window to be Disabled, including Exit. The solution of using a Param with the BindGadgetEvent() would have been a neat solution and avoid having to have four, then unnecessary Procedures:.ie. Add, Edit, Save and Cancel. It is no trouble to create the four of them, which only have two lines of code in each, it just appeared to be pointless. Each of those Procs in turn then call the EdEditSaveCancel() Procedure which decides to switch Enabled On or Off for Fields and Buttons. As I said in the post it works fine like that....but

Using BindGadgetEvent() with the Procedure and Param would have been a tidy solution though. Plus I couldn't see any reason for it not to work. No worries though, thanks anyway.
If it's falling over......just remember the computer is never wrong!
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: BindGadgetEvent() With Parameter to Procedure

Post by mk-soft »

PB calls the procedures that are bound with Bind[Gadget]Event before passing them to WaitWindowEvent.

Thus, the following functions are available in the procedure to query the event

- Event()
- EventWindow()
- EventGadget()
- EventType()
- EventData()

If you want to trigger the procedure manually via an event, you can do this with PostEvent. You then pass additional data with the optional parameter "Data".

Code: Select all

;-TOP

EnableExplicit

;- Constant
Enumeration Windows
  #Main
EndEnumeration

Enumeration Menus
  #Menu
EndEnumeration

Enumeration MenuItems
  #MenuExitApplication
EndEnumeration
  
Enumeration Gadgets
  #List
EndEnumeration

Enumeration Statusbar
  #Status
EndEnumeration

Enumeration Images
  
EndEnumeration

;- Global Variable
Global ExitApplication

;- Functions
Procedure UpdateWindow()
  
  Protected x, y, dx, dy, menu, status
  
  menu = MenuHeight()
  If IsStatusBar(#Status)
    status = StatusBarHeight(#Status)
  Else
    status = 0
  EndIf
  x = 0
  y = 0
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - menu - status
  ResizeGadget(#List, x, y, dx, dy)
  
EndProcedure

; ----

Procedure AllocateString(String.s)
  Protected *mem.string
  
  *mem = AllocateStructure(String)
  If *mem
    *mem\s = String
  EndIf
  ProcedureReturn *mem
EndProcedure

Procedure.s FreeString(*mem.string)
  Protected r1.s
  If *mem
    r1 = *mem\s
    FreeStructure(*mem)
  EndIf
  ProcedureReturn r1
EndProcedure

; ----

Procedure DoEventList()
  Select EventType()
    Case #PB_EventType_FirstCustomValue
      AddGadgetItem(EventGadget(), -1, FreeString(EventData()))
      
  EndSelect
EndProcedure

;- Main
Procedure Main()
  
  Protected event, dx, dy
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget
  dx = 800
  dy = 600
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, dx, dy, "Main Window", #WinStyle)
    
    ; Menu
    CreateMenu(#Menu, WindowID(#Main))
    MenuTitle("File")
    MenuItem(#MenuExitApplication, "E&xit")
    ; Gadgets
    ListViewGadget(#List, 0, 0, dx, dy)
    
    ; Statusbar
    CreateStatusBar(#Status, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Init
    UpdateWindow()
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    BindGadgetEvent(#List, @DoEventList())
    
    ; Sim Event
    AddWindowTimer(#Main, 99, 2000)
    
    ; Main Loop
    Repeat
      event = WaitWindowEvent()
      Select event
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                ExitApplication = #True
                
            CompilerEndIf
              
            Case #MenuExitApplication
              ExitApplication = #True
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #List
              
          EndSelect
          
        Case #PB_Event_Timer
          Select EventTimer()
            Case 99
              PostEvent(#PB_Event_Gadget, #Main, #List, #PB_EventType_FirstCustomValue, AllocateString("Timer: " + Str(Date())))
              
          EndSelect
          
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              ExitApplication = #True
              
          EndSelect
          
      EndSelect
      
    Until ExitApplication
    
  EndIf
  
EndProcedure : Main()

End
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: BindGadgetEvent() With Parameter to Procedure

Post by jacdelad »

mk-soft wrote exactly what I wanted to reply. EventGadget tells you which button was clicked, so this should make it easy.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: BindGadgetEvent() With Parameter to Procedure

Post by NicTheQuick »

I usually use SetGadgetData() together with GetGadgetData(EventGadget()) as Cyllceaux already pointed out.

And if there is more data to add to a gadget, just use a structure with as many fields as you like and AllocateStructure() and FreeStructure() if you free the gadget again.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: BindGadgetEvent() With Parameter to Procedure

Post by C87 »

I've got Nº2 running from Cyllceaux which seems fine. I'll go through the others later.
Have to say not a solution I'd have come up with myself! I'd have had to stick with the four Functions.

Many thanks All
If it's falling over......just remember the computer is never wrong!
Post Reply