Sam
Sam

Reputation: 243

Problem with formatting a string with String.Format in C#

I need to print a string in a message box in specific format for which i am using code similar to as shown below:

string text=""; 
for (int i=0; i<n; i++)
{
   a=..
   b=..
   c=..
   text += String.Format("{0, -8} {1,-4} {2,8}", a, b, c);
}
MessageBox.Show(text);

So for following set of values:

XYZ,ABC,100

X,ABC,100

I get following output:

XYZ     ABC     100

X     ABC     100

So you can see the second line is not well formatted. Probably this is happening because i am printing this in MessageBox. The space a character and a 'space' takes is different. Any solution for this?

Upvotes: 3

Views: 4465

Answers (5)

Bob Black
Bob Black

Reputation: 2405

Try using a \t to insert tabs between values.

Upvotes: 5

Tim Trout
Tim Trout

Reputation: 1072

The MessageBox class, whether from Forms or WPF, is just a wrapper around the win32 message box, so a programmer isn't able to (easily) do things like change the font to a fixed-pitch font so all characters line up nicely with string formatting.

You could, however, make your own clone of MessageBox using a Form and a Label (and whatever buttons you need), then show it using the ShowDialog() method.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942255

This doesn't work because MessageBox uses a proportionally spaced font, the letter M is much wider than the letter l. Just like it is in this message your are reading now. You can only expect alignment like this to work if it is displayed with a fixed-pitch font. Changing the message box font is not appropriate, it is a system setting.

You can get it somewhat better by using tabs:

text += String.Format("{0}\t{1}\t{2}", a, b, c);

but it isn't fool-proof if the field size approaches the tab size. Use a ListView with View = Details instead.

Upvotes: 2

jdehaan
jdehaan

Reputation: 19938

Not sure if it is actually what you mean, but use a monospaced font like "Courier New". If you already did, then sorry for this obvious answer.

Nevermind: it's not possible with the standard MessageBox accoding to this thread. Maybe then an option is to create your own MessageBox class.

Upvotes: 1

decyclone
decyclone

Reputation: 30840

A test created in Windows Application with the following code :

    public void Test1()
    {
        List<List<String>> list = new List<List<string>>() { 
            new List<String>() { "XYZ", "ABC","100" },
            new List<String>() { "X", "ABC", "100"},
        };

        string text = "", a = "", b = "", c = "";
        for (int i = 0; i < list.Count; i++)
        {
            a = list[i][0];
            b = list[i][1];
            c = list[i][2];
            text += String.Format("{0, -8} {1,-4} {2,8}{3}", a, b, c, Environment.NewLine);
        }
        MessageBox.Show(text);
    }

Does what you said, but after checking it with the console application with the following code:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();
            Console.ReadKey();
        }

        public static void Test1()
        {
            List<List<String>> list = new List<List<string>>() { 
                new List<String>() { "XYZ", "ABC","100" },
                new List<String>() { "X", "ABC", "100"},
            };

            string text = "", a = "", b = "", c = "";
            for (int i = 0; i < list.Count; i++)
            {
                a = list[i][0];
                b = list[i][1];
                c = list[i][2];
                text += String.Format("{0, -8} {1,-4} {2,8}{3}", a, b, c, Environment.NewLine);
            }
            Console.WriteLine(text);
        }
    }
}

It does what you expect.

So, what the tests suggest is if is doing what it is supposed to do, but with the lack of same width font in the MessageBox, it does not line up properly. But on the other hand, with the console application using the same width fonts, it does line up exactly.

Upvotes: 0

Related Questions