使用 C# 在文本中插入制表符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/366124/
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
Inserting a tab character into text using C#
提问by Layla
I'm building an application where I should capture several values and build a text with them: Name
, Age
, etc.
我建立一个应用程序,我应该抓住几个值,并建立一个文本与他们同在Name
,Age
等等。
The output will be a plain text into a TextBox
.
输出将是一个纯文本到TextBox
.
I am trying to make those information appear in kind of columns
, therefore I am trying to separate them with tab
to make it clearer.
我试图让这些信息以某种形式出现columns
,因此我试图将它们分开tab
以使其更清晰。
For example, instead of having:
例如,而不是:
Ann 26
Sarah 29
Paul 45
I would like it to show as:
我希望它显示为:
Ann tab26 Sarah tab29 Paul tab45
安选项卡26 莎拉选项卡29 保罗选项卡45
Any tip on how to insert
the tabs into my text?
关于如何insert
将标签添加到我的文本中的任何提示?
采纳答案by DShook
Try using the \t
character in your strings
尝试使用\t
字符串中的字符
回答by Dan R
Hazar is right with his \t
. Here's the full list of escape characters for C#:
Hazar 是对的\t
。这是 C# 转义字符的完整列表:
\'
for a single quote.
\'
对于单引号。
\"
for a double quote.
\"
对于双引号。
\\
for a backslash.
\\
为反斜杠。
\0
for a null character.
\0
对于空字符。
\a
for an alert character.
\a
对于警告字符。
\b
for a backspace.
\b
退格。
\f
for a form feed.
\f
用于换页。
\n
for a new line.
\n
换一条新线路。
\r
for a carriage return.
\r
回车。
\t
for a horizontal tab.
\t
对于水平选项卡。
\v
for a vertical tab.
\v
对于垂直选项卡。
\uxxxx
for a unicode character hex value (e.g. \u0020
).
\uxxxx
对于 unicode 字符十六进制值(例如\u0020
)。
\x
is the same as \u
, but you don't need leading zeroes (e.g. \x20
).
\x
与 相同\u
,但您不需要前导零(例如\x20
)。
\Uxxxxxxxx
for a unicode character hex value (longer form needed for generating surrogates).
\Uxxxxxxxx
对于 unicode 字符十六进制值(生成代理需要更长的形式)。
回答by david valentine
It can also be useful to use String.Format
, e.g.
使用它也很有用String.Format
,例如
String.Format("{0}\t{1}", FirstName,Count);
回答by Amin Saqi
There are several ways to do it. The simplest is using \t
in your text. However, it's possible that \t
doesn't work in some situations, like PdfReport
nuget package.
有几种方法可以做到。最简单的就是\t
在你的文本中使用。但是,\t
在某些情况下可能不起作用,例如PdfReport
nuget 包。
回答by MafazR
var text = "Ann@26"
var editedText = text.Replace("@", "\t");
回答by CERI
string St = String.Format("{0,-20} {1,5:N1}\r", names[ctr], hours[ctr]);
richTextBox1.Text += St;
This works well, but you must have a mono-spaced font.
这很有效,但您必须使用等宽字体。
回答by Hecatonchires
When using literal strings (start with @") this might be easier
使用文字字符串(以 @" 开头)时,这可能更容易
char tab = '\u0009';
string A = "Apple";
string B = "Bob";
string myStr = String.Format(@"{0}:{1}{2}", A, tab, B);
Would result in Apple:<tab>Bob
会导致 Apple:<tab>Bob
回答by Heersert
In addition to the anwsers above you can use PadLeft or PadRight:
除了上面的答案,您还可以使用 PadLeft 或 PadRight:
string name = "John";
string surname = "Smith";
Console.WriteLine("Name:".PadRight(15)+"Surname:".PadRight(15));
Console.WriteLine( name.PadRight(15) + surname.PadRight(15));
This will fill in the string with spaces to the left or right.
这将用左边或右边的空格填充字符串。
回答by schlebe
Using Microsoft Winform controls
, it is impossible to solve correctly your problem without an little workaround that I will explain below.
使用Microsoft Winform controls
,如果没有我将在下面解释的一些解决方法,就不可能正确解决您的问题。
PROBLEM
问题
The problem in using simply "\t"
or vbTab
is that when more than one TextBox are displayed and that alignment must be respected for all TextBox, the ONLY "\t"
or vbTab
solution will display something that is NOT ALWAYS correctly aligned.
简单地使用"\t"
or的问题vbTab
是,当显示多个文本框并且所有文本框都必须遵守对齐方式时,唯一"\t"
或vbTab
解决方案将显示一些并不总是正确对齐的内容。
Example in VB.Net:
VB.Net 中的示例:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Bernard" + vbTab + "32"
TextBox2.Text = "Luc" + vbTab + "47"
TextBox3.Text = "Fran?ois-Victor" + vbTab + "12"
End Sub
will display
会显示
as you can see, age
value for Fran?ois-Victor
is shifted to the right and is not aligned with age
value of two others TextBox.
如您所见,age
for 的值Fran?ois-Victor
向右移动并且未与age
另外两个 TextBox 的值对齐。
SOLUTION
解决方案
To solve this problem, you must set Tabs position using a specific SendMessage()
user32.dll API function as shown below.
要解决此问题,您必须使用特定的SendMessage()
user32.dll API 函数设置 Tabs 位置,如下所示。
Public Class Form1
Public Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" _
( ByVal hWnd As IntPtr _
, ByVal wMsg As Integer _
, ByVal wParam As Integer _
, ByVal lParam() As Integer _
) As Integer
Private Const EM_SETTABSTOPS As Integer = &HCB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim tabs() As Integer = {4 * 25}
TextBox1.Text = "Bernard" + vbTab + "32"
SendMessage(TextBox1.Handle, EM_SETTABSTOPS, 1, tabs)
TextBox2.Text = "Luc" + vbTab + "47"
SendMessage(TextBox2.Handle, EM_SETTABSTOPS, 1, tabs)
TextBox3.Text = "Fran?ois-Victor" + vbTab + "12"
SendMessage(TextBox3.Handle, EM_SETTABSTOPS, 1, tabs)
End Sub
End Class
and following Form will be displayed
将显示以下表格
You can see that now, all value are correctly aligned :-)
您现在可以看到,所有值都正确对齐了 :-)
REMARKS
评论
Multiline
property of the TextBox must be set to True. If this properties is set to False, the Tab is positioned as before.
Multiline
TextBox 的属性必须设置为 True。如果此属性设置为 False,则 Tab 将像以前一样定位。
How AcceptsTab
property is assigned is not important (I have tested).
如何AcceptsTab
分配财产并不重要(我已经测试过)。
This question has already be treated on StackOverflow
这个问题已经在StackOverflow上处理过了
Caution: the mesure Unit for Tab position is not character but something that seems to be 1/4 of character. That is why I multiply the length by 4.
注意:制表符位置的测量单位不是字符而是似乎是字符的 1/4。这就是我将长度乘以 4 的原因。
C# SOLUTION
C# 解决方案
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, uint[] lParam);
private const int EM_SETTABSTOPS = 0x00CB;
private const char vbTab = '\t';
public Form1()
{
InitializeComponent();
var tabs = new uint[] { 25 * 4 };
textBox1.Text = "Bernard" + vbTab + "32";
SendMessage(textBox1.Handle, EM_SETTABSTOPS, 1, tabs);
textBox2.Text = "Luc" + vbTab + "47";
SendMessage(textBox2.Handle, EM_SETTABSTOPS, 1, tabs);
textBox3.Text = "Fran?ois-Victor" + vbTab + "12";
SendMessage(textBox3.Handle, EM_SETTABSTOPS, 1, tabs);
}
}
}