标签属性接受转义序列在C# [英] Label property to accept escape sequences in C#

查看:138
本文介绍了标签属性接受转义序列在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

输入&符号进入Windows窗体中的文本标签

是否有任何属性标签在C#中显示'&'?

Is there any property for a label to display '&' in C#?

我试图在标签中显示客户名称。名称中有&符号时,会显示为_。例如, A& B XXX 被显示为 AB B 下划线。

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 as received by means of setting any property?

推荐答案

标签控件无法显示由转义序列编码的控制字符,并且没有控件的属性。原因是它使用内置于框架中的标准图形例程绘制其文本,这不会扩展控制字符。更不用说,像 \t 这样的含义是不明确的:应该将多少空格视为标签?

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"






编辑回复编辑的问题:

默认情况下,&符号(& )字符表示标签的密钥助记符。这被显示为带下划线的字母,对于键盘访问是有用的。无论何时用户按下特定控件的助记符键和 Alt 键,该控件将接收输入焦点。 (当然,在不能接收焦点的标签的情况下,标签顺序中的下一个控件会接收焦点,这对于本身不支持键盘助记符的文本框或其他控件之前的标签很有用。)

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.)

这当然是好的,但有时你不需要分配给特定控件的助记符序列,或者在你的情况下,你需要&符号以显示而不仅仅是作为一个助记符指示器使用。 您可以通过设置轻松实现此目的 UseMnemonic 属性标签控件为False。当此属性为false ,和号字符不会被解释为访问密钥前缀字符,而是作为文字字符。

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.

所以,如下所示:

myLabel.Text = "Milk && &Cookies"

将产生:牛奶和饼干(C中的饼干下划线)与 UseMnemonic == true

would produce: "Milk & Cookies" (with the C in "Cookies" underlined) with UseMnemonic == true.

这篇关于标签属性接受转义序列在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆