I have the codes in the WebPage1.WebButton1.Action Event
Dim DisplayMsg As New Notification // Notification is the WebDialog Class
DisplayMsg = New Notification
AddHandler DisplayMsg.NotificationDismissedAfter, AddressOf WebPage1.NotificationDismissedAfter
DisplayMsg.Show
DisplayMsg.lTextMsg.Text = "Hello Xojo"
I would like to move these codes to a module. How do I pass the reference of WebPage1 to the module? Or how should I reference this statement to my module?
sub displayMsg(Page as WebPage)
Dim DisplayMsg As New Notification // Notification is the WebDialog Class
DisplayMsg = New Notification
AddHandler DisplayMsg.NotificationDismissedAfter, AddressOf Page.NotificationDismissedAfter
DisplayMsg.Show
DisplayMsg.lTextMsg.Text = "Hello Xojo"
end sub
From WebPage1 :
displayMsgPage(self)
That said, you should be careful with addhandler in a method. If you don’t use RemoveHandler later, you have leaks. Especially if you run this method several times.
I would probably start a timer in the method after a few seconds to remove the handler each time the method is called. Or right in Page.NotificationDismissedAfter
sub displayMsg(ShowText As String, Optional Page as WebPage)
Dim DisplayMsg As New Notification // Notification is the WebDialog Class
DisplayMsg = New Notification
AddHandler DisplayMsg.NotificationDismissedAfter, AddressOf Page.NotificationDismissedAfter
DisplayMsg.Show
DisplayMsg.lTextMsg.Text = ShowText
end sub
That’s because not all WebPages have a NotificationDismissedAfter method. To accomplish this, you’d probably need to either create an interface or a delegate.
Do you meant that by using Delegate, I can still use the AddHandler Statement or I am supposed to use other statements in combination of Delegate?
I had read thru the Delegate documentation and also had viewed the Delegate Project Example but I still cannot find any directions to make my Module working.
I would appreciate if you could point me to the right direction by providing some examples. Thank you again.
After some trial and errors, this seems to be working but I think it defeats the purpose of Delegate.
// AddHandler DisplayMsg.NotificationDismissedAfter, AddressOf Page.NotificationDismissedAfter
Dim CallMethod As MethodCaller // MethodCaller is the Delegate
Select Case Page
Case "RegisterPage"
CallMethod = AddressOf RegisterPage.NotificationDismissedAfter
End Select
AddHandler DisplayMsg.NotificationDismissedAfter, CallMethod
Using a delegate would allow you to pass the callback method right in your method call. It’s similar to AddHandler except that it doesn’t carry the extra reference issue that people have run into.
Essentially, you create a delegate method signature and then specify a parameter in your method of that delegate type;
Sub MyMethod(p as string, callback as MyDelegate)
Then in the method you can call the method by using: