Do you have your Visual Studio WinForms Designer License?
I often joke at work that you should need a license to use the Visual Studio GUI Designer. Not because it is hard to use- quite the contrary. Because it is so goddamn easy to make a UI, people forget that UI classes should be written like any other piece of code, NOT as if they were created using a WYSIWYG editor.
Since when was it OK to have an object with 70 fields? Why is OK to have a UserControl with 70 fields on it, including a context menu and top menu, a tab control and three full tab pages set up on it?
I’ve seen very talented programmers forget that they are talented programmers, and build UI’s with the designer that are absolutely atrocious. Here’s how I’d suggest you break bad habits for using the Designer:
- Only use the Designer to set up UserControl subclasses of no more than 10 controls (both Controls and other subclassed UserControls). If your UserControl/Form subclass has more than 10 designer-fields, it’s too big.
- All ToolStripItem/MenuItem classes should always be created dynamically (or at least in the main class file), NOT through the Designer. Remember, you can build a ‘tree’ of ToolStripItems through code (think of Linq2Xml and how the code formally looks like the XML), not having to assign things to fields all over the place. I’ll show an example in a future post.
- All events should be assigned through code, NOT the Designer (you can also avoid tons of event handler methods by just hooking up the events with lambdas that call the method you would have been calling).
- Use subclasses of Control types (ComboBox, ContextMenuStrip) to encapsulate logic. Make them private classes inside of your UserControl if possible- the goal is to break up the logic out of a ton of methods on the UserControl. If you have more than 4 control-specific methods, look at creating a subclass.
- Rarely if ever pass a reference to a Parent around- instead, raise events on your children and listen to them from the parent.
- If you can create your UI dynamically, you can get away with private parameterless constructors on UserControls, so you can actually have some data integrity and straightforward object use (no ‘must call Initialize/Bind method to assign data objects’ crap).
- Learn UI programming in a language without a WYSIWYG editor, like python. You quickly learn small UI classes laid out in a dynamic manner are more flexible than monster static UI’s that the VS Designer is often used to create.
I can quickly gauge how passionate and experienced someone is about their craft by looking at how they build a complex UI. If they build designer-driven uber-classes, you can be sure they either haven’t had enough experience building complex UI’s, or haven’t thought enough about what they’re doing to realize how fucked up the code they are writing is.
[…] didn’t require a designer. I’ve ranted previously about what I think visual UI designer tools are doing to our children. I never once felt the need […]