Y yonson Active Member Licensed User Longtime User Dec 30, 2014 #1 First off a massive thank you for Erel in developing B4i which is in every way an improvement on the previous tool we were using. I've just been going through the tutorials and putting a few projects together and have a quick query regarding event handling in a class. I have created a class which I'm using to hold a configurable panel (to hold a mix of views based on the input parameters). I've stripped back all the code to something very simple. Here is the relevant code:- Code: Copy 'Class module Sub Class_Globals Dim pnl As Panel End Sub 'Initializes the object. You can add parameters to this method if needed. Public Sub Initialize(text As String, x As Int, y As Int, size As Int) pnl.Initialize("PanelButton") End Sub Sub render As Panel Return pnl End Sub Sub PanelButton_Click Log("PanelButton was Clicked") End Sub My problem is that whilst the views etc are rendered fine, when I click on the panel, I don't get the log message, but instead get this code:- "Event target instance was released: btn_click" However, if I remove the Sub PanelButton_Click completely, then it just closes with no log entry at all. Sure it will be a simple explanation but I'm looking to understand why this isn't working as expected. Do I need to move the call into another module? Many thanks! Last edited: Dec 30, 2014
First off a massive thank you for Erel in developing B4i which is in every way an improvement on the previous tool we were using. I've just been going through the tutorials and putting a few projects together and have a quick query regarding event handling in a class. I have created a class which I'm using to hold a configurable panel (to hold a mix of views based on the input parameters). I've stripped back all the code to something very simple. Here is the relevant code:- Code: Copy 'Class module Sub Class_Globals Dim pnl As Panel End Sub 'Initializes the object. You can add parameters to this method if needed. Public Sub Initialize(text As String, x As Int, y As Int, size As Int) pnl.Initialize("PanelButton") End Sub Sub render As Panel Return pnl End Sub Sub PanelButton_Click Log("PanelButton was Clicked") End Sub My problem is that whilst the views etc are rendered fine, when I click on the panel, I don't get the log message, but instead get this code:- "Event target instance was released: btn_click" However, if I remove the Sub PanelButton_Click completely, then it just closes with no log entry at all. Sure it will be a simple explanation but I'm looking to understand why this isn't working as expected. Do I need to move the call into another module? Many thanks!
Erel B4X founder Staff member Licensed User Longtime User Dec 30, 2014 #2 Please use [code]code here...[/code] tags when posting code. The class instance itself is being released so it cannot handle events. The problem is in the code that uses this class. Upvote 0
Please use [code]code here...[/code] tags when posting code. The class instance itself is being released so it cannot handle events. The problem is in the code that uses this class.
Y yonson Active Member Licensed User Longtime User Dec 30, 2014 #3 thanks Erel - what is the correct approach - i.e. where should the event handler be placed in this example? here is a sample code that calls the class:- Code: Copy ' sample class name Dim p As PanelButton p.Initialize("hello there",0,0,25%x) Page1.RootPanel.AddView(p.render,0,0,50%x,50%x) Last edited: Dec 30, 2014 Upvote 0
thanks Erel - what is the correct approach - i.e. where should the event handler be placed in this example? here is a sample code that calls the class:- Code: Copy ' sample class name Dim p As PanelButton p.Initialize("hello there",0,0,25%x) Page1.RootPanel.AddView(p.render,0,0,50%x,50%x)
Erel B4X founder Staff member Licensed User Longtime User Dec 31, 2014 #4 You have several options. You can make p a global variable (change its name in that case) or you can add a reference from your view to p by settings the view tag property: Code: Copy p.SomeView.Tag = p You need to create a reference to p or it will be released. Upvote 0
You have several options. You can make p a global variable (change its name in that case) or you can add a reference from your view to p by settings the view tag property: Code: Copy p.SomeView.Tag = p You need to create a reference to p or it will be released.
Y yonson Active Member Licensed User Longtime User Jan 3, 2015 #5 thats great thanks Erel - the 2nd option works fine, my understanding is that so long as a reference is kept in the tag then it persists long enough for the click action to work. Thanks for clearing that up Upvote 0
thats great thanks Erel - the 2nd option works fine, my understanding is that so long as a reference is kept in the tag then it persists long enough for the click action to work. Thanks for clearing that up
Erel B4X founder Staff member Licensed User Longtime User Jan 4, 2015 #6 yonson said: my understanding is that so long as a reference is kept in the tag then it persists long enough for the click action to work Click to expand... It is more accurate to say that as long as there is a reference to the view (and there is one because you added the view to another view) then the reference to the tag will be kept. Upvote 0
yonson said: my understanding is that so long as a reference is kept in the tag then it persists long enough for the click action to work Click to expand... It is more accurate to say that as long as there is a reference to the view (and there is one because you added the view to another view) then the reference to the tag will be kept.
Y yonson Active Member Licensed User Longtime User Jan 6, 2015 #7 thats great and makes sense, thanks for your help Upvote 0