views:

703

answers:

4

Hi, 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  A: 

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.

ChrisF
You can do this is one step with string.format or stringbuilder.appendformat, by specifying a padding amount with the {0} format identifier
Eoin Campbell
I realised that after I'd posted, but wanted to point out the steps the OP needed to think about.
ChrisF
A: 

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?).

gimel
+1  A: 

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

NExt, you can't 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());

outputs:

Title 1             Title 2
Val Col 1 A         Val Col 2 A
Val Col 1 B         Val Col 2 B
Eoin Campbell
I think you meant, "can provide".
hmemcpy
A: 

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

Crash893