views:

429

answers:

7

Despite the growing popularity Web Applications, and WPF, there's still a lot of work being done in "old-style" Windows.Forms, especially for in-house ("bespoke") business software systems.

So, what are the best (and most hidden) features and tricks of Windows.Forms, the things that can help us turn out useful applications faster?

(I was somewhat surprised to see that this question hadn't already been asked!)

As always for these kinds of questions ...

  • One tip per answer, to allow the best to be voted to the top
  • Don't repost an answer that's already here; vote it up instead
  • Stay on topic - tips and tricks for Windows.Forms

Related questions

(Though, none of these touch on Windows.Forms much, if at all.)

+3  A: 

Well, I don't know about faster (this is a complex topic) - but one of the more hidden features is that winform (and IIRC WPF) data-binding supports custom property-models, akin to some of the things you can do with dynamic - but going way back. To do this, either implement ICustomTypeDescriptor / ITypedList, or create a TypeDescriptionProvider. And of course write your own PropertyDescriptor models...

For example:

Marc Gravell
+9  A: 

Instead of the normal Panel, consider using TableLayoutPanel - it provides much the same functionality as the WPF Grid layout, but for WinForms.

Great for creating dialogs and other windows that need to be resizable:

Sample Resizable Dialog

Why do I like it?

  • Saves fussing around with nested panels and anchors to get the resizing behaviour I want
  • Automatic layout with a consistent amount of spacing between controls

What don't I like?

  • Can be slow to resize dynamically as the user resizes the form
Bevan
+1 graphical example
Anonymous Type
+4  A: 

I don't know if this counts as hidden (just badly documented!), but Windows Forms provides very good support for the design-time environment. Examples include:

  • It's amazingly easy to create custom editors for your control's properties, or to add commands to be shown at design time (such as an Add Column command for a data grid).
  • Customise the designer generated code for your control using CodeDom and the DesignerSerializerAttribute. Rarely needed, but a critical hook when it is!
  • Extender providers allow controls such as the ErrorProvider to add properties to other controls (similar to WPF attached properties).

Lots of stuff like this (Marc's note about property extensibility ties into this as well). Admittedly, this doesn't directly help you develop applications faster -- but if you're building reusable controls or components, it certainly helps the users of those controls get going faster and work more easily.

itowlson
A: 

DataGridView in virtual mode, may make quite a big difference when dealing with large amounts of data.

Sorin Comanescu
+1  A: 

BufferedGraphics is definitely a hidden gem when you want to have more control over double buffering or want to have transparency work correctly on an offscreen buffer with some P/Invoked GDI calls.

Eric
+3  A: 

System.Windows.Forms.ControlPaint seems to be little known, yet it can be very useful for painting common Windows controls.

Sorin Comanescu
+2  A: 

The SystemInformation class is full of useful metrics.

To illustrate, VerticalScrollBarWidth is useful when you're autosizing columns in a ListView, so that you allow the right amount of space for the vertical scrollbar without bringing a horizontal scrollbar into view.

Also, UserName and UserDomainName are also useful for identifying the current user.

Bevan