WinForms: Labels and ampersands

Today I bumped into this problem at work. I had a label on a form and I had to display an employee’s department.

Quite easy, but the department sometimes contains an ampersand. And that ampersand was not displayed. I went looking for this problem and I bumped into this property (in System.Windows.Forms.Label): Label.UseMnemonic Property

Setting this property true means: an ampersand that precedents a character underlines that character, so that it can be used in conjunction with the alt key.

The problem arises when you for example pull a department from the database with an ampersand (for example: shipping & hauling). Putting this into the label causes the & not to be displayed since the label assumes that the ampersand means ‘underline the character after the ampersand’.

To display the ampersand you’ve got two choices:

  1. set UseMnemonic to false so that it doesn’t parse the ampersand.
  2. if you need the mnemonic: replace the & with &&, that displays 1 ampersand in the label.