Button events are mislinking...
Button events are mislinking...
Hi all, hoping someone here might have an idea what is going on. I have an application that consists of a number of windows. There is one 'main' window that has 8 image buttons on it, which when pressed open up another window for data entry. Each of these data entry windows has an Exit button on it. When I run the application, select a data entry window, then close it via the Exit button, all is well. However if I then go open up a different data entry window, often the Exit button, or sometimes others, will then 'link' to a different event, for example another button on the form that opens a dialog box or loads an image. If I shut down the application and start it again, the data entry window will behave correctly again the first time it is used. I thought it might be something to do with the name getting defined more than once, but that is not the case, and when I debugged and looked at the unique value for the gadgets - both the button and the gadget for the event that got actually called - they were very different, and all of them are defined using #PB_Any.
So, I'm very perplexed, and this is making my program unusable. Does anybody have any suggestions as to where I could look to resolve this. Thanks very much.
So, I'm very perplexed, and this is making my program unusable. Does anybody have any suggestions as to where I could look to resolve this. Thanks very much.
Valui ad satanam in computatrum meum invocandum
Re: Button events are mislinking...
Without the source, that shows the problem ..... difficult to say 

Re: Button events are mislinking...
Hi kiwidave, and welcome to the wonderful world of PureBasic!kiwidave wrote:...I have an application that consists of a number of windows. There is one 'main' window that has 8 image buttons on it, which when pressed open up another window for data entry. Each of these data entry windows has an Exit button on it....
If I understand correctly, this sample should be illustrative of your code:
Code: Select all
Enumeration
#MainWindow
#DataWindow1
#DataWindow2
#DataWindow3
#DataWindow4
#DataWindow5
#DataWindow6
#DataWindow7
#DataWindow8
#ImageButton1
#ImageButton2
#ImageButton3
#ImageButton4
#ImageButton5
#ImageButton6
#ImageButton7
#ImageButton8
EndEnumeration
Procedure makeImage(iColor)
iNo = CreateImage(#PB_Any, 100, 20)
StartDrawing(ImageOutput(iNo))
Box(0, 0, OutputWidth(), OutputHeight(), iColor)
StopDrawing()
ProcedureReturn ImageID(iNo)
EndProcedure
Procedure makeWindow(wNo)
If IsWindow(wNo)
SetActiveWindow(wNo)
Else
wPos = 180 + (30 * wNo)
OpenWindow(wNo, wPos, wPos, 200, 200, "Data Window " + Str(wNo))
EndIf
EndProcedure
OpenWindow(#MainWindow, 50, 150, 150, 260, "Main Window", #PB_Window_SystemMenu)
ButtonImageGadget(#ImageButton1, 10, 10, 100, 20, makeImage(RGB(255, 0, 0)))
ButtonImageGadget(#ImageButton2, 10, 40, 100, 20, makeImage(RGB(0, 255, 0)))
ButtonImageGadget(#ImageButton3, 10, 70, 100, 20, makeImage(RGB(0, 0, 255)))
ButtonImageGadget(#ImageButton4, 10, 100, 100, 20, makeImage(RGB(255, 255, 0)))
ButtonImageGadget(#ImageButton5, 10, 130, 100, 20, makeImage(RGB(255, 0, 255)))
ButtonImageGadget(#ImageButton6, 10, 160, 100, 20, makeImage(RGB(255, 255, 255)))
ButtonImageGadget(#ImageButton7, 10, 190, 100, 20, makeImage(RGB(100, 0, 0)))
ButtonImageGadget(#ImageButton8, 10, 220, 100, 20, makeImage(RGB(0, 100, 0)))
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
If EventWindow() = #MainWindow
appQuit = 1
Else
CloseWindow(EventWindow())
EndIf
Case #PB_Event_Gadget
makeWindow(EventGadget() - 8)
EndSelect
Until appQuit = 1
Hope it helps.

Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Button events are mislinking...
Thank you for that code, TI-994A. I'm using the form designer to put together the windows, so the code used is thus...
From the button to open the window:
The initialisation from the form designer:
The event code on the exit button:
I don't think there is anything out of the ordinary here, as I mentioned, if I run this as it stands there is no problem, it all works fine, but if I then open up another window and come back to this one, when I click on the Exit button it executes a completely different event. It feels like there is a mixup with pointers somewhere, and I'm wondering if perhaps I should be doing something like cleaning up all of the form objects as part of the exit routine.
From the button to open the window:
Code: Select all
OpenwinEnv_Clients()
SetGadgetText(txtTitle_Organisation1_3,fnAESDecode(fnAESEncode(stcOrganisation\strName)))
SetGadgetText(txtTitle_PlanYear1_3,fnAESDecode(fnAESEncode(Str(stcPlanInstance\intYear))) + " Planning Year")
winEnvClients_ReloadData()
Code: Select all
Procedure OpenwinEnv_Clients(x = 0, y = 0, width = 710, height = 545)
winEnv_Clients = OpenWindow(#PB_Any, x, y, width, height, "BUSINESS PLANNER", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_WindowCentered, WindowID(winEnv))
SetWindowColor(winEnv_Clients, RGB(255,255,255))
btnExit1_3 = ButtonGadget(#PB_Any, 590, 510, 100, 25, "Exit")
Code: Select all
Case btnExit1_3
CloseWindow(winEnv_Clients)
Valui ad satanam in computatrum meum invocandum
Re: Button events are mislinking...
I've just done some more testing. It's not restricted to this button, it seems definitely to be a problem with handling of events. If I go in and out of one form, add some data, save etc...no problem. If I then simply open up another form, close it then go back to the original, all event handling seems to be mixed up. This is just weird. Obviously I've done something screwy or missed something, but the question is...what...
Valui ad satanam in computatrum meum invocandum
Re: Button events are mislinking...
This is my event-handling code in the main startup routine:
Code: Select all
Repeat
intEvent = WaitWindowEvent()
Select EventWindow()
Case winMain
winMain_Events(intEvent)
Case winEnv
winEnv_Events(intEvent)
Case winEnv_Location
winEnv_Location_Events(intEvent)
Case winEnv_Products
winEnv_Products_Events(intEvent)
Case winEnv_Clients
winEnv_Clients_Events(intEvent)
Case winEnv_Market
winEnv_Market_Events(intEvent)
Case winEnv_Stakeholders
winEnv_Stakeholders_Events(intEvent)
Case winPlan
winPlan_Events(intEvent)
Case winFin
winFin_Events(intEvent)
EndSelect
Until EventWindow() = winMain And intEvent = #PB_Event_CloseWindow ; Exit program completely only when the main menu window is closed
Valui ad satanam in computatrum meum invocandum
Re: Button events are mislinking...
For starters, it would be better to trap the winMain events in the Select/Case loop itself to avoid redundancy, like so:Beyond that, a look at one of the window's events procedures might help. 
Code: Select all
Repeat
intEvent = WaitWindowEvent()
Select EventWindow()
Case winMain
If intEvent = #PB_Event_CloseWindow
End
EndIf
winMain_Events(intEvent)
Case winEnv
winEnv_Events(intEvent)
Case winEnv_Location
winEnv_Location_Events(intEvent)
Case winEnv_Products
winEnv_Products_Events(intEvent)
Case winEnv_Clients
winEnv_Clients_Events(intEvent)
Case winEnv_Market
winEnv_Market_Events(intEvent)
Case winEnv_Stakeholders
winEnv_Stakeholders_Events(intEvent)
Case winPlan
winPlan_Events(intEvent)
Case winFin
winFin_Events(intEvent)
EndSelect
ForEver

Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Button events are mislinking...
Thanks again for the suggestion, I'll do that.
I did some investigating (actually I thought I posted that but can't have submitted it, silly me), and when I put a "Debug EventWindow()" statement above the Select statement, it turns out that the window number being returned doesn't match the window in focus. For example, if I open Window1, this shows 1 as the event window (just using "1" as an example). If I close Window1, the calling form's event window number shows fine. But then if I open Window2, instead of "2" showing as the event window it shows "1" - the previous window's number.
Any ideas out there why this may be? Thanks again.
I did some investigating (actually I thought I posted that but can't have submitted it, silly me), and when I put a "Debug EventWindow()" statement above the Select statement, it turns out that the window number being returned doesn't match the window in focus. For example, if I open Window1, this shows 1 as the event window (just using "1" as an example). If I close Window1, the calling form's event window number shows fine. But then if I open Window2, instead of "2" showing as the event window it shows "1" - the previous window's number.
Any ideas out there why this may be? Thanks again.
Valui ad satanam in computatrum meum invocandum
Re: Button events are mislinking...
Hello again kiwidave. You're right, it does sound strange. However, unless there's an expanded view of how the windows and events are structured, it'd be pretty difficult to determine the points of error.kiwidave wrote:...when I put a "Debug EventWindow()" statement above the Select statement, it turns out that the window number being returned doesn't match the window in focus. For example, if I open Window1, this shows 1 as the event window (just using "1" as an example). If I close Window1, the calling form's event window number shows fine. But then if I open Window2, instead of "2" showing as the event window it shows "1" - the previous window's number...
If you find it too long to post, perhaps you could link it somewhere, and we'd be glad to take a look.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 
