Question

I have a MouseEnter event which currently handles some custom controls on my form. The program is a card game. I have a collection (handCards) that gets populated when the user draws a card and then it adds the latest card to the form. This collection holds cards of various custom types, all which inherit from picturebox. Drawing the cards from the deck and adding them to the form works fine. The trouble I am having is that at runtime, after a card is drawn and added to the form, I've created an addhandler line of code to have those cards respond to my MouseEnter event, but my addhandler line of code is telling me that MouseEnter is not an event of object. How can I get around this so that after a card is drawn and added to the form, when the mouse enters the new custom control, my MouseEnter event fires? Here's one of the many things I've tried and what I think should be the simplest and easiest that should work.

deck.DrawCard()
AddHandler handCards(handCards.Count).MouseEnter, AddressOf Cards_MouseEnter

P.S. the MouseEnter event works fine for custom controls that are on the form prior to runtime and all it does is take the image of the control and enlarge it by placing the image to a bigger card on the form.

Was it helpful?

Solution 2

So this is how I fixed it, in case anyone comes across this post. Made a separate Sub to do the AddHandler. After the program draws a card, it calls upon this method, which then adds the MouseEnter handler I need. The ByVal was key. I originally thought I was supposed to use ByRef, but no. MouseEnter is an event of control, but apparently not Object, so now it works.

Public Sub addHandlers(ByVal inputObject As Control)
    AddHandler inputObject.MouseEnter, AddressOf Cards_MouseEnter
End Sub

OTHER TIPS

I am supposing your handCards Collection is an Object Collection. Try casting it to the proper type using CType, something like this:

AddHandler CType(handCards(handCards.Count), PictureBox).MouseEnter, AddressOf Cards_MouseEnter

as @Jason mentioned using the handCards.Count as an index will not work because it is the total number of items where as your index is zero based and will be one less than the Count. so handCards(handCard.Count) should be handCards(handCards.Count -1)

You could use a generic collection to avoid type casting.

Private handCards As System.Collections.Generic.List(Of PictureBox) _
    = New System.Collections.Generic.List(Of PictureBox)(52)

Or you could just use an array of PictureBox objects

Private handCards(5) As PictureBox

Remember that you'll have to initialise the collection or array though, by assigning a PictureBox object to each element of the array.

Now you can add the handler to a PictureBox element of the array since PictureBox derives from Control which implements the MouseEnter event.

deck.DrawCard()
If handCards.Count > 0 andAlso handCards.Last() IsNot Nothing then
    AddHandler handCards.Last().MouseEnter, AddressOf Cards_MouseEnter
End If

Your handler will look something like this

Private Function Cards_MouseEnter(sender As Object, e As System.EventArgs) As Object
    ' Handle mouse events here
End Function

Luckily I was working around and I managed to do successfully this solution.

First Add Event Handler Method Where ever you Wish, For Test I have added this Function at Button_Click

addHandlers(Label1) 'Label one is the control on which I have to attach Mouse Events (Enter,LEave)

Now "addHandlers" function implementation

 Public Sub addHandlers(ByVal obj1 As Control)
    AddHandler obj1.MouseEnter, AddressOf MouseEventArgs
    AddHandler obj1.MouseLeave, AddressOf _MouseLeave
 End Sub

Now the Mouse Events:

Private Function _MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) As Object
    Try
        Me.Cursor = Cursors.Default
    Catch ex As Exception

    End Try

End Function

Private Function MouseEventArgs(ByVal sender As Object, ByVal e As System.EventArgs) As Object
    Try
        Me.Cursor = Cursors.Hand
    Catch ex As Exception

    End Try
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top