Q:

winforms autocomplete format strings

cities = result.Results.Entities.Cast<Address>().ToList();
foreach (Address address in cities)
{
    int spaces = (30 - address.City.Length);
     string s1 = address.City.Trim();
    for (int i = 0; i <= spaces; i++)
    {
        s1 += " ";
    }
    s1 += address.PostalCode;
    customCollection.Add(s1);
}

I want to concatenate two strings so that they are arranged in columns in a textbox autocompletecustomsource.
No matter what I do I can't get the city and the postal code to align in columns.
I want:

New Bedlam    101010
New York      102010

I get:

New Bedlam      101010
New York      102010

Tried a bunch of things, string.format, padright, nothing works.

A:

Just use string.Format with a left-justifying width specifier:
foreach (Address address in cities)
{
    string s1 = string.Format("{0,-30}{1}",address.City,address.PostalCode);
    customCollection.Add(s1);
}

Be aware that if your text box uses a variable-width font (like Arial) then the zip codes WON'T line up.  Use a fixed-width font like Consolas or Courier New to align properly.

