c# - 如何对齐 TreeView 中的列等文本?

标签 c# string winforms treeview

我有一个 TreeView,我想在其中很好地安排我的数据。

我这样格式化我的字符串:

string name = ItemName;
string current = ("Current: " + info.Current.ToString() + " " + identifier);
string maximum = ("Maximum: " + info.Max.ToString() + " " + identifier);
string minimum = ("Minimum: " + info.Min.ToString() + " " + identifier);

string output = string.Format("{0,-20}{1,-30}{2,-30}{3,-30}{4,-30}", name, current, maximum, minimum, average);
dataNode.Text = output;

当我将 output 写入控制台时,它会按照我想要的方式打印它,就像这样(值已关闭,但这无关紧要):

Load                Current: 9,23 %               Maximum: 33,33 %              Minimum: 6,06 %               Average: 0 %
Temperature         Current: 40 °C                Maximum: 49 °C                Minimum: 38 °C                Average: 0 °C
Clock               Current: 1200 Mhz             Maximum: 2800,09 Mhz          Minimum: 1200 Mhz             Average: 0 Mhz
etc..

但是当我将完全相同的字符串打印到我的 TreeView 时,它显示如下: enter image description here

我遵循了 this 上的示例页面,但这对我不起作用。

我想这与我在 TreeView 中使用的不同节点有关,但我不知道如何正确对齐我的数据。

最佳答案

看来您需要为 TreeView 设置等宽字体。

这里有一个类似的问题,例如等宽字体 (FontFamily.GenericMonospace):

C# .NET multiline TextBox with same-width characters

关于c# - 如何对齐 TreeView 中的列等文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37030758/

相关文章:

java - Python 到 Java 代码 : String. 在 java 中进行编码()

arrays - 使用 R 查找数组中的特定字符串

c# - 使用linq Max()方法时如何获取 key

c# - DataGridView/ComboBox 需要两种显示状态(长和短)的信息

c# - 将 long.MaxValue 转换为 int 和将 float.MaxValue 转换为 int 有什么区别?

android - 将项目放在字符串数组中?

c# - MessageBox.Show ("First Name,{0}", textBox1.Text);

c# - TreeView 文件夹结构有异常

c# - 使用 SendKeys.Send() 模拟右 Shift+Ctrl

c# - .NET 中的 KeyValuePair 和 Hashtable 有什么区别?