Button events are mislinking...

Just starting out? Need help? Post your questions and find answers here.
User avatar
kiwidave
User
User
Posts: 13
Joined: Sun Mar 09, 2014 9:27 am
Location: New Zealand

Button events are mislinking...

Post by kiwidave »

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.
Valui ad satanam in computatrum meum invocandum
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Button events are mislinking...

Post by Bisonte »

Without the source, that shows the problem ..... difficult to say ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Button events are mislinking...

Post by TI-994A »

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....
Hi kiwidave, and welcome to the wonderful world of PureBasic!

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
Instead of exit buttons, each data form can be closed with the close button on the window itself. It works pretty well without any discernible conflict.

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 :D
User avatar
kiwidave
User
User
Posts: 13
Joined: Sun Mar 09, 2014 9:27 am
Location: New Zealand

Re: Button events are mislinking...

Post by kiwidave »

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:

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()
The initialisation from the form designer:

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")
The event code on the exit button:

Code: Select all

        Case btnExit1_3
          CloseWindow(winEnv_Clients)
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.
Valui ad satanam in computatrum meum invocandum
User avatar
kiwidave
User
User
Posts: 13
Joined: Sun Mar 09, 2014 9:27 am
Location: New Zealand

Re: Button events are mislinking...

Post by kiwidave »

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
User avatar
kiwidave
User
User
Posts: 13
Joined: Sun Mar 09, 2014 9:27 am
Location: New Zealand

Re: Button events are mislinking...

Post by kiwidave »

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
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Button events are mislinking...

Post by TI-994A »

For starters, it would be better to trap the winMain events in the Select/Case loop itself to avoid redundancy, like so:

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
Beyond that, a look at one of the window's events procedures might help. :)
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 :D
User avatar
kiwidave
User
User
Posts: 13
Joined: Sun Mar 09, 2014 9:27 am
Location: New Zealand

Re: Button events are mislinking...

Post by kiwidave »

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.
Valui ad satanam in computatrum meum invocandum
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Button events are mislinking...

Post by TI-994A »

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...
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.

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 :D
Post Reply