Dynamic adding controls (control array) but can't get MouseHover event to work

Andi0404

Newcomer
Joined
Mar 25, 2007
Messages
1
Hello.

Im using this code to add a custom control a have made.
Code:
        Dim AtomElement As Element() = New Element(118) {}
        Dim i As Integer = 1
        Dim X As Integer = 10
        Dim Y As Integer = 8

        Dim Xpos() As Byte = {0, 0, 17, 0, 1, 12, 13, 14, 15, 16, 17, 0, 1, 12, 13, 14, 15, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
        Dim Ypos() As Decimal = {0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5}
        Dim ElementColorIndex() As Byte = {0, 1, 13, 1, 2, 10, 11, 11, 11, 12, 13, 1, 2, 14, 10, 11, 11, 12, 13, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 14, 10, 10, 11, 12, 13, 1, 2, 3, 3, 3, 3, 3, 6, 6, 6, 7, 3, 14, 14, 10, 10, 12, 13, 1, 2, 4, 3, 3, 3, 3, 6, 6, 6, 7, 3, 14, 14, 14, 10, 12, 13, 1, 2, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 8, 9, 9, 9, 9, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}
        Dim ElementColor() As Color = {Color.FromArgb(0, 0, 0), Color.FromArgb(83, 142, 213), Color.FromArgb(149, 179, 215), Color.FromArgb(194, 214, 154), Color.FromArgb(209, 102, 0), Color.FromArgb(237, 215, 121), Color.FromArgb(203, 203, 203), Color.FromArgb(252, 213, 180), Color.FromArgb(215, 228, 188), Color.FromArgb(255, 255, 255), Color.FromArgb(216, 216, 216), Color.FromArgb(117, 146, 60), Color.FromArgb(246, 47, 0), Color.FromArgb(242, 242, 242), Color.FromArgb(165, 165, 165)}

        For i = 1 To 118

            AtomElement(i) = New Element
            AtomElement(i).Location = New System.Drawing.Point(10 + (Xpos(i) * 45), (60 * Ypos(i)))
            AtomElement(i).Size = New System.Drawing.Size(46, 61)
            AtomElement(i).SetAtomNumber(i)
            AtomElement(i).BackColor = ElementColor(ElementColorIndex(i))

            Me.Controls.Add(AtomElement(i))

        Next

And my problem is that I don't know how to get the event MouseHover to work. After the controls are on the form I want to show a messagebox when hovering the control. Can anyone pls help me, or point me in the direction on how to make the event work.
 
I recommend you research the AddHandler keyword. It provides a mechanism to dynamically add event handlers. If you are removing controls later on, you should also use the RemoveHandler keyword to remove handlers so that garbage can be properly collected.

Here is a quick sample that will handle the click event (I don't know the signature of the hover event and I don't have VB on this machine).
Visual Basic:
    Sub IDontKnowWhereThisSuffAllGoes
        Dim AtomElement As Element() = New Element(118) {}
        Dim i As Integer = 1
        Dim X As Integer = 10
        Dim Y As Integer = 8

        Dim Xpos() As Byte = {0, 0, 17, 0, 1, 12, 13, 14, 15, 16, 17, 0, 1, 12, 13, 14, 15, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
        Dim Ypos() As Decimal = {0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5}
        Dim ElementColorIndex() As Byte = {0, 1, 13, 1, 2, 10, 11, 11, 11, 12, 13, 1, 2, 14, 10, 11, 11, 12, 13, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 14, 10, 10, 11, 12, 13, 1, 2, 3, 3, 3, 3, 3, 6, 6, 6, 7, 3, 14, 14, 10, 10, 12, 13, 1, 2, 4, 3, 3, 3, 3, 6, 6, 6, 7, 3, 14, 14, 14, 10, 12, 13, 1, 2, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 8, 9, 9, 9, 9, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}
        Dim ElementColor() As Color = {Color.FromArgb(0, 0, 0), Color.FromArgb(83, 142, 213), Color.FromArgb(149, 179, 215), Color.FromArgb(194, 214, 154), Color.FromArgb(209, 102, 0), Color.FromArgb(237, 215, 121), Color.FromArgb(203, 203, 203), Color.FromArgb(252, 213, 180), Color.FromArgb(215, 228, 188), Color.FromArgb(255, 255, 255), Color.FromArgb(216, 216, 216), Color.FromArgb(117, 146, 60), Color.FromArgb(246, 47, 0), Color.FromArgb(242, 242, 242), Color.FromArgb(165, 165, 165)}

        For i = 1 To 118

            AtomElement(i) = New Element
            AtomElement(i).Location = New System.Drawing.Point(10 + (Xpos(i) * 45), (60 * Ypos(i)))
            AtomElement(i).Size = New System.Drawing.Size(46, 61)
            AtomElement(i).SetAtomNumber(i)
            AtomElement(i).BackColor = ElementColor(ElementColorIndex(i))

            AddHandler AtomElement(i).Click, AddressOf CustomClickHandler
            Me.Controls.Add(AtomElement(i))

        Next
    End Sub
 
    Sub CustomClickHandler(sender As Object, e As EventArgs
        MessageBox.Show("Holy Macaroni!")
    End Sub
Note lines 20 and 26-28.
 
Back
Top