Organization Using WinForms

So, I just started developing C # WinForm applications, and every project I worked on seems to be larger and requires more user-defined functions. If I add all the functionality to one form, it is obvious that it can get out of hand very quickly. In my last project, I would separate the functionality into separate Forms, and whenever someone said that they wanted to execute “Feature1”, I would create an instance of Feature1 Form and show it as a dialog with the main form as its owner (therefore they don’t could click on it).

I'm just wondering what other methods exist to store code organized in Forms. If you have to have tons of functions in one form, is there a good way to keep elements organized? I just hate having a code file with a length of hundreds / thousands of lines.

The answer may simply be in design, try creating an interface before you can use multiple forms?

Another example that I came across. I created a Tab control and had about 5 tabs. These 5 tabs had many features and were saved in the same CS file. What were my other options? Create a new custom TabControl class with my specific functionality for this tab?

I do not mind reading, so if there are decent articles, feel free to link them!

+5
source share
3 answers

The go-to method is a controller / presenter. The idea is that the window should only be responsible for actually managing the user interface events of its controls, and it should do this by invoking methods on the controller that do the real work for the window. A window can have the necessary handlers or bind user interface events directly to controller methods; the first, as a rule, is a simpler method, but it may be tempting to sneak into a line of code here or there, which really should be in the Controller method. By doing this, you parse the layout and presentation logic in the Form class with the business logic in the controller.

Mark Hall . UserControl , , , "", . , ; DRY , , , , .

+4
+2

I kind of recommend sharing your logic code with the user interface. If you do this, you need to be careful about how calls are made in the application to avoid Cross Thread exceptions. I was taught how to create delegates and events for updating the user interface from a logical class, but MSDN , of course, also has a lot of information on making thread-safe calls.

+1
source

All Articles