Same Gadget ID's in different windows
Same Gadget ID's in different windows
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.
Sometimes I get up to six gadgets and windows with same ID's. What I am doing wrong?
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.
Sometimes I get up to six gadgets and windows with same ID's. What I am doing wrong?
Re: Same Gadget ID's in different windows
Unbind the gadgets when you close a window.
Re: Same Gadget ID's in different windows
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:
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)
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:37162624
37162624
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 more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: Same Gadget ID's in different windows
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?
Re: Same Gadget ID's in different windows
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.
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 more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: Same Gadget ID's in different windows
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
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
Re: Same Gadget ID's in different windows
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
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
Re: Same Gadget ID's in different windows
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.
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Same Gadget ID's in different windows
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
Re: Same Gadget ID's in different windows
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.
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.
Re: Same Gadget ID's in different windows
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.
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
Re: Same Gadget ID's in different windows
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 ...
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Same Gadget ID's in different windows
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...
-
- Addict
- Posts: 1309
- Joined: Fri Aug 28, 2015 6:10 pm
- Location: Portugal
Re: Same Gadget ID's in different windows
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
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.