如何整齐地排列文本的文本框,并保持其导出为.txt时候 [英] How to neatly align text in a textbox and keep it when exporting to .txt

查看:189
本文介绍了如何整齐地排列文本的文本框,并保持其导出为.txt时候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的愚蠢的问题,我希望你们能以你的经验加入

I am having a small stupid issue and I was hoping you guys could join in with your experience.

我需要以下(动态)的数据文本框:

I need a textbox with the following (dynamic) data:

TEXT    
TEXT
TEXT
TEXT

_______________________
Name:   	   Amount:
Blah                  3
Blahblah             10
_______________________

但箱内文本的问题。
到目前为止,我将一切本文以的StringBuilder 名为 SB 和我设置结束 MyTextBox.Text = SB.ToString();

But the text inside the box is the issue. So far I am making all this text with a StringBuilder called SB and in the end I set MyTextBox.Text = SB.ToString();

我设置了头这样的盒子里面:

I set the header inside the box like this:

SB.AppendLine("Name\t\Amount:");



我产生这样的循环中的数据:

I generate the data inside a loop like this:

SB.AppendLine(name + "\t\t" + amount);



不过,这并不整齐排列像我的榜样!
如果名称比以前的名称越长,金额将出现在该行的整体不同的地方 - 相比之前的

But it doesn't align neatly like my example! If the name is longer than the previous name, the amount will be appear a whole different place on the line - compared to the previous.

Fx的。像这样的:

TEXT
TEXT
TEXT
TEXT

__________________
Navn    	Antal:
SpecSaver   	    1
MadEyes 	  1
Gucci   	1
__________________

我的第一个问题:我怎样才能避免这种情况?你通常当你需要一个多行文本框以包含大量的数据做。
我的第二个问题::当我将它导出到一个.txt文件是这样的:

My first problem: How can I prevent this? What do you usually do when you need a multiline textbox to contain a lot of data. My second problem: When I export it to a .txt document like this:

using(var text = new StreamWriter("textfile.txt", false))
{
    text.Write(SB);
}



它给尚未数据,完全错位的另一个版本。它应该等同于MyTextBox.Text。

It gives yet another version of the data, completely misaligned. It should be identical to the MyTextBox.Text.

推荐答案

首先,您需要选择一个的 固定宽度 字体为文本框

First off you'll need to pick a Fixed-Width font for your text box.

接下来,你可以提供一个填充量的String.format()
(或 StringBuilder.AppendFormat

Next, you can provide a Padding amount to string.Format() (or to StringBuilder.AppendFormat)

        StringBuilder sb = new StringBuilder();
        sb.AppendLine(string.Format("{0, -20}{1, -10}", "Title 1", "Title 2"));
        sb.AppendLine(string.Format("{0, -20}{1, -10}", "Val Col 1 A", "Val Col 2 A"));
        sb.AppendLine(string.Format("{0, -20}{1, -10}", "Val Col 1 B", "Val Col 2 B"));

        Console.WriteLine(sb.ToString());



结果:

Results in:

Title 1             Title 2
Val Col 1 A         Val Col 2 A
Val Col 1 B         Val Col 2 B

这篇关于如何整齐地排列文本的文本框,并保持其导出为.txt时候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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