Same Gadget ID's in different windows

Just starting out? Need help? Post your questions and find answers here.
4otomax
New User
New User
Posts: 7
Joined: Tue Sep 21, 2021 2:00 am

Same Gadget ID's in different windows

Post by 4otomax »

Hello, can anyone help me please?
There is 6 different windows in my app created with Form Designer. Gadgets are created using #PB_Any, and their ID's are stored in variables.
Each time I open any of secondary window, PB assigns new value to gadget's ID, but this ID is the same for other gadget in other window (opened then closed previously). For example if I click on TextGadget, it calls function binded for StringGadget in other window.

Image

Sometimes I get up to six gadgets and windows with same ID's. What I am doing wrong? :cry:
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Same Gadget ID's in different windows

Post by Demivec »

Unbind the gadgets when you close a window.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Same Gadget ID's in different windows

Post by STARGÅTE »

It is normal that memory (IDs) is recycled, when objects (in this case gadgets) are released and create new ones.
It happens also in other libraries like images:

Code: Select all

Define ID = CreateImage(#PB_Any, 32, 32)
Debug ID
FreeImage(ID)

Define ID = CreateImage(#PB_Any, 32, 32)
Debug ID
FreeImage(ID)
37162624
37162624
On the other hand, a bound gadget event should be released when the gadget is released. So "it calls function binded for StringGadget in other window" is not possible:

Code: Select all

Procedure Test1()
	Debug "Test1"
EndProcedure

Procedure Test2()
	Debug "Test2"
EndProcedure

OpenWindow(1, 0, 0, 200, 200, "", #PB_Window_WindowCentered)
Define ID = StringGadget(#PB_Any, 10, 10, 100, 20, "")
Debug ID
BindGadgetEvent(ID, @Test1())
CloseWindow(1) ; Also unbind the gadget event

OpenWindow(1, 0, 0, 200, 200, "", #PB_Window_WindowCentered)
Define ID = StringGadget(#PB_Any, 10, 10, 100, 20, "")
Debug ID ; same ID
BindGadgetEvent(ID, @Test2()) ; New bound.


Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			End
	EndSelect
ForEver
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
4otomax
New User
New User
Posts: 7
Joined: Tue Sep 21, 2021 2:00 am

Re: Same Gadget ID's in different windows

Post by 4otomax »

STARGÅTE wrote: Tue Sep 21, 2021 9:07 am On the other hand, a bound gadget event should be released when the gadget is released.
I don't use bind/unbind gadget events functions and process all gadget events in main loop. Can I leave gadget events for main window gadgets in a main loop (where WaintWindowEvent() is) or it is a bad idea at all?

Also I tried not to use #PB_Any for gadget id's. Form designer created enumerations for gadgets, but it's not possible to set start value for them. So there can be same situation: different gadgets takes equal id's, right?
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Same Gadget ID's in different windows

Post by STARGÅTE »

4otomax wrote: Tue Sep 21, 2021 10:42 am
STARGÅTE wrote: Tue Sep 21, 2021 9:07 am On the other hand, a bound gadget event should be released when the gadget is released.
I don't use bind/unbind gadget events functions and process all gadget events in main loop. Can I leave gadget events for main window gadgets in a main loop (where WaintWindowEvent() is) or it is a bad idea at all?
So you use the returned gadget ID in the Select EventGadget() : Case ... section? Or what?
Yes, it is possible to split event handling into the main-loop but also some with bind-event.
4otomax wrote: Tue Sep 21, 2021 10:42 am Also I tried not to use #PB_Any for gadget id's. Form designer created enumerations for gadgets, but it's not possible to set start value for them. So there can be same situation: different gadgets takes equal id's, right?
I don't have experience with the form designer, I wrote all my applications just by the code editor and using my own enumerations.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Same Gadget ID's in different windows

Post by RASHAD »

Welcome to PB :)
I do not use Form Designer so I don't know exactly the cause of the problems
But maybe if you can use UseGadgetList(WindowID) before creating each Window gadget it may help
Good luck
Egypt my love
4otomax
New User
New User
Posts: 7
Joined: Tue Sep 21, 2021 2:00 am

Re: Same Gadget ID's in different windows

Post by 4otomax »

STARGÅTE wrote: Tue Sep 21, 2021 11:02 am So you use the returned gadget ID in the Select EventGadget() : Case ... section?
Yes.

One more question. This is code in main loop:

Code: Select all

Case #PB_Event_CloseWindow
      CurrentWindow.l = EventWindow()
      Select CurrentWindow 
        Case Window_main
          ExitProgramRequester()
        Default
          CloseWindow(CurrentWindow)
      EndSelect
Is it OK to add separate Case for each window and add UnbindEvent() after each CloseWindow()?

And BindEvent() for window in this place? (window opens after menu or shortcut pressing)

Code: Select all

Case #MenuItem_Settings
          If IsWindow(Window_settings) = 0
            OpenWindow_settings()
            fillSettingsWindow()
            ; bind gadget event here
          EndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Same Gadget ID's in different windows

Post by mk-soft »

I always use constants and not variables in the FormDesigner.
If you use variables and close the window, you have to set the variables of the gadgets and windows to NULL, because the dynamic IDs (object address) are used again.
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
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Same Gadget ID's in different windows

Post by spikey »

4otomax wrote: Tue Sep 21, 2021 10:42 am Also I tried not to use #PB_Any for gadget id's. Form designer created enumerations for gadgets, but it's not possible to set start value for them. So there can be same situation: different gadgets takes equal id's, right?
The Form Designer uses a named enumeration for window numbers and gadget numbers. They should get continuous numbering automatically when the seperate form files are included in a "main" file - something like this:-

Code: Select all

; ** FIRST FORM FILE
;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Enumeration FormWindow
  #Window_0
EndEnumeration

Enumeration FormGadget
  #Button_0
  #Calendar_0
  #Checkbox_0
EndEnumeration

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  OpenWindow(#Window_0, x, y, width, height, "", #PB_Window_SystemMenu)
  ButtonGadget(#Button_0, 10, 10, 100, 25, "")
  CalendarGadget(#Calendar_0, 10, 55, 265, 100, 0)
  CheckBoxGadget(#Checkbox_0, 10, 170, 100, 25, "")
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

; ** SECOND FORM FILE
;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Enumeration FormWindow
  #Window_1
EndEnumeration

Enumeration FormGadget
  #Combo_1
  #Date_1
  #Editor_1
EndEnumeration


Procedure OpenWindow_1(x = 0, y = 0, width = 600, height = 400)
  OpenWindow(#Window_1, x, y, width, height, "", #PB_Window_SystemMenu)
  ComboBoxGadget(#Combo_1, 10, 10, 100, 25)
  DateGadget(#Date_1, 10, 40, 100, 25, "")
  EditorGadget(#Editor_1, 10, 75, 100, 25)
EndProcedure

Procedure Window_1_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

; ** "INCLUDING FILE"
; Ok I've squashed everything into one but hopefully you get the idea.
OpenWindow_0()
OpenWindow_1()

Debug "Windows"
Debug #Window_0
Debug #Window_1

Debug "Gadgets"
Debug #Button_0
Debug #Calendar_0
Debug #Checkbox_0
Debug #Combo_1
Debug #Date_1
Debug #Editor_1

Repeat 
  
  Event = WaitWindowEvent(50)
  
  If Event = #PB_Event_CloseWindow
    Quit = #True
  EndIf
  
  If EventWindow() = #Window_0
    Window_0_Events(Event)
    
  ElseIf EventWindow() = #Window_1
    Window_1_Events(Event)
    
  EndIf
  
Until Quit = #True
(Just for the record I'm NOT handling Window closures properly in this example.)
4otomax
New User
New User
Posts: 7
Joined: Tue Sep 21, 2021 2:00 am

Re: Same Gadget ID's in different windows

Post by 4otomax »

Thanks to all for answers!
There is about 200 gadgets and 8000 code lines in my app, so I need to think what will be the best.
This is my hobby project (I'm not a programmer) and I have been writing this program for 2 years. It's funny, that I catched this bug only now. I have never opened all windows and so have tested only small parts of code.
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

Re: Same Gadget ID's in different windows

Post by Axolotl »

Here is another suggestion.
I like to use the enums and structure them as in the example below. Constants, used in this way, are INHO very flexible as modal or non-modal windows (just my opinion). I have never checked if this uses more memory. The simple logic was always more important to me.
About the form desiger: I use it only for complicated arrangements for a rough positioning. I always copy the results into the source code and there it goes....
Certainly not the smartest solution, but the Form Designer is unfortunately a bit simple.

Code: Select all

;' example of enumeration of windows and gadgets only 
;---== Enumerations ==---------------------------------------------------------
Enumeration EWindow 
  #WINDOW_Main   ;' Window Main is the primary window 
  #WINDOW_Edit    ;' dialog window (Opened and Closed) 
EndEnumeration

Enumeration EGadget 
;:#WINDOW_Main  ;' primary window 
  #GADGET_Main_lbFileList   ;' show all files to backup .. Source | Destination 

  #GADGET_Main_strSource          ;' edit 
  #GADGET_Main_strTarget          ;' 

  #GADGET_Main_btnCopy 
  
  #GADGET_Main_CurrProgressValue  
  #GADGET_Main_CurrProgressBar 

  #GADGET_Main_ProgressTime  
  #GADGET_Main_ProgressTimeResult  

;:#WINDOW_Edit  ;'  modal Editor window 
  #GADGET_Edit_strFile 
  #GADGET_Edit_strSource 
  #GADGET_Edit_strTarget 
  #GADGET_Edit_btnOk 
  #GADGET_Edit_btnCancel 
EndEnumeration
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Same Gadget ID's in different windows

Post by mk-soft »

To manage multiple windows I use the (my) EventDesigner. This creates a common GUI file. A subsequent adaptation or modification of the original form files (*.pbf) is also possible. To avoid conflicts with the constants, the window constant is used as a prefix for all gadgets and menus.

See signature ...
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
4otomax
New User
New User
Posts: 7
Joined: Tue Sep 21, 2021 2:00 am

Re: Same Gadget ID's in different windows

Post by 4otomax »

Axolotl wrote: Wed Sep 22, 2021 12:31 pm Here is another suggestion.
I like to use the enums and structure them as in the example below.
I use very similar windows' and gadget's names. My mistake was to use default IDE settings. Next time I will disable the use of #PB_Any in combination with variables for gadget id's. Now I have no strength to search for all variables in 22 files. This could be another point of failure.
This is my temporary solution as mk-soft suggested:

Code: Select all

Case #PB_Event_CloseWindow
      Select EventWindow()
        Case Window_main
          ExitProgramRequester()
        Case Window_HDDInfo
          CloseWindow(Window_HDDInfo)
          Window_HDDInfo = #Null
          Text_HDDInfo_HDDName = #Null
          String_HDDInfo_HDDName = #Null
          ; and so on...
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Same Gadget ID's in different windows

Post by collectordave »

When starting a new programme the first thing I do is number each window as I create it.

So the main form starts as 0

The next form Starts at 100

And so on.

#MainForm = 0
#NextForm = 100

Then create the gadget ids with enumeration

Enumeration #MainForm

#mfGadget1
#mfGadget2
#mfGadget3

Endenumeration

Enumeration #NextForm

#nfGadget1
#nfGadget2
#nfGadget3

Endenumeration
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply