I have a large (at least for me - approximately 10k lines) project that is written in C#. I have a treeview object on the left of my form and i have many nodes on my treeview. When i click a node, i bring up a panel on the right side of form. Since i only do GUI item operations (filling textboxes, clicking buttons, showing images on pictureboxes etc.), i write all of my code into MainForm class. This seems not very good but on the other side, i think i have to write into MainForm class because all of my job is to evaluate form items on panels. What should i do to write a more portable or understandable code? Do i have to write into MainForm class or do i have some options or methods to implement?
|
GUI classes can quickly grow into a ball of mud if they're not managed carefully. Simple refactoring will do the trick. Some tips:
|
|||||||||||||||
|
|
That kind of code would be much easier to handle if, for example, instead of having all the customizing behavior of a treeview in your MainForm, you actually had a class that extended TreeView and made all of the customization there, and your MainForm just used it. You should do that for every control that exists in your window (or canvas). |
|||
|
|
|
This is something that you will only learn by writing more and more code. A 10k line file is gigantic, and you probably have a lot of code that can be split out. The problem is that if you didn't initially approach the problem with plans to separate concerns, things may be so intermingled now that it would require essentially an entire rewrite. That said, at 10k lines, is the program "complete"? Or is someone going to be maintaining this? If this was a small one off utility, then you might be best off to leave as is. On the other hand, if this is something you need to maintain, you will likely benefit from intense refactoring. Not knowing your domain makes this difficult, but here are some common forms patterns that might be applicable:
|
||||
|
|
|
I guess you do not have only GUI operations (or at least not only operations that must be programmed in this way): your treeview shows some data, and in your panels you have also a lot of data. You may have used (or abused) just GUI items as containers for this, but that's not the way things must (or should) be organized. I suggest start separating the GUI-dependent parts from the data. Then you will most probably identify GUI-independent operations which you can separate from your main form. |
|||||||||
|