Results 1 to 7 of 7

Thread: WithEvents Class Array

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Granby, Qc, Canada
    Posts
    602

    WithEvents Class Array

    I created a class and inside of it, I created a Public Event.

    Now, in my form, I want to declare an array of my class and I want all of them linked on the event I created in my class, but I seems I can't use WithEvents with an array !

    Is there any workaround for this ?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: WithEvents Class Array

    You will have to loop through each element in the array and add an event handler like this:

    (example with a textbox array)
    VB Code:
    1. Private tb(5) As TextBox
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         For Each t As TextBox In tb
    4.             AddHandler t.TextChanged, AddressOf ArrayTextChanged
    5.         Next
    6.     End Sub
    7.  
    8.     Private Sub ArrayTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    9.  
    10.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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

    Re: WithEvents Class Array

    More likely you'd connect the event handler to each object as you created it, unless of course you're getting the array already filled from another location.

    Also note that in the event handler, as is always the case, the 'sender' argument is a reference to the object that raised the event.
    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

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Granby, Qc, Canada
    Posts
    602

    Re: WithEvents Class Array

    How come if my event have parameters ?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Granby, Qc, Canada
    Posts
    602

    Re: WithEvents Class Array

    My class name is clsBlock.
    My event is BlockChanged(Byval eImageType as enumImageType).

    I'm using this :

    VB Code:
    1. For i = 0 to 39 'I have 40 class item in an array
    2.   AddHandler myClsBlock(i).BlockChanged, AddressOf ArrayBlockChanged
    3. Next

    It's not working

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

    Re: WithEvents Class Array

    That's not a good event signature. You should ALWAYS stick to the convention of a 'sender' argument and an 'e' argument that is or derives from EventArgs. In your case you should declare your own class that inherits EventArgs:
    Code:
    Public Class BlockChangedEventArgs
        Inherits EventArgs
    
        Private _imageType As enumImageType
    
        Public ReadOnly Property ImageType() As enumImageType
            Get
                Return Me._imageType
            End Get
        End Property
    
        Public Sub New(ByVal imageType As enumImageType)
            Me._imageType = imageType
        End Sub
    End Class
    Now you declare your event like this:
    Code:
    Public Event BlockChanged(ByVal sender As Object, ByVal e As BlockChangedEventArgs)
    
    Protected Overridable Sub OnBlockChanged(ByVal e As BlockChangedEventArgs)
        RaiseEvent BlockChanged(Me, e)
    End Sub
    Now you shoudl raise the event like this:
    Code:
    Dim e As New BlockChangedEventArgs(myImageType)
    
    Me.OnBlockChanged(e)
    Now you would write an event handler like so:
    Code:
    Private Sub ArrayBlockChanged(ByVal sender As Object, ByVal e As BlockChangedEventArgs)
        Dim myBlock As clsBlock = DirectCast(sender, clsBlock)
        Dim myImageType As enumImageType = e.ImageType
    End Sub
    Now you can attach that event handler to an array of clsBlock objects like this:
    Code:
    For Each block As clsBlock In myBlockArray
        AddHandler block.BlockChanged, AddressOf ArrayBlockChanged
    Next block
    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

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Granby, Qc, Canada
    Posts
    602

    Re: WithEvents Class Array

    Well, I guess it's a good way to work, I'll have to try to understand it, it seems tricky !

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