|
-
Feb 27th, 2007, 05:50 PM
#1
Thread Starter
Fanatic Member
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 ?
-
Feb 27th, 2007, 06:03 PM
#2
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:
Private tb(5) As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each t As TextBox In tb
AddHandler t.TextChanged, AddressOf ArrayTextChanged
Next
End Sub
Private Sub ArrayTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
-
Feb 27th, 2007, 06:06 PM
#3
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.
-
Feb 27th, 2007, 06:12 PM
#4
Thread Starter
Fanatic Member
Re: WithEvents Class Array
How come if my event have parameters ?
-
Feb 27th, 2007, 06:21 PM
#5
Thread Starter
Fanatic Member
Re: WithEvents Class Array
My class name is clsBlock.
My event is BlockChanged(Byval eImageType as enumImageType).
I'm using this :
VB Code:
For i = 0 to 39 'I have 40 class item in an array
AddHandler myClsBlock(i).BlockChanged, AddressOf ArrayBlockChanged
Next
It's not working
-
Feb 27th, 2007, 06:41 PM
#6
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
-
Feb 27th, 2007, 09:42 PM
#7
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|