-1

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Dravis85
  • 85
  • 1
  • 11
  • Because you have to work with controls you can put it "anywhere" - important that binding must happened after `InitializeComponents` method. Remember that if your "binding" takes a time - form will not be showed before all binding completes if you bind it in `Load` eventhandler. if you have some logic which can be used later then use `Shown` event. – Fabio Dec 01 '16 at 22:14

1 Answers1

0

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).

Community
  • 1
  • 1
Gimly
  • 5,975
  • 3
  • 40
  • 75
  • I try to do the segregation of UI, Business methods, data access, etc, but where I'm at they are kinda against such paradigms. The older guys here just want to see the code "right up front without digging." A lot of the code they wrote should have been placed in a business class, but most everything is in code behind :/ The goal of the question was to be sure no one said "Hey that's not going to work." My research says I'm fine, but I'm a newbie so I get scared. – Dravis85 Dec 01 '16 at 21:41
  • Yeah unfortunately the industry is full of "It always worked like that" people, but trust me, separating things will help you a lot in the long run. – Gimly Dec 01 '16 at 22:02