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

独自空忆成欢 提交于 2019-12-05 22:32:53
Bob Black

Try using a \t to insert tabs between values.

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.

jdehaan

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.

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.

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.

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