How to accurately align text in a text box and save it when exporting to .txt

I have a little silly problem and I was hoping you guys could join in with your experience.

I need a text box with the following (dynamic) data:

TEXT    
TEXT
TEXT
TEXT

_______________________
Name:          Amount:
Blah                  3
Blahblah             10
_______________________

But the text inside the box is a problem. So far I am doing all this text with a StringBuildertitle SB, and in the end I set MyTextBox.Text= SB.ToString();

I set the title inside the field as follows:

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

I generate data inside the loop as follows:

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

But this does not exactly match my example! If the name is larger than the previous one, the amount will be displayed in a different place compared to the previous one.

Fx. eg:

TEXT
TEXT
TEXT
TEXT

__________________
Navn        Antal:
SpecSaver           1
MadEyes       1
Gucci       1
__________________

: ? , . :. .txt :

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

, . MyTextBox.Text.

+3
5

Fixed-Width .

string.Format() ( 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());

:

Title 1             Title 2
Val Col 1 A         Val Col 2 A
Val Col 1 B         Val Col 2 B
+3

( ) . , , , , "" . , :

"Hi"
"Hello there"

, - , 4 .

, , , , (. Eoin).

() . , , , , , , .

+1

- - courier-new , . ​​

. string-padding-problem (, MessageBox ?).

+1

.padleft.padright(int), , .

You can also look into your Curior font, but make it so that each charter occupies the same WM i space as in other fonts, I would take up less space than W

0
source

If these are just numbers that cause spacing problems, spaces can 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'));
0
source

Source: https://habr.com/ru/post/1709474/


All Articles