1

I have code like so (simplifying for the question):

public const string THIS_AND_THAT = "This & That";

System.Windows.Forms.GroupBox myGroupBox;
System.Windows.Forms.TreeView myTreeView;
System.Windows.Forms.TreeNode myTreeNode;

myGroupBox.Text = THIS_AND_THAT;

myTreeNode = new TreeNode(THIS_AND_THAT);
myTreeNode.Name = THIS_AND_THAT;
myTreeView.Nodes.Add(THIS_AND_THAT);

With this code, the GroupBox displays as "This That" and the TreeView displays correctly as "This & That".

So I changed the string (as suggested here) to this:

public const string THIS_AND_THAT = "This && That";

In this case, the GroupBox displays correctly as "This & That", but the TreeView displays as "This && That". I don't see that I can use UseMnemonic Property on the GroupBox or TreeView.

What do I do?

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • 2
    You can't use the same strings verbatim for TreeView and GroupBox, you will need to provide an escaped version for some controls and an unescaped version for other controls. – Dai Mar 12 '18 at 17:06
  • The is correct so you can make this into an answer. – Al Lelopath Mar 29 '18 at 14:04
  • Avoid `public const String Foo` only expose `const` values as `internal` because of how the compiler works. For publically exposed values you should use `public static String Foo { get; } = "Value";` instead, or if you really need a field use `public static readonly String`. – Dai Mar 29 '18 at 15:52

1 Answers1

1

(Promoting my comment to an answer):

There is inconsistency in how different controls in Windows and WinForms handle strings displayed to the user, as you've discovered TreeView does not support escaped ampersands while GroupBox does.

Fortunately it's straightforward to provide appropriate text:

String text = "This && that";

myGroupBox.Text = text;
myTreeNode.Text = text.Replace("&&", "&");
Dai
  • 141,631
  • 28
  • 261
  • 374