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

早过忘川 提交于 2019-12-22 11:18:11

问题


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?


回答1:


Try using a \t to insert tabs between values.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/3168180/problem-with-formatting-a-string-with-string-format-in-c-sharp

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