Results 1 to 9 of 9

Thread: [RESOLVED] Raise Event question..

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [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!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Public Class Form1
    2.  
    3.     Private WithEvents myLooper As Looper
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Me.myLooper = New Looper
    7.         Me.myLooper.PerformLoop()
    8.     End Sub
    9.  
    10.     Private Sub myLooper_IterationComplete(ByVal sender As Object, _
    11.                                            ByVal e As IterationCompleteEventArgs) Handles myLooper.IterationComplete
    12.         Console.WriteLine("Iteration {0} completed.", e.Count)
    13.     End Sub
    14.  
    15. End Class
    16.  
    17.  
    18. Public Class IterationCompleteEventArgs
    19.     Inherits EventArgs
    20.  
    21.     Private _count As Integer
    22.  
    23.     Public ReadOnly Property Count() As Integer
    24.         Get
    25.             Return Me._count
    26.         End Get
    27.     End Property
    28.  
    29.     Public Sub New(ByVal count As Integer)
    30.         Me._count = count
    31.     End Sub
    32.  
    33. End Class
    34.  
    35.  
    36. Public Class Looper
    37.  
    38.     Public Event IterationComplete As EventHandler(Of IterationCompleteEventArgs)
    39.  
    40.     Public Sub PerformLoop()
    41.         For count = 0 To 100
    42.             'Do something here.
    43.  
    44.             Me.OnIterationComplete(New IterationCompleteEventArgs(count))
    45.         Next
    46.     End Sub
    47.  
    48.     Protected Overridable Sub OnIterationComplete(ByVal e As IterationCompleteEventArgs)
    49.         RaiseEvent IterationComplete(Me, e)
    50.     End Sub
    51.  
    52. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Raise Event question..

    Thanks John! Now I know what I have to do! I'll post back with specific questions!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Raise Event question..

    John, I have it now, thanks! No need for questions, your example was just the jump start kick I needed.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Raise Event question..

    Always happy to give someone a kick.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [RESOLVED] Raise Event question..

    I did not know generics could have constraints. (Referring to the EventHandler) That's pretty cool!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Raise Event question..

    Quote Originally Posted by ForumAccount View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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).

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Raise Event question..

    Quote Originally Posted by ForumAccount View Post
    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. I hadn't even considered that that generic type was constrained, even though I provided a link to the doco page that defines it. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width