Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't & show up in a Windows Forms Label? [duplicate]

Tags:

c#

c#-3.0

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. &

like image 438
user1078897 Avatar asked Jan 20 '12 07:01

user1078897


3 Answers

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.

like image 71
Joey Avatar answered Sep 29 '22 22:09

Joey


Use this:

private void Form1_Load(object sender, EventArgs e)
{
    label1.Text = "&&";
}
like image 25
Dhaval Shukla Avatar answered Sep 30 '22 00:09

Dhaval Shukla


& 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 = "&&";
like image 33
Ravi Gadag Avatar answered Sep 29 '22 23:09

Ravi Gadag