2

I want to display && in the text of a button in a Windows Form application, but the displayed button displays only the text &, with a single character.

// this is how the button is declared and formatted
Button si = new Button();
si.Width = 25;
si.Height = 25;
this.Controls.Add(si);
si.bringToFront();
si.Text = "&&";
si.Font = new Font("Arial", 4, FontStyle.Regular) // as you can see, I made the font so small, in order to make sure the text fits the size of the button
si.Top = 10;
si.Left = 10;

When I make a MessageBox, which tells what text is contained in the button.Text, it displays && (the desired result). However, the button found on the Windows Form contains only one character: &.

Some of the methods I have already tried are shown below:

// 1
si.Text = "&" + "&"; 

// 2
char c = (char)38;
si.Text = c.ToString() + c.ToString();

// 3
si.Text = "&" + (char)38;

I have already tried to make the size of the button bigger, but it still did not work, the font is not the problem, in this case, as, for sure, the text fits the button size. I think it is worth to mention I have tried to give the text of the button values like &x (si.Text = "&x") and the only character to be found in the text of the button was x.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • 1
    See also [How can I show an “&” (ampersand) in button or label text?](https://stackoverflow.com/q/7601617/402022). – Theraot Jun 02 '19 at 07:42

2 Answers2

10

You can solve this by setting the button's UseMnemonic property to false.

The UseMnemonic property gets or sets a value indicating whether the first character that is preceded by an ampersand (&) is used as the mnemonic key of the control. So setting it to false will allow you to use the & character without escaping the character that follows it.

For further reading.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • I would love to see more detail as to what this property means within your answer. – Enigmativity Jun 02 '19 at 07:46
  • I upvoted but yes I am not aware of what is purpose of `UseMnemonic` property. why it changed `&` to `&&` – Prasad Telkikar Jun 02 '19 at 07:52
  • @Enigmativity Thanks for the downvote for a simple, clear answer that solved the OP's issue. – Koby Douek Jun 02 '19 at 07:54
  • 2
    @KobyDouek - I agree that it is simple, but it's not clear. I'm just using the voting system in the way that it is designed to be used. I feel that the answer, as it stands now, is not a good answer. – Enigmativity Jun 02 '19 at 08:23
  • @Enigmativity Fair enough I guess, though there isn't much to elaborate, except the explenation I just edited from MSDN. – Koby Douek Jun 02 '19 at 12:10
7

By default UseMnemonic is enabled, this allows you to set a key to work as shortcut. The key is set to whatever character follows the "&" in the text (the user would be able press Alt+Key to interact the control).

What happens is that one "&" escapes the other "&", so it is not taken as a shortcut key (Mnemonic key).

If you do not want to disable them (For example you still want to set a shortcut), you can have it display two "&", by setting the text to "&&&&".

See:

Escape ampersand demo

Theraot
  • 31,890
  • 5
  • 57
  • 86