C# Winforms Label Text 属性不显示 \t 制表符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2154623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Winforms Label Text property not displaying \t tab character
提问by Jon
This should be very simple.
这应该很简单。
I have a Label control on my Form and I am trying to put a tab character between text
我的表单上有一个标签控件,我试图在文本之间放置一个制表符
Label.Text = "Is there a\ttab";
The output is "Is there atab";
输出是“是否有atab”;
What am I doing wrong?
我究竟做错了什么?
采纳答案by Joey
Tab is actually a non-printing character—or rather, a control character. What it does is entirely dependent on the application. What exactly do you expect? 8 spaces? 4 spaces? As many spaces as needed to get to a multiple of 8 columns? Indentation of the following text by one cm?
Tab 实际上是一个非打印字符——或者更确切地说,是一个控制字符。它的作用完全取决于应用程序。你究竟期望什么?8个空格?4个空格?需要多少空间才能达到 8 列的倍数?将以下文本缩进一厘米?
To put it short: The Label
control doesn't support tabs. Actually, Label just uses normal graphics routines for rendering its text and how should theyknow what you intend to do with your tab character?
简而言之:Label
控件不支持选项卡。实际上,Label 只是使用普通的图形例程来呈现其文本,他们如何知道您打算对制表符做什么?
If you need to display that character as a number of spaces, then you should replace it by that number of spaces.
如果您需要将该字符显示为多个空格,则应将其替换为该数量的空格。
回答by Ash
Nothing, windows forms labels are very limited in functionality and don't support the \t character.
没什么,windows 窗体标签的功能非常有限,不支持 \t 字符。
A (slightly awkward) alternative might be:
一个(有点尴尬的)替代方案可能是:
label1.Text = "test\ting\t123".Replace("\t"," ");
回答by SwDevMan81
Right, to insert a tab, just add the spaces desired.
对,要插入制表符,只需添加所需的空格。
If you want to offset the next by a specified length, you could try
如果你想按指定的长度偏移下一个,你可以尝试
int offset_text = 20;
label1.Text = "Is there a".PadRight(offset_text)+"Tab";
label2.Text = "More Text".PadRight(offset_text)+"Too";
回答by curtisk
Just use a literal string and you should be good to go...
只需使用一个文字字符串,你应该很高兴......
label1.Text = @"Test for Tab";
Where that big space is where I actually hit tab three times...hope this helps
那个大空间是我实际上按三下 Tab 的地方...希望这会有所帮助
回答by Contristo
Old thread, but since none of the answers seemed to work for me, I will go ahead and throw in my 2 cents. I could not get a "\t" or even use manual spaces to add spacing to the label. What I ended up doing was using alt code alt-255 5 times. This worked like a charm. Gotta love total hacks...
旧线程,但由于所有答案似乎都不适合我,我将继续投入我的 2 美分。我无法获得“\t”,甚至无法使用手动空格为标签添加间距。我最终做的是使用 alt 代码 alt-255 5 次。这就像一个魅力。必须喜欢完全黑客...
回答by Jess
I wanted to add tabs ("\t"
) to a dropdown list of items. The items have a ToString
method that gives about 3 words concatenated together. They did not line up. For example:
我想将选项卡 ( "\t"
)添加到项目下拉列表中。这些项目有一种ToString
方法可以将大约 3 个单词连接在一起。他们没有排队。例如:
- 1-I 45
- 123-AB 511
- 123456-MMM 611
- 1-I 45
- 123-AB 511
- 123456-MMM 611
A long list like this is hard to read. So I used string.Format
like this:
像这样的长列表很难阅读。所以我string.Format
是这样使用的:
string.Format("{0,6}-{1,-4} {2}",id,name,num);
The number after the comma will right align/pad if positive and left align/pad if negative. Then I changed my font in the Combobox to be monospaced, like Courier New, and you get something like this:
如果是正数,逗号后面的数字将右对齐/填充,如果是负数,则左对齐/填充。然后我将 Combobox 中的字体更改为等宽字体,例如 Courier New,您会得到如下内容:
1-I 45
123-AB 511
123456-MMM 611
That is much easier for a user to read.
这对用户来说更容易阅读。
回答by Pedro Francisco Pereira
Just click in the arrow at the right of the Text property of the label (click in the Text property content and the drop-down-arrow will show up). A box for text-editing will open, and in that box you can use Enter, Tab, and so on.
只需单击标签的 Text 属性右侧的箭头(单击 Text 属性内容,将显示下拉箭头)。将打开一个文本编辑框,您可以在该框中使用 Enter、Tab 等。