Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 233233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:05:01+00:00 2026-05-11T20:05:01+00:00

This question is related to C#, but may be applicable to other languages as

  • 0

This question is related to C#, but may be applicable to other languages as well. I have a reservation against using code such as the following:

using System.Windows.Forms;

class MyForm : Form
{
    private Timer myTimer;
    private Button myButton;

    public MyForm()
    {
        // Initialize the components, etc.

        myTimer.Tick += new EventHandler( myTimer_Tick );
        myButton.Click += new EventHandler( myButton_Click );

        myTimer.Start();
    }

    private void myTimer_Tick( object sender, EventArgs eventArgs )
    {
        myTimer.Stop();
        // also, I see a lot of usage of 
        // Timer.Enabled = true/false instead of -^
        myButton_Click( this, ea /* or event EventArgs.Empty, or null */ );
        return;
    }

    private void myButton_Click( object sender, EventArgs eventArgs )
    {
        // do a lot of stuff, with lots of logic that doesn't even use the
        // state of the eventArgs
        return;
    }
}

Am I alone, in that the above style is a pet peeve of mine? Are there others who enjoy the clarity of separating event handling from the workload of functions, or even separating out complex routines into separate functions?

Is there even an accepted style? I feel like any expressiveness and flexibility that event handling in C# has can be lost with styles like this. I feel like if you have a method that means “a button has been clicked”, then it should only be called when a button is clicked.

To those who write like this, I would say: if you insist on having an EventHandler method to handle your timer tick, and your button click, then call it something other than button_Click — perhaps “handleUserEvent( object sender, EventArgs eventArgs )“.

Really, though, the question is, are there any style guidelines that are widely used which either support or discourage usage such as the above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:05:02+00:00Added an answer on May 11, 2026 at 8:05 pm

    I agree with Rex M’s answer, but I’d take it one step further. If you are using the MVC pattern (or something similar), the view would delegate the button click to the controller. The controllers methods can of course be called from elsewhere in your class – say, from your timer callback.

    So, back to your original code:

    using System.Windows.Forms;
    
    class MyForm : Form
    {
        private Timer myTimer;
        private Button myButton;
    
        private MyController myController;
    
        public MyForm()
        {
            // ...
            // Initialize the components, etc.
            // ...
    
            myTimer.Tick += new EventHandler( myTimer_Tick );
            myButton.Click += new EventHandler( myButton_Click );
    
            myTimer.Start();
        }
    
        private void myTimer_Tick( object sender, EventArgs eventArgs )
        {
            myTimer.Stop();
            myController.SomeMethod()
        }
    
        private void myButton_Click( object sender, EventArgs eventArgs )
        {
            // All the stuff done here will likely be moved 
            // into MyController.SomeMethod()
            myController.SomeMethod();
        }
    }
    

    One advantage of using MVC is the decoupling of the controller from the view. The controller can now be used across multiple view types easily and exiting GUIs are easier to maintain as they contain very little application logic.

    EDIT: Added in response to comments from the OP

    The fundamental design principals of software engineering talk about coupling and cohesion. Importantly we strive to minimise coupling between components while maximising cohesion as this leads to a more modular and maintainable system. Patterns like MVC and principals like the Open/Closed Principal build on these fundamentals, providing more tangible patterns of implemenation for the developer to follow.

    So, anyone who writes code as seen in the original post has not understood the fundamentals of software design and needs to develop their skills considerably. The OP should be commended for identifying this “code smell” and trying to understand why it’s not quite right.

    Some relevant references:

    • http://en.wikipedia.org/wiki/Coupling_(computer_science)
    • http://en.wikipedia.org/wiki/Cohesion_(computer_science)
    • http://en.wikipedia.org/wiki/Loose_coupling
    • http://en.wikipedia.org/wiki/Model–view–controller
    • http://en.wikipedia.org/wiki/Design_patterns
    • http://en.wikipedia.org/wiki/Open/closed_principle
    • http://en.wikipedia.org/wiki/Design_Patterns_(book)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is kind of related to another question but I have a specific
There is another possibly related question on this but it didn't have answers and
I've looked at other questions on Stack Overflow related to this question, but none
This question is related to this one , but simpler. [I think I may
I think my question may be related to this question here but i'll ask
This question may not be exactly programming related, but this is about development. I
This question is related with Microcontroller programming but anyone may suggest a good algorithm
I have looked around but cannot find anything that answered this particular question related
This is more like a design question but related to SQL optimization as well.
This is somewhat related to this question , but I was hoping there may

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.