By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,640 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,640 IT Pros & Developers. It's quick & easy.

Formating output text

P: n/a
Hi!

I need to write some strings in text file, it consist 3 columns and it
must looks like:

ID Name Points
======================
1 Mike 4,5
2 Joseph 1,2
3 Freddy 12,3

The data is in listview control and i know how to get it from it
but when when I put this records in file i can't make to format
these record. I try to use .PadLeft and .PadRight but all i get is
something like this:

ID Name Points
======================
1 Mike 4,5
2 Joseph 1,2
3 Freddy 12,3

How to make that all columns are alligned?
Apr 3 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Apr 3, 3:53*pm, Joza <josip.pejako...@MAKNIOVOetfos.hrwrote:
Hi!

I need to write some strings in text file, it consist 3 columns and it
must looks like:

ID * * *Name * *Points
======================
1 * * * Mike * * * 4,5
2 * * * Joseph * * 1,2
3 * * * Freddy * *12,3

The data is in listview control and i know how to get it from it
but when when I put this records in file i can't make to format
these record. I try to use .PadLeft and .PadRight but all i get is
something like this:

ID * * *Name * *Points
======================
1 * * * Mike * * * 4,5
2 * * * Joseph * * *1,2
3 * * * Freddy * * *12,3

How to make that all columns are alligned?
There are not alligned in the "corrected" example to begin with.
In any case you will have to do it manually, I know of no provider
that does this formatting (fixed length fields).
It's very easy in fact, all you have to do is pritn the field value,
and pad it with white spaces ( [size of the field] - [size of the
actual value] ). Of course you have to check for overflow.
Apr 3 '08 #2

P: n/a
Ignacio Machin ( .NET/ C# MVP ) wrote:
>
There are not alligned in the "corrected" example to begin with.
In any case you will have to do it manually, I know of no provider
that does this formatting (fixed length fields).
It's very easy in fact, all you have to do is pritn the field value,
and pad it with white spaces ( [size of the field] - [size of the
actual value] ). Of course you have to check for overflow.
I try like this:

string var="";
string txt1 = "ABCD";
string txt2 = "EFGHIJK";
string num1 = "2.4";
string num2 = "6.5";
int myFieldSize = 35;

Console.WriteLine(txt1 + var.PadRight(myFieldSize - num1.Length) + num1);
Console.WriteLine(txt2 + var.PadRight(myFieldSize - num2.Length) + num2);

And the result is something like:

ABCD 2.4
EFGHIJK 6.5

instead of

ABCD 2.4
EFGHIJK 6.5

I just don't get it... :(
Apr 3 '08 #3

P: n/a
Ignacio Machin ( .NET/ C# MVP ) wrote:
>
There are not alligned in the "corrected" example to begin with.
In any case you will have to do it manually, I know of no provider
that does this formatting (fixed length fields).
It's very easy in fact, all you have to do is pritn the field value,
and pad it with white spaces ( [size of the field] - [size of the
actual value] ). Of course you have to check for overflow.
I try like this:

string var="";
string txt1 = "ABCD";
string txt2 = "EFGHIJK";
string num1 = "2.4";
string num2 = "6.5";
int myFieldSize = 35;

Console.WriteLine(txt1 + var.PadRight(myFieldSize - num1.Length) + num1);
Console.WriteLine(txt2 + var.PadRight(myFieldSize - num2.Length) + num2);

And the result is something like:

ABCD 2.4
EFGHIJK 6.5

instead of

ABCD 2.4
EFGHIJK 6.5

I just don't get it... :(
Apr 3 '08 #4

P: n/a

"Joza" <jo*************@MAKNIOVOetfos.hrwrote in message
news:ft**********@garrison.globalnet.hr...
Ignacio Machin ( .NET/ C# MVP ) wrote:
>>
There are not alligned in the "corrected" example to begin with.
In any case you will have to do it manually, I know of no provider
that does this formatting (fixed length fields).
It's very easy in fact, all you have to do is pritn the field value,
and pad it with white spaces ( [size of the field] - [size of the
actual value] ). Of course you have to check for overflow.

I try like this:

string var="";
string txt1 = "ABCD";
string txt2 = "EFGHIJK";
string num1 = "2.4";
string num2 = "6.5";
int myFieldSize = 35;

Console.WriteLine(txt1 + var.PadRight(myFieldSize - num1.Length) + num1);
Console.WriteLine(txt2 + var.PadRight(myFieldSize - num2.Length) + num2);
You need to use the first column when calculating how much padding to add,
i.e.:
Console.WriteLine(txt1 + var.PadRight(myFieldSize - txt1.Length) + num1);
Console.WriteLine(txt2 + var.PadRight(myFieldSize - txt2.Length) + num2);

Also note that this will only align correctly if you view it using a
fixed-width font

/claes

Apr 4 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.