events with controls added at runtime

liquid8

Freshman
Joined
Jan 14, 2003
Messages
31
This one is really baffling me..

My project adds controls in a For Next loop at startup, each control when added has AddHandlers specified to link the mouse events to that control. I then pass the event that is called to a function which sets the properties of that control.. so now I can change the image or text properties for each event.

Here's the problem. I call the Setproperties function when the control is originally created, and then each time an event is called.

The first control that is added works fine, it responds to all events, and sets the properties for each.

Every control after that, when it is originally added it sets the properties, but does not set them when the events occur. The events are however firing, it's just not setting the properties for the event. Any ideas as to why this might be happening?

I am sure this might be confusing, and I can add the code when I get home, but it's been bothering me all day! :)

Thanks in advance,

liquid8
 
Can you post the relevant code? I assume you're using the "sender" argument as the control to set the properties on?
 
ItemCollection is a collection of strings with names for a new control, newPage is a Form, and SkinnedItem is a Class which inherits a panel, and has a few extra properties

Here is the initial adding of the controls

Visual Basic:
       Dim strInfo As String
        'Add each Item to the page
        For Each strInfo In itemCollection
            Dim newItem As New SkinnedItem()
            'Set Item Properties
            newItem.Name = strInfo
            newItem.SetItemProps(newItem, strInfo)
            newItem.Initialize
            'Add the Button
            newPage.Controls.Add(newItem)
        Next

This is the code in the initialize method for a SkinnedItem which attaches the events

Visual Basic:
Public Sub Initialize()
            AddHandler Me.MouseEnter, AddressOf Mouse_Enter
            AddHandler Me.MouseLeave, AddressOf Mouse_Leave
            AddHandler Me.MouseUp, AddressOf Mouse_Up
            AddHandler Me.MouseDown, AddressOf Mouse_Down
End Sub

Here is the SetItemProps method

Visual Basic:
Public Sub SetItemProps(ByVal Item As Object, ByVal ItemName As String, optional EventName as string)
          Select Case EventName
             Case "onmouseenter"
                Item.BackgroundImage = Image.FromFile("C:\temp\image1.jpg")
          End Select
End Sub

And here is the Event Code

Visual Basic:
Public Sub Mouse_Enter(ByVal sender As Object, ByVal e As System.EventArgs)
        If sender.GetType.Name.ToString = "SkinnedItem" Then
            SetItemProps(sender, sender.name, "onmouseenter")
        End If
End Sub

I had to simplify some of the code, to make it understandable as I am reading all the properties for each event from an xml file, but if you don't see anything in this... I could attach the project.

Thanks a bunch,

liquid8
 
Have you set some breakpoints to see if you're: getting in the Mouse_Enter event, if you are getting in that sender's type is "SkinnedItem", and if so then SetItemProps is getting called and falling into the .BackgroundImage code?

-ner
 
Ive run all the way through many times and it seems to be getting the correct values for the 2nd control that is added, I see no difference between that and the 1st one.

I think it has something to do with the sender but i just can't figure it out....

an important bit of info i left out, the SkinnedItem class adds a Label to itself (i needed to use a panel for certain reasons, but I also needed to change the offset of the label from the panels BG image... best way i could figure to do it) anyways.. so maybe the sender is confusing the two... still, I had that problem before, and all it did was just change the properties of the Label to the SkinnedItems properties..

that being said, when i break on the mouse event, it is showing that the sender is SkinnedItem, so I dunno...

thanks guys,

liquid8
 
Well, I did a seperate test adding the controls dynamically, then attaching the events, and it works fine, so I will just have to bang my head against the wall till i figure out what in my code is causing the prob ;)

I will post when I figure out what I'm doing wrong.. Just a note, I am not using a seperate class for the handlers on the test, so somethings going on in there..

liquid8


********** Found It **********

It was getting incorrect values from the XML. Anyways, my coding was incorrect for searching the XML, and it was basically retrieving the same information for every event.. thereby it was doing everything, just not changing..

Thanks guys,

liquid8
 
Last edited:
Back
Top