6

Consider:

Label label = new Label();

label.Text = "&";

But... I can't see the &, after I run the program.

Code

How do I show that character in a label?

I want to show only the character. &

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1078897
  • 61
  • 1
  • 2

3 Answers3

19

That's because & in labels, buttons and menus is prefixed to access key characters, i.e. those characters you can press with Alt to give focus to a control directly.

label1.UseMnemonic = false;

will get rid of that behaviour.

From the documentation of UseMnemonic:

true if the label doesn't display the ampersand character and underlines the character after the ampersand in its displayed text and treats the underlined character as an access key; otherwise, false if the ampersand character is displayed in the text of the control. The default is true.

If you need to display & and the access key behaviour, then you need to escape the & as &&, as several other answers mentioned.

Joey
  • 344,408
  • 85
  • 689
  • 683
5

Use this:

private void Form1_Load(object sender, EventArgs e)
{
    label1.Text = "&&";
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dhaval Shukla
  • 1,127
  • 2
  • 10
  • 19
5

& is used as a Localizable resource in Windows Forms. And it is also used to specify shortcuts. So you need to escape it:

this.label1.Text = "&&";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83