When adding code to event handlers to a form code behind (i.e. your button click event), consider the following guideline(s).
Place as little code in the event handler as possible. You should delegate the actual functionality out to a user interface process block or business object or some other type of controller depending on what pattern you are implemeting. The code in the event itself should only dispatch to some internal implementation handler and make sure system generated errors do not get to the end user.
Consider the following example
public class SampleCode
private void NewButton_Click(object sender, EventArgs args)
this.MousePointer = Cursors.AppStarting;
// Safe to display this message to the user and provide options
// or suggestions on how to proceed
// something unexpected happened, log the exception
this.MousePointer = Cursors.Default;
public class Program
public static void Main(string args)
public static void FatalExceptionHandler(...)
// 1) Notify the user with safe generic message
// 2) If you can save the state of the application so the user
// can pick where they left off when exception happened
// 3) Let the user know the application needs to be restarted
In my example here, one could argue the cursor handling code should be moved to the CreateNewItem(…) routine, I think you get the still get the point.
I know you may have heard this one before, even so I have participated in several code reviews with several lines of code dealing with application logic directly in the event handler. The entire point to this post is to have all of my friends in C# land to keep the code behind those events in your UI short and simple.
Use Code Complexity Tools
Create code that is easy to maintain looking for areas to reduce code complexity. There are tools to help do this check out a few listed below:
In addition review Mike Swansons blog on code complexity here.