views:

53

answers:

4

I'm setting them during runtime, I'm kinda afraid that this will bog down my application eventually.

Is there a better way?

public MainForm()
    {
        InitializeComponent();
        SetPictureBoxEvents();
    }



    private void SetPictureBoxEvents()
    {
        Andromeda.MouseEnter += new EventHandler(HeroMouseEnter);
        Andromeda.MouseLeave += new EventHandler(HeroMouseLeave);
        Engineer.MouseEnter += new EventHandler(HeroMouseEnter);
        Engineer.MouseLeave += new EventHandler(HeroMouseLeave);
        Nighthound.MouseEnter += new EventHandler(HeroMouseEnter);
        Swiftblade.MouseEnter += new EventHandler(HeroMouseEnter);
    }
+3  A: 

I don't see why this would bog down your application, since they are set at runtime anyways (as part of InitializeComponent, if you wire it up using the designer). You'd just be changing when they get set.

Also, you can remove the "new EventHandler" part from each:

//like this
Andromeda.MouseEnter += HeroMouseEnter;
dan
+3  A: 

Just remember to unsubscribe the events when the object is disposed. Take a look at this article.

Yuval Peled
This is not necessary, these are not static events. When the controls get garbage collected, along with their event handler lists, they cannot keep the form object alive.
Hans Passant
+2  A: 

There is no problem with this, and it won't bog down your application...

Except...

If you call this function more than one time, for a single instance of your class, you could cause problems. Each time you call SetPictureBoxEvents(), you'll get a new set of delegates attached to your event handler. If you call this four times, and don't unsubscribe, your routines will run 4 times each when an event is raised.

Reed Copsey
+1 for pointing out the perils of doing these things in a loop.
Jason D
+1  A: 

There is no problem here, there can be only one event triggered when the user moves the mouse. That takes roughly a dozen nanoseconds. Moreover, these events happen in "human-time", triggered by the user moving the mouse. S/he'll perceive anything that takes less than 20 milliseconds as "instant". You've got a margin of 1:166,666 of having a problem.

Hans Passant