使用填充格式化文本在 C# 中不对齐 [英] Formatting text with padding does not line up in C#

查看:65
本文介绍了使用填充格式化文本在 C# 中不对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程还很陌生,我刚刚用 C# .NET 编写了一个简单的应用程序来检索有关系统驱动器空间的信息.该程序运行良好,但我正在努力格式化输出.

I am fairly new to programming and I just wrote a simple application in C# .NET to retrieve information about system drive space. The program functions fine but I'm struggling with formatting the output.

查看输出:

我正在尝试使用填充使文本在富文本框中以列格式排列,但输出不会排列,因为如果有多个驱动器,驱动器名称的长度不同扔掉填充物.即使驱动器号返回一个为 M: 另一个为 I: 字母大小的差异足以在填充时摆脱对齐.

I'm trying to use padding to get the text to line up in sort of a column format within a rich text box but the output doesn't line up because if there are multiple drives, the drive names are different lengths which throws off the padding. Even if the drive letter comes back one as M: and the other as I: the difference in the size of the letter is enough to throw off the alignment while padding.

我想知道是否有办法将每个字符串值强制为特定长度,以便均匀地应用填充,或者是否有更好的方法来格式化我的输出.提前感谢您抽出宝贵时间,如果有任何进一步的信息会有所帮助,请告诉我!

I am wondering if there is a way to force each string value to a specific length so the padding is applied evenly or if maybe there's an even better way to format my output. Thank you in advance for your time and let me know if any further information would be helpful!

推荐答案

注意: 其中一条评论提出了一个重要问题,关于该问题是否涉及 System.Windows.Forms.RichTextBox (WinForms)System.Windows.Controls.RichTextBox (WPF) 控件.此答案适用于 RichTextBox 的 WinForms 版本,因此如果您使用的是 WPF,则这不适用.

Note: One of the comments asked an important question, regarding whether the question refers to the System.Windows.Forms.RichTextBox (WinForms) or the System.Windows.Controls.RichTextBox (WPF) control. This answer applies only to the WinForms version of RichTextBox, so if you're using WPF, this doesn't apply.

最重要的事情,这在评论中提到,是你需要使用 等宽字体.

The most important thing, and this was mentioned in the comments, is that you'll need to use a Monospaced font.

既然您声明您使用的是 RichTextBox,您需要知道如何将其设置为使用您选择的任何等宽字体.

Since you stated you're using a RichTextBox, you'll need to know how to set it to use whatever monospaced font you've chosen.

为此,您可以使用 RichTextBox.SelectionFont 属性.

To do that, you can use the RichTextBox.SelectionFont property.

有关更一般的说明,请参阅此 MSDN 文章:为 Windows 窗体 RichTextBox 控件设置字体属性

For more general instructions, refer to this MSDN article: Setting Font Attributes for the Windows Forms RichTextBox Control

一旦您设置了 RichTextBox.SelectionFont 属性,只有添加到控件之后的文本将使用指定的字体.要将字体应用于现有文本(即填充RichTextBox,然后将字体更改为适当的等宽字体),请查看这个答案,它准确地告诉你该怎么做.

Once you set the RichTextBox.SelectionFont property, only text added to the control afterwards will use the specified font. To apply the font to existing text (i.e. you populate the RichTextBox and then change the font to an appropriate monospaced font), take a look at this answer, which tells you precisely what to do.

一旦完成,剩下的就是在每个字符串的末尾添加适当数量的空格,以便下一段数据出现在适当的位置.您可能会使用 String.PadRight,但有关填充字符串的更多一般信息,请查看这篇 MSDN 文章:.NET 框架中的填充字符串

Once that's done, there remains the simple matter of adding the appropriate amount of whitespace to the end of each string, such that the next piece of data appears at the appropriate position. You'll probably be using String.PadRight, but for more general information about padding strings, check out this MSDN article: Padding Strings in the .NET Framework

这篇关于使用填充格式化文本在 C# 中不对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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