How to neatly align text in a textbox and keep it when exporting to .txt

纵然是瞬间 提交于 2019-12-14 01:32:33

问题


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
_______________________

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. like this:

TEXT
TEXT
TEXT
TEXT

__________________
Navn        Antal:
SpecSaver           1
MadEyes       1
Gucci       1
__________________

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);
}

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


回答1:


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

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



回答2:


Character alignment is achieved with fixed-pitch fonts - select something like courier-new as the font for both textbox and printing.

See string-padding-problem (Maybe your MessageBox is showing variable-pitch font?).




回答3:


To (incompletely) answer your first question. Rather than using the same number of tabs for each string you need to count the number of characters in the string as it might contain one, two or more "tabs' worth" of extra characters. For example, consider the following two strings:

"Hi"
"Hello there"

If you added the same number of tabs to each of these the second would still be longer, assuming a tab of 4 spaces.

You can do this manually before you start adding tabs and then only add enough tabs to take the length to the desired amount, or you can us the format statement (see the Eoin's answers).

To (perhaps) answer your second question. Having counted the length of the initial text you could add the required number of spaces rather than tabs and then, assuming you've got a non-proportional font, the numbers will always line up.




回答4:


you could also lookinto the .padleft .padright(int) methods that add spaces to the left or right to help keep things aligned.

You may also lookinto your font Curior is guly but its made so that each charter in takes up the exact same space W M i stuff like that where in other fonts i would take up less space than a W




回答5:


If it is just numbers, that are causing the spacing issues, figure spaces can right align them correctly.

int bigNumber = 1234;
int smallNumber = 5;

string bigNumberString = bigNumber.ToString();
string smallNumberString = smallNumber.ToString();

// This works only in a TextBox with a monospaced font.
textBox1.Text = string.Format("{0,4}\n{1,4}",
    bigNumberString,
    smallNumberString);

// This works in TextBoxes with any font.
textBox1.Text = string.Format("{0}\n{1}",
    bigNumberString,
    smallNumberString.PadLeft(bigNumberString.Length, '\u2007'));


来源:https://stackoverflow.com/questions/932267/how-to-neatly-align-text-in-a-textbox-and-keep-it-when-exporting-to-txt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!