Jon N. Jon N. - 1 year ago 97
C# Question

Winform control code placement

I'm adding a bar manager and popup menu control to a Winforms application. I have to add the code to bind the menu to the manager, but I don't know where it would be best to do so.

I'll be using the designer heavily (company mandated) for the rest of the build-out, but the binding has to be done in code AFAIK. Currently I have it in the form load method.

I believe this is just fine to make it work, but I'm curious if you could put it in the designer code with the control details, or if it should go somewhere else in the code behind.

Hopefully this isn't an opinion based question.

Answer Source

Winforms doesn't make it very easy to separate things correctly, but you should try to separate as much as possible the UI code from the functional code.

Basically, you should try to put all your business logic in classes that are separated from your UI. Try to think that all that code could be used by another type of application, like a web app, or a WPF app.

The things that are in the codebehind should be only related to UI management, updating the UI and passing the changes to your business classes. There also seems to be some things that exist to have a MVVM or MVP on Winforms, check this SO question: UI Design Pattern for Windows Forms (like MVVM for WPF).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download