String.Format()'s alignment behaves different when it comes to MessageBox

I am using String.Format() to format some text information and it works perfectly with "Console.WriteLine()" as documented in msdn.

My code is like this:

        StringBuilder strBuilder = new StringBuilder();
        strBuilder.AppendLine("Summary Information:");
        strBuilder.AppendFormat("{0,-10}{1,-8}{2,-10}{3,-30}{4,-7}\n", "Header1", "Header2", "Header3", "Header4", "Header5");
        strBuilder.AppendFormat("{0,-10}{1,-8}{2,-10}{3,-30}{4,-7}\n\n", "A", "1", "Y", "Long Information", "13");
        string result = strBuilder.ToString();

        Console.WriteLine(result);

The console result is shown below.

But, when I try to show it on a MessageBox, the columns are shifting. Somehow the spaces are swallowed.

 MessageBox.Show(result, "MBox Test");

It is also okay, because I wasn't very kneen on the "MessageBox" class of the .Net. It has so much limitations.

So I decided to implement a custom form to display this message. I have tried with Label and RichTextBox. But unfortunately the result was exactly same as the MessageBox.

And I am confused. Why .Net's own controls can not support String.Format()'s alignment. Is there any way I can handle this situation?

The String.Format(...) works perfectly well, but it does not align as a table in your message box because of the font!

In your console you use a fixed-width (monospace) font, but the messagebox uses one with dynamic width. Therefore e.g. an "M" consumes more space than an "i" or a " " (space) and this messes up your layout.

As you can't change the messagebox font, you have to create your own frame with any kind of text widget that supports custom fonts, e.g a TextBox or just an ordinary Label.

Java String format() The java string format() method returns the formatted string by given locale, format and arguments. If you don't specify the locale in String.format() method, it uses default locale by calling Locale.getDefault() method. The format() method of java language is like sprintf() function in c language and printf() method of

The console uses a monospaced font, whereas the message box uses a variable-width font. This is why it appears that the alignment is not kept in the message box.

In the console, a space is the same width as any other character. In the message box, a space is not as wide (and neither are characters like "i" and "l"), so things do not line up how you want them to.

The closest you'll be able to get to keeping the alignment in the message box is to use tabs instead of spaces, but even then things may not line up because of the length of some of the words.

The String.Format method also provides similar functionality. If you are formatting a nonlocalized numeric string, you should use a user-defined numeric format to ensure that you get the look you want.

You can use tabs (\t) instead of spaces to get same distances between columns, it should work perfectly in every font-type :)

The java string format() method returns a formatted string using the given locale, specified format string and arguments.We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Signature: There are two type of string format() method:

In a pinch, using tabs after each column might work but is very hit and miss.

//I'm using Tuple Types and Interpolated Strings which are available in C# 7 and .NET 4.7+
(int, int, int) maxFieldLengths = (10, 10, 10); //Use 10 as the min length
List<(string, string, string)> values = new List<(string, string, string)>();
values.Add(("header1", "header2", "header3"));
values.Add(("value1", "value2", "value3"));
values.Add(("longerstringvalue", "value4", "evenlongerstringvaluehere"));

foreach (var value in values)
{
    maxFieldLengths.Item1 = Math.Max(maxFieldLengths.Item1, value.Item1.Length);
    maxFieldLengths.Item2 = Math.Max(maxFieldLengths.Item2, value.Item2.Length);
    maxFieldLengths.Item3 = Math.Max(maxFieldLengths.Item3, value.Item3.Length);
}

var messageBuilder = new StringBuilder();
foreach (var value in values)
{
    messageBuilder.AppendFormat($"{{0,{maxFieldLengths.Item1 * -1}}}\t| {{1,{maxFieldLengths.Item2 * -1}}}\t| {{2,{maxFieldLengths.Item3 * -1}}}{Environment.NewLine}",
        value.Item1, value.Item2, value.Item3);
}
MessageBox.Show(messageBuilder.ToString());

This produces:

The most common way of formatting a string in java is using String.format(). If there were a “java sprintf” then this would be it. If there were a “java sprintf” then this would be it.

First, we use string.Format to combine 3 strings with formatting options. The format string itself is the first argument. It is usually specified as a string literal.

How does String.Format map the additional information MM/dd/yyyy to a string result? As specified above, it just calls the IFormattable .ToString(string format, IFormatProvider provider). The provider is often something that tells you what the culture is of your system. Often null because people don't pass it String.Format() as you did in your

The .NET composite formatting feature, which is used by some Write and WriteLine methods of the Console and StreamWriter classes, the String.Format method, and the StringBuilder.AppendFormat method. The composite format feature allows you to include the string representation of multiple data items in a single string, to specify field width, and

Comments
  • well after learning this is because of the font type, I've made a messagebox which works with html. so problem solved. and now I have much more flexibility formatting the messagebox.