Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello !

I have a program with +-8000 lines of code(.NET 4.0, winforms). The idea is to separate code into different *.cs files. I know how to use partial classes, but I can't think of solution how to split event handler. Let's have this scenario:
Main.cs file:
public partial class Main
{
   void ButtonClick(object sender, EventArgs e)
   {
       // Do some calculation for example...
   }
}


and then PartialMain.cs file:
public partial class Main
{
   void ButtonClick(object sender, EventArgs e)
   {
       Button.Enabled = false;
   }
}


I have many winforms objects added to program´s GUI and I want to separate code for calculation and code for controlling smth.Enabled property.
How do I split ButtonClick function?
partial void ButtonClick(object sender, EventArgs e) 

This is not working.
Posted

You cannot have two identically named methods with identical lists of identical parameter types in the same NameSpace, even if the contents of a Form Class in that NameSpace (constants, variables, properties, methods, functions, etc.) are, for organization, defined in multiple files, by using 'partial with the Class 'Name.

You can however, have more than one EventHandler assigned to an Event (Events in .NET are, by design, multi-cast). And, you can manipulate which EventHandlers, at any given time are defined, adding them with +=, and removing them with -=. I doubt that's your real concern here, however.

You got great advice from OriginalGriff on refactoring in his response here; I'll take that one step further, and state my opinion that what you want to aim for is something like the ideas in the MVC (model-view-controller) concept in OOD.

For example, you might start by creating a public Class called 'Calculations in which you isolate the functions that simply take input parameters which may come from one, or many, of your GUI controls, and return some type of value. In that class you "hide" all the logic of those calculations from the code that manages the users *typically" asynchronous interaction with the GUI controls.

If your user interface switches between different "modes" in which, depending on the mode, some controls are hidden, or disabled, and others are shown and enabled, you might consider defining different classes to manage the GUI configuration for each mode.

So, if the user is working in "mode 1," and switches to "mode 2:" a method is called in the Mode2ViewManager class, which must have access, of course, to your Form(s), that "does the right thing" to hide/show enable/disable what you want the user to see, and use, in that mode.

That kind of organization can lead to much easier to understand code, possibly removing a lot of complex if/the/else clauses, or switch/case, statements, if all the GUI view management is one Form Class.

Finally, I suggest you start reading some of the tutorial articles here on CP on MVC design philosophy. While the nature of WinForms is that it combines (in a Form) both logic and presentation: you are not "stuck with that model" at all.
 
Share this answer
 
You can't "split a method into two sections" - the compiler would not know which to execute first!

Instead, move the code that the Event handler executes into a method and call that from the event handler. You can then refactor that method into as many sub-methods as you feel necessary.

[edit]
As a suggestion, rather than trying to break your single class into separate files, it might be an idea to look at breaking it into UserControls, separate classes and so forth where possible, to keep the size of your form down to manageable proportions. I realize that isn't alway possible, but it can make a big difference to code quality if you can.
[/edit]
 
Share this answer
 
v2
.Net doesn't support the functionality of partial events. So, you have to think yourself how you should manage it. The best possible way is not to write everything within the same event, but some very basic functionality or something which can not be separated from it. Move rest of the code to some clean class/method and then call the objects of the class or methods from that event. Before doing anything, have a look at the best practices for coding in .Net as sometimes you really need to know how to write code in a best possible way to avoid such kind of issues.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900