[RESOLVED] Raise Event question..
Hello,
Right now I have some questions regarding how to raise an event... Let's say I have a class that does some calculation using a For... Next loop, from 0 to 100... How would I return the value to the main form AFTER each iteration is completed? How would I raise the event? I have read the documentation but I can't wrap my head around this...
Any help is greatly appreciated!
Thanks in advance!
Re: Raise Event question..
I'm making some assumptions here but if anything doesn't fit your circumstances exactly you can ask about it later.
vb.net Code:
Public Class Form1
Private WithEvents myLooper As Looper
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.myLooper = New Looper
Me.myLooper.PerformLoop()
End Sub
Private Sub myLooper_IterationComplete(ByVal sender As Object, _
ByVal e As IterationCompleteEventArgs) Handles myLooper.IterationComplete
Console.WriteLine("Iteration {0} completed.", e.Count)
End Sub
End Class
Public Class IterationCompleteEventArgs
Inherits EventArgs
Private _count As Integer
Public ReadOnly Property Count() As Integer
Get
Return Me._count
End Get
End Property
Public Sub New(ByVal count As Integer)
Me._count = count
End Sub
End Class
Public Class Looper
Public Event IterationComplete As EventHandler(Of IterationCompleteEventArgs)
Public Sub PerformLoop()
For count = 0 To 100
'Do something here.
Me.OnIterationComplete(New IterationCompleteEventArgs(count))
Next
End Sub
Protected Overridable Sub OnIterationComplete(ByVal e As IterationCompleteEventArgs)
RaiseEvent IterationComplete(Me, e)
End Sub
End Class
Re: Raise Event question..
Thanks John! Now I know what I have to do! I'll post back with specific questions!
Re: Raise Event question..
John, I have it now, thanks! No need for questions, your example was just the jump start kick I needed.
Re: [RESOLVED] Raise Event question..
Always happy to give someone a kick. ;)
Re: [RESOLVED] Raise Event question..
I did not know generics could have constraints. (Referring to the EventHandler) That's pretty cool!
Re: [RESOLVED] Raise Event question..
Quote:
Originally Posted by
ForumAccount
I did not know generics could have constraints. (Referring to the EventHandler) That's pretty cool!
While generics can have constraints, this is not an example of it. There are simply two different EventHandler delegates: one standard and one generic.
http://msdn.microsoft.com/en-us/libr...nthandler.aspx
http://msdn.microsoft.com/en-us/library/db0etb8x.aspx
If you want an example of generic constraints then check this out.
Re: [RESOLVED] Raise Event question..
The EventHandler(Of TEventArgs) is constrained to a base type of System.EventArgs, that's what I was pointing out (more or less).
Re: [RESOLVED] Raise Event question..
Quote:
Originally Posted by
ForumAccount
The EventHandler(Of TEventArgs) is constrained to a base type of System.EventArgs, that's what I was pointing out (more or less).
D'oh! Of course you were. :sick: I hadn't even considered that that generic type was constrained, even though I provided a link to the doco page that defines it. :blush: I guess I just never considered using a type that didn't inherit EventArgs so I didn't consider that you couldn't if you wanted to. Good old human brain, filling in the gaps with general fuzziness that we just accept.