How to use an "&" character in a C# button text property to display properly
My C# code is:
Button button;
button = new Button();
button.Text = "B&C";
this.Controls.Add(button);
But when I run this code the button on my form displays BC (and the C is usually underlined).
I know that I can set it to B&&C to get my button text to come out as B&C but the users of my app will not know to type B&&C when they configure the text on their buttons.
They will just type B&C, or C&B, etc, which will come out as BC and CB.
开发者_如何学CHow can I work around this?
Thanks.
Simply replace the "&" before setting the button text:
button.Text = text.Replace("&","&&");
You can use simply;
button.Text = "B && C";
精彩评论