1

I have list of string in which I am adding location and its zip code dynamically,

and I am populating that list in text box with scroll bar. But I get space between location and zip code depending on location length,

for example :

   Chicago     60016
   Niles     12332
   San Francisco 95858

What I want is

  Chicago       60016
  Niles         12332
  San Francisco 95858

Here is my code :

var List<string> CityZip = new List<string>();

foreach(var item in CollectionofCityZip)
{
  CityZip.Add(item.City + "    " + item.Zip);

}

Update

I tried CityZip.Add(item.City.PadRight(14) + item.Zip);

It gave me :

   Chicago     60016
   Niles     12332
   San Francisco    95858

But I want

  Chicago       60016
  Niles         12332
  San Francisco 95858
pk_code
  • 2,608
  • 1
  • 29
  • 33
  • 1
    Where do you display that text? If you output tool use a proportional width font this behavior is unavoidable without a lot of code to adjust the empty space length. Use a control that supports columns – Steve Sep 10 '15 at 16:30

3 Answers3

2

If you are using a monospaced font (each letter is the same width) then you can use the built in PadRight function which will pad the string with spaces to be the length you specify:

CityZip.Add(item.City.PadRight(14) + item.Zip);

Note the 14 there is arbitrary. If you want to line it up just right, you'll need to scan your list first and see how long the longest string is and pad to that length. Something like this should work:

const int extraPadding = 1; // how much space to put after the longest entry
int maxLen = CollectionOfCityZip.Select(item => item.City.Length).Concat(new[] {0}).Max();
CityZip.Add(item.City.PadRight(maxLen + extraPadding) + item.Zip);
David
  • 4,665
  • 4
  • 34
  • 60
  • Updated question with trying PadRight output – pk_code Sep 10 '15 at 16:43
  • 1
    `Niles` only has a total width of 10 in your example above. Is that simply because you are trying to show that it didn't line up, or are there really only 10 spaces? If there are actually all 14 but it still didn't line up, then you must not be using a monospaced font. – David Sep 10 '15 at 16:51
  • A quick and dirty fix if you are not using monospaced fonts is to add one or more `\t` characters INSTEAD OF spaces at the end of each city. You will have to determine how many characters equate to the space of 1 tab to determine the number to add. This is not the best solution and will likely not port well between different computers, but you can play with it and see if it suits you in this case. If not, then I hope that the links that @DasKrümelmonster posted may be of some use to you. – David Sep 10 '15 at 16:56
2

If you use WPF, you can specify an item template for the Listbox that could look something like this:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="{Binding Data}"></TextBlock>
            <TextBlock Grid.Column="1" Text="x"></TextBlock >
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

If you are using Winforms, the monospaced font with a variable number of spaces is the simplest solution. Alternatively you can draw the entries yourself.

Relevant MSDN

Relevant SO

Community
  • 1
  • 1
DasKrümelmonster
  • 5,816
  • 1
  • 24
  • 45
0

C# 6.0:

   CityZip.Add($"{item.City,-14}{item.Zip}");

C# < 6.0:

   CityZip.Add(string.Format("{0,-14}{1}", item.City, item.Zip));
Serj-Tm
  • 16,581
  • 4
  • 54
  • 61