Different behavior in desktop vs. device on AddEvent

willisgt

Active Member
Licensed User
Not sure if this actually constitutes a bug, but it's an odd (and somewhat annoying) behavior I've observed recently.

I've written a program that creates certain buttons dynamically (e.g., at runtime). I also add a click event to those buttons using AddEvent.

When I run this program on the desktop ( F5>Run), open the form, close the form, and open it again, the buttons now have *two* events assigned to them, and both events get raised, even if it's the same event.

So...

B4X:
   AddButton ("Form1", "Button1", 20,20,60,60,"Click Me")
   AddEvent ("Button1", Click, "MySub")

   Sub MySub
            msgbox( Sender.Text )
   End Sub

would display the message box twice. I had to write a little routine to keep track of what events had been assigned to what controls, to avoid adding the same event to a control twice.

Okay, part II - if I compile the same program for either the desktop or the device, run it, open the same form, click on the same controls (buttons), the duplicate events don't get raised, no matter how many times I assign the event to the control. In this case, I am *not* checking beforehand to see if I've already added the event to the control... I just add it every time the control gets created. It works perfectly.

So why then, is the behavior different when run from inside B4PPC?

Is this a bug, or am I missing somwthing?


Gary
 

willisgt

Active Member
Licensed User
Erel, you're right, the code you posted does not exhibit the behavior I described.

The program I'm attaching, however, does. Run the executable; click Button1, and Form2 opens. Click the 'Click Me' button, and a mesagew box appears. Close Form2. Click Button1 again, and Form2 opens once again. Click the 'Click Me' button, and the message box appears, but only once.

Now run Basic4PPC and load the .sbp file. Run it (F5). Form1 opens. Click Button1. Form2 opens. Click the 'Click Me' button, and a message box appears. Close Form2. Click Button1 again, and Form2 reopens. Click the 'Click Me' button, and the message box appears twice in rapid succession.

At least, that's the behavior on my desktop.

BTW, I am using Basic4PPC 6.0 on a computer running Windows XP Pro.


Gary
 

agraham

Expert
Licensed User
Longtime User
At least, that's the behavior on my desktop
And on mine. It looks like Control("Button2").Dispose on the desktop isn't truly disposing it. It looks as though it still has an entry in an event table somewhere in B4PPC even though it is cleared from a control list and so doesn't error when added again. So when it is added back the second AddEvent adds a second event to the event table. Interestingly one would then think that closing Form2 and reopening would give three Message boxes - but sadly it doesn't
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…