0

What control should I use to make a list of items display? I want to be able to adjust the height of the item so its height can take up more space then another item in the same control list of items.

I looked at listboxes, but you can't adjust the size of the items. I've considered making blank entries for placeholders to be grouped as the same item but would rather not if possible.

What this control is to be used for is to represent chunks of time from the beginning of the day (the top) to the end of the day (the bottom).

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Naate
  • 115
  • 10
  • You can set the height of all rows with a ListView; see http://stackoverflow.com/questions/6563863/c-sharp-change-listview-items-rows-height. For adjusting each item separately you need an owner-drawn ListBox. – TypeIA Mar 02 '14 at 02:24
  • Also consider GridView for this task. – TypeIA Mar 02 '14 at 02:29
  • `represent chunks of time from the beginning of the day (the top) to the end of the day (the bottom).` - you may want to check [My Example](http://stackoverflow.com/a/19049586/643085) of a similar thing using current, relevant .Net Windows UI technologies. – Federico Berasategui Mar 02 '14 at 04:36

2 Answers2

3

I looked at listboxes, but you can't adjust the size of the items

But you can. Set the DrawMode property to OwnerDrawVariable.

You of course need to tell it how tall each item needs to be, that requires implementing the MeasureItem event. And of course you need to draw it, so you fill up the space that you reserved with MeasureItem, that requires implementing the DrawItem event. You'll find an excellent example in the MSDN Library article for MeasureItem.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

A custom panel does it like a charm:

public class AutoPanel : Panel
{
    public AutoPanel()
    {
        AutoScroll = true;
    }
    private int _nextOffset = 0;
    public int ItemMarginX = 5;
    public int ItemMarginY = 5;
    public void Add(Control child)
    {
        child.Location = new Point(ItemMarginX, _nextOffset);
        _nextOffset += (child.Height + ItemMarginY);
        Controls.Add(child);
    }
}

Add it to your form and add items to it like this:

        panel1.Add(new Button { Text = "smaller item", Height = 20 });
        panel1.Add(new Button { Text = "medium item", Height = 23 });
        panel1.Add(new Button { Text = "larger item", Height = 32 });
Bizhan
  • 16,157
  • 9
  • 63
  • 101