开发者

Is this the best way for me to set events for my controls?

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);
    }


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;


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜