Label property to accept escape sequences in C# [duplicate]
Possible Duplicate:
Enter “&” symbol into a text Label in Windows Forms?
Is there any property for a label to display '&' in C#?
I'm trying to display customer names in a label. When there is any '&' symbol in the name, it's getting displayed as '_'. For example, A&B XXX
is getting displayed as AB
with B
underlined.
Instead of hardcoding, is there any way to display &
symbols a开发者_Python百科s received by means of setting any property?
The Label
control can't display the control characters encoded by escape sequences, and there's no property that controls that. The reason is it draws its text using standard graphics routines built into the framework, which do not expand control characters. Not to mention that the meaning of something like \t
is ambiguous: how many spaces should be treated as a tab?
If you want to display something like a tab or skip to a new line, you'll need to hardcode it into your string:
myTabLabel.Text = "This is a spaced out label!";
myNewLineLabel.Text = "First line" + Environment.NewLine + "Second line"
EDIT in response to edited question:
By default, the ampersand (&
) character indicates the key mnemonic for a label. This is shown as an underlined letter and useful for keyboard access. Whenever the user presses the key that is specified as the mnemonic for a particular control along with the Alt key, that control receives the input focus. (Of course, in the case of a label, which can't receive focus, the next control in the tab order receives the focus, which is useful for a label preceding a textbox or other control that does not itself support keyboard mnemonics.)
That's all fine and good, of course, but sometimes you don't need a mnemonic sequence assigned to a particular control, or as in your case, you want the ampersand to be displayed rather than consumed as a mere mnemonic indicator. You can easily achieve this by setting the UseMnemonic
property of the Label
control to "False." When this property is false, the ampersand character is not interpreted as an access key prefix character, but instead as a literal character.
Alternatively, if you don't want to set this property for whatever reason or you want to continue using a different character as the keyboard mnemonic, you can insert two ampersand characters next to each other in your label caption. This will essentially "escape" the ampersand character so that it is displayed as a literal, but use the character following the other ampersand as the access key prefix character.
So, something like:
myLabel.Text = "Milk && &Cookies"
would produce: "Milk & Cookies" (with the C in "Cookies" underlined) with UseMnemonic == true
.
The property you are looking for is UseMnemonic
. If you set it to false
, you will get expected behavior.
Judging from the way you wrote your question, the answer is no. Full stop. No property in the System.Windows.Forms.Label
class is related to escape sequences.
But I didn't fully understand your question. Do you need to display escape sequences in label? For example, separate items by \n. You can double-escape the value you put into the Text
property of your label, in that case, or, smoother, preceed the string quotes with @
. For example
myLabel.Text=@"Separate items with \n";
I hope I have been of help.
Use two &'s, when the UseMnemonic property is true:
Hello && World!
-> Hello & World!
If, however, UseMnemonic is false, then:
Hello && World!
-> Hello && World!
The choice is yours :-)
Note: this behavior has nothing to do with C# string escaping. It is just how the control renders the supplied string and applies a specified mnemonic.
精彩评论