Is there a prettier way to handle lots of controls with lots of Click events?

Posted on 2006-05-08
Last Modified: 2010-04-16
Sounds like a silly questions.  I used to program in VS (VB and C++) quite awhile ago.  I'm back in the MS world of programming and like a lot of the new features.  However, I'm wondering if we can group the click events or maybe create a subclass in the main class to handle the events.  

In short, I'm looking at a class that is more than 20 pages long with a lot of methods and click events, thinking there has got to be a better way.  Any ideas?

Question by:pdlarue
    LVL 15

    Expert Comment

    Might depend on what you're click-event-handling method look like.  Are they all very similar to eaach other, and/or is there a lot of overlapping/duplication functionality that could be abstracted into helpful methods, etc.

    Also, if you're looking at a single page/form/control that has a lot of UI elements (and their respective event handlers) all over it, it might be worth grouping those elements into composite controls that each focus on a certain functional group.  (So you could have a page/form that has a handful of controls, and each control contains a handful of child controls, etc.)

    It'd be difficult to be much more specific without seeing some examples of what you're dealing with...
    LVL 4

    Accepted Solution

    Normally, we create an Handler for a user-defined event. to event of .NET control, such as, click, load, etc. we need to use System.EventHandler. If we define a delegate and event for our user control, it's ok too.
    Your idea sound good if we group all reuse function in a class. Then use it for our event handler.
    If you have two or more button which is handled by the same function, we use this:
    this.button1.Click += new System.EventHandler(this.MyHandler_Click);
    this.button2.Click += new System.EventHandler(this.MyHandler_Click);

    Author Comment

    Thank you, nguyenvinhtu, your answer leads me to believe that .NET code can be written using the standard Microsoft default structure or it can be written using a more Java like structure.  If this is true then we can write these events in a separate class for reusability.  I will explore this concept further.  Thank you for your reply.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now