Enter "&" symbol into a text Label in Windows Forms?

C#.NetWinformsLabelSpecial Characters

C# Problem Overview


How would one enter special characters into a Label in C# (Windows Forms)?

If you try to write a "&" into a label you'll get a sort of underscore instead..

So what's the C# equivalent of "&"? ("&" obviously doesn't work).

C# Solutions


Solution 1 - C#

Two ways:

  • Escape it with another ampersand (&&).

  • Set UseMnemonic for that label to false. This causes all ampersands within the text to be taken literally so you don't need to double any of them. You'll lose the underlining and access key features though.

    You can set the value either in the designer, or in code:

     myLabel.UseMnemonic = false;
     myLabel.Text = "Text&Text";
    

Solution 2 - C#

Add another ampersand in front of it, so: &&

Or have a look at the following: Label.UseMnemonic (MSDN documentation)

Solution 3 - C#

You can escape & by adding it two times, to try &&.

Solution 4 - C#

I don't know how to use '&' in the designer, but in the code you can use '&&' for showing one '&'

Solution 5 - C#

Try this:

((char)0x26).ToString()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpectraljumpView Question on Stackoverflow
Solution 1 - C#BoltClockView Answer on Stackoverflow
Solution 2 - C#JensView Answer on Stackoverflow
Solution 3 - C#Øyvind BråthenView Answer on Stackoverflow
Solution 4 - C#bastiView Answer on Stackoverflow
Solution 5 - C#Adel El BiariView Answer on Stackoverflow