Answered by:
addhandler and addressof

Question
-
Hi,
if I code so, I get the error: Error 1 'AddressOf' operand must be the name of a method (without parentheses). D:\Egyeb\OTIS\OTIS\kozos\kozos.vb 36 40 OTIS
Public Sub GreenMessage(ByVal item As BarStaticItem, ByVal szoveg As String) item.Caption = szoveg item.ImageIndex = 2 item.Appearance.BackColor = ColorTranslator.FromOle(greenstatusbar) My.Computer.Audio.Play(My.Resources.sapmsg, AudioPlayMode.Background) tmr.Interval = 500 AddHandler tmr.Tick, AddressOf tmr_tick(item) tmr.Start() End Sub Private Sub tmr_tick(ByVal item As BarStaticItem) item.Appearance.BackColor = ColorTranslator.FromOle(14206908) tmr.Stop() End Sub
If I delete the argument, I get the error: Error 1 Argument not specified for parameter 'item' of 'Private Sub tmr_tick(item As DevExpress.XtraBars.BarStaticItem)'. D:\Egyeb\OTIS\OTIS\kozos\kozos.vb 36 40 OTIS
Public Sub GreenMessage(ByVal item As BarStaticItem, ByVal szoveg As String) item.Caption = szoveg item.ImageIndex = 2 item.Appearance.BackColor = ColorTranslator.FromOle(greenstatusbar) My.Computer.Audio.Play(My.Resources.sapmsg, AudioPlayMode.Background) tmr.Interval = 500 AddHandler tmr.Tick, AddressOf tmr_tick() tmr.Start() End Sub Private Sub tmr_tick(ByVal item As BarStaticItem) item.Appearance.BackColor = ColorTranslator.FromOle(14206908) tmr.Stop() End Sub
So, how would it be the right way?
Sunday, January 6, 2013 2:46 PM
Answers
-
Use the Timer's Tag property to hold the item:
Public Sub GreenMessage(ByVal item As BarStaticItem, ByVal szoveg As String)
item.Caption = szoveg
item.ImageIndex = 2
item.Appearance.BackColor = ColorTranslator.FromOle(greenstatusbar)
My.Computer.Audio.Play(My.Resources.sapmsg, AudioPlayMode.Background)
tmr.Tag = item
tmr.Interval = 500
AddHandler tmr.Tick, AddressOf tmr_tick()
tmr.Start()
End Sub
Private Sub tmr_tick(sender As Object, e As EventArgs)
Dim Item As BarStaticItem = DirectCast(tmr.Tag, BarStaticItem)
item.Appearance.BackColor = ColorTranslator.FromOle(14206908)
tmr.Stop()
End Sub
Sunday, January 6, 2013 3:46 PM -
I just want to clarify further, instead of trying to deviate from the intended signature of an event, instead try making your value public and accessing that value from there, meanwhile, you have to use a sub that has a matching signature of the event you are assigning to it.
Public Class Form1 Friend WithEvents Timer1 As New Timer Public Item As String Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddHandler Timer1.Tick, AddressOf Timer1_Tick End Sub Sub Timer1_Tick(sender As Object, e As EventArgs) End Sub End Class
If you want something you've never had, you need to do something you've never done.
Everyone should take the time to mark helpful posts and propose answers!Answer an interesting question?
Create a wiki article about it!
- Edited by Paul Ishak Sunday, January 6, 2013 3:14 PM
- Proposed as answer by Heslacher Sunday, January 6, 2013 3:41 PM
- Marked as answer by Mike Feng Thursday, January 10, 2013 9:50 AM
Sunday, January 6, 2013 3:12 PM -
Assigning events with AddHandler requires that the arguments of the sub you're attaching an event to has the parameters of the event. Timer.Tick has two parameters, the sender object, and the e eventargs. That's why when you double click a timer in the Designer, you get:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick End Sub
BarStaticItem is not passed from the Timer.Tick event, which makes it throw an error.
You could overcome it by, for example, making a custom class that holds a timer and an item.
Here's an example, adding a new class MyTimer:
Public Class MyTimer Public Tmr As Timer Public Item As BarStaticItem Sub New(tmr As Timer, item As BarStaticItem) Me.Tmr = tmr Me.Item = item AddHandler tmr.Tick, AddressOf Tmr_Tick End Sub Private Sub Tmr_Tick(sender As System.Object, e As System.EventArgs) Item.Appearance.BackColor = ColorTranslator.FromOle(14206908) Tmr.Stop() End Sub End Class
This way you can create a new MyTimer like this:
Public Sub GreenMessage(ByVal item As BarStaticItem, ByVal szoveg As String) item.Caption = szoveg item.ImageIndex = 2 item.Appearance.BackColor = ColorTranslator.FromOle(greenstatusbar) My.Computer.Audio.Play(My.Resources.sapmsg, AudioPlayMode.Background) tmr.Interval = 500 Dim tmrItem As New MyTimer(tmr, item) tmrItem.Tmr.Start() End Sub
I hope you got it and that this helps.
Sincerely yours,
- bilde2910If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
Check out my development so far!Sunday, January 6, 2013 3:15 PM
All replies
-
You cannot add an event handler that does not have the correct matching signature for the event you are handling. A timer.Tick event does not have the following signature.
ByVal item As BarStaticItem
If you want something you've never had, you need to do something you've never done.
Everyone should take the time to mark helpful posts and propose answers!Answer an interesting question?
Create a wiki article about it!
- Edited by Paul Ishak Sunday, January 6, 2013 3:22 PM
Sunday, January 6, 2013 3:09 PM -
I just want to clarify further, instead of trying to deviate from the intended signature of an event, instead try making your value public and accessing that value from there, meanwhile, you have to use a sub that has a matching signature of the event you are assigning to it.
Public Class Form1 Friend WithEvents Timer1 As New Timer Public Item As String Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddHandler Timer1.Tick, AddressOf Timer1_Tick End Sub Sub Timer1_Tick(sender As Object, e As EventArgs) End Sub End Class
If you want something you've never had, you need to do something you've never done.
Everyone should take the time to mark helpful posts and propose answers!Answer an interesting question?
Create a wiki article about it!
- Edited by Paul Ishak Sunday, January 6, 2013 3:14 PM
- Proposed as answer by Heslacher Sunday, January 6, 2013 3:41 PM
- Marked as answer by Mike Feng Thursday, January 10, 2013 9:50 AM
Sunday, January 6, 2013 3:12 PM -
Assigning events with AddHandler requires that the arguments of the sub you're attaching an event to has the parameters of the event. Timer.Tick has two parameters, the sender object, and the e eventargs. That's why when you double click a timer in the Designer, you get:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick End Sub
BarStaticItem is not passed from the Timer.Tick event, which makes it throw an error.
You could overcome it by, for example, making a custom class that holds a timer and an item.
Here's an example, adding a new class MyTimer:
Public Class MyTimer Public Tmr As Timer Public Item As BarStaticItem Sub New(tmr As Timer, item As BarStaticItem) Me.Tmr = tmr Me.Item = item AddHandler tmr.Tick, AddressOf Tmr_Tick End Sub Private Sub Tmr_Tick(sender As System.Object, e As System.EventArgs) Item.Appearance.BackColor = ColorTranslator.FromOle(14206908) Tmr.Stop() End Sub End Class
This way you can create a new MyTimer like this:
Public Sub GreenMessage(ByVal item As BarStaticItem, ByVal szoveg As String) item.Caption = szoveg item.ImageIndex = 2 item.Appearance.BackColor = ColorTranslator.FromOle(greenstatusbar) My.Computer.Audio.Play(My.Resources.sapmsg, AudioPlayMode.Background) tmr.Interval = 500 Dim tmrItem As New MyTimer(tmr, item) tmrItem.Tmr.Start() End Sub
I hope you got it and that this helps.
Sincerely yours,
- bilde2910If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
Check out my development so far!Sunday, January 6, 2013 3:15 PM -
Use the Timer's Tag property to hold the item:
Public Sub GreenMessage(ByVal item As BarStaticItem, ByVal szoveg As String)
item.Caption = szoveg
item.ImageIndex = 2
item.Appearance.BackColor = ColorTranslator.FromOle(greenstatusbar)
My.Computer.Audio.Play(My.Resources.sapmsg, AudioPlayMode.Background)
tmr.Tag = item
tmr.Interval = 500
AddHandler tmr.Tick, AddressOf tmr_tick()
tmr.Start()
End Sub
Private Sub tmr_tick(sender As Object, e As EventArgs)
Dim Item As BarStaticItem = DirectCast(tmr.Tag, BarStaticItem)
item.Appearance.BackColor = ColorTranslator.FromOle(14206908)
tmr.Stop()
End Sub
Sunday, January 6, 2013 3:46 PM