0

Due to a new job opportunity I am switching back from WPF to WinForms.

Currently I am playing around a little to get used to it.

Here are my main questions that arose:

  • Should I use the designer in a professional environment? It feels somehow wrong to get that auto-generated clumsy event handlers... Otherwise generating all manually is hell of a work. But this seems more stable to me especially in combination with git.

  • Do you leave your control logic in its cs file or do you capsule it in some kind of viewmodel/controller class?

Thanks in advance for the help!

selmaohneh
  • 313
  • 1
  • 7

2 Answers2

3

Do use the visual designer to layout your GUI. Doing anything else is masochism.

Don’t put all of your code in the Form’s code behind.

Do use the Model-View-Presenter pattern. The form raises an event, which the presenter listens for, and reacts to by modifying the view and performing other business logic.

RubberDuck
  • 8,911
  • 5
  • 35
  • 44
  • 1
    The key point here is Winforms is for UI and there should be a proper separation between code behind and business logic – Liath Mar 26 '18 at 16:23
0

Regarding the first question, as long as the designer meets muy needs, I usually use it. If or when I want to do something more sophisticated (e.g. create or modify controls dynamically, or even initializing them according to complex rules), then I move to code. In any case, I choose the names ( both of controls and of event handlers) that I use in the code and don't just keep the auto generated ones. Regarding the second question, for simple UI logic (e.g. enable/disable another control) I usually keep it in the code- behind cs file. But for anything further than this, you should use an MVC or MVP pattern. Implementing MVVM is not very feasible because binding in WinForms is pretty limited

  • 1
    Not the downvoter, but it is possible to implement MVVM in winforms. I created a proof of concept a few years back. https://github.com/rubberduck203/Rubberduck.Winforms – RubberDuck Mar 26 '18 at 16:16