How to make a fancy format

T

Tony Johansson

Hello!

Here is the code that update the listBox but there is a small problem. When
I have different number of item in the foodList will the position for the
column title not be lined up the the data. How do I best solve this lined up
between the column title and the data for the corresponding column ?

public void UpdateGUI()
{
bool flag = false;
lstOutput.Items.Clear();

foreach (Animal animal in animalManager.Animals.ToArray())
{
string foodList = string.Join(",",
animal.FoodMenyList.ToArray());
if (!flag)
{
lstOutput.Items.Add(string.Format("{0}{1,16}{2,29}{3,22}{4,18}{5,35}{6,35}{7,20}{8,20}",
"Animal", "ID", "Name", "Age", "Gender", "Food items",
"Aggressive", "Housing", "Special data"));
lstOutput.Items.Add(string.Empty);
flag = true;
}

lstOutput.Items.Add(string.Format("{0}{1,27}{2,17}{3,20}{4,22}{5,40}{6,21}{7,29}{8,23}",
animal.MyAnimal, animal.ID, animal.Name,
animal.Age,animal.Gender, foodList,
animal.Aggressive,animal.Housing,"No Extra Info"));
}
}

//Tony
 
A

Arne Vajhøj

Here is the code that update the listBox but there is a small problem. When
I have different number of item in the foodList will the position for the
column title not be lined up the the data. How do I best solve this lined up
between the column title and the data for the corresponding column ?

public void UpdateGUI()
{
bool flag = false;
lstOutput.Items.Clear();

foreach (Animal animal in animalManager.Animals.ToArray())
{
string foodList = string.Join(",",
animal.FoodMenyList.ToArray());
if (!flag)
{
lstOutput.Items.Add(string.Format("{0}{1,16}{2,29}{3,22}{4,18}{5,35}{6,35}{7,20}{8,20}",
"Animal", "ID", "Name", "Age", "Gender", "Food items",
"Aggressive", "Housing", "Special data"));
lstOutput.Items.Add(string.Empty);
flag = true;
}

lstOutput.Items.Add(string.Format("{0}{1,27}{2,17}{3,20}{4,22}{5,40}{6,21}{7,29}{8,23}",
animal.MyAnimal, animal.ID, animal.Name,
animal.Age,animal.Gender, foodList,
animal.Aggressive,animal.Housing,"No Extra Info"));
}
}

The pragmatic solution is to make the variable length data the
right most column.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
The pragmatic solution is to make the variable length data the
right most column.

Arne

But in my case almost all data can vary in length which mean that the data
after will not be lined up below the column title.
So is there no easy way to solve this matter.

//Tony

//Tony
 
T

Tony Johansson

Peter Duniho said:
If you are using a fixed-width font for the ListBox, then you can simply
write a format string that is used to create the real format string, and
which takes a single input, the number of characters in the longest
"foodList". For example:

string headerFormat =
string.Format("{{0}}{{1,16}}{{2,29}}{{3,22}}{{4,18}}{{5,{0}}}{{6,35}}{{7,20}}{{8,20}}",
maxFoodListChars);

.then later:

lstOutput.Items.Add(string.Format(headerFormat, "Animal", "ID",
"Name".);

.and the same when adding the actual animal's row.

If you're not using a fixed-width font, then the cause is fairly hopeless
if you insist on using the ListBox control and can't make the variable
length column the last one, as Arne suggests.

But there are other controls that support actual column headers, so why
not use one of those? The ListView control, in Details mode, is probably
the most appropriate. But you could also use a GridView, or even a
TableLayoutPanel. They all have the concept of columns and would ensure
that your columns remain lined up (ListBox actually can do multiple
columns as well, but not in a way that is conducive to this particular
scenario).

Pete

I use fixed-width font but if I want all my data that can vary in length to
be lined up in a nice way below the header I had to use another control then
the ListBox as you suggested. The ListView control that you mentioned would
be a good choice.

//Tony
 
A

Arne Vajhøj

But in my case almost all data can vary in length which mean that the data
after will not be lined up below the column title.
So is there no easy way to solve this matter.

You could use iterate over all rows and calculate the max width for
each column and then format after that.

But I would not recommend that. It could become a very wide screen. 2000
character wide screens are not use friendly.

I would suggest that you either change the format from:
1 aaa bbb ccc
2 ddd eee fff
to:
1:
aaa
bbb
ccc
2:
ddd
eee
fff

or show long values as first N character and then 3 dots and make
it clickable so when people click on it then they get a popup where
all data can be read.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top