1

I've implemented the MeasureItem and DrawItem methods in my C# owner-drawn-fixed listbox.

Nothing shows up.

Out of desperation, I added three random strings to the Items collection, and THE FIRST THREE OF MY ITEMS SHOWED UP!

This tells me the listbox didn't know there were items. Do I have to add hundreds of dummy items to the Items collection, just to see MY items?? This is dingbatty; there should be a way to TELL the listbox how many items there are -- I just can't find it!

How do you set the number of items in an owner-drawn listbox?

The code:

private void listVersions_MeasureItem (object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = font.Height + 6;
}

private void listVersions_DrawItem (object sender, DrawItemEventArgs e)
{
    if (entries == null)
        return;

    int i = e.Index;

    if (i < 0 || i >= entries.Count)
        return;

    e.Graphics.FillRectangle (new SolidBrush (BackColor), e.Bounds);
    VersionEntry ent = entries[i];
    Rectangle rect = e.Bounds;
    Graphics g = e.Graphics;

    g.DrawString (i.ToString () + "  " + ent.name, font, Brushes.Black, rect.Location);
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user20493
  • 5,704
  • 7
  • 34
  • 31
  • Can you show your code? – crthompson Nov 21 '13 at 22:18
  • Tack this on to your post.. it would look a lot better. Also, are you perhaps looking for [OnPropertyChanged?](http://stackoverflow.com/questions/12034840/handling-onpropertychanged) – crthompson Nov 21 '13 at 22:23
  • I don't see how OnPropertyChanged would help; nothing changes: I just want to display a list. – user20493 Nov 21 '13 at 22:24
  • "entries" is a list that contains the items to draw. The whole purpose of the owner-drawn listbox is to display entries. And when I add dummy items to the Items collection, the entries items are correctly drawn. IT LOOKS LIKE THE ONLY PROBLEM IS TELLING THE LISTBOX HOW MANY ITEMS THERE ARE. – user20493 Nov 21 '13 at 22:35

2 Answers2

2

Try making the DataSource of your ListBox your entries collection:

listVersions.DataSource = entries
LarsTech
  • 80,625
  • 14
  • 153
  • 225
0

This may help: http://msdn.microsoft.com/en-us/library/windows/desktop/bb761340%28v=vs.85%29.aspx

They (allegedly) have an LB_SETCOUNT message that can be sent to a list box.

Microsoft really should provide a SetCount () method in CListBox, but they often leave out methods you need for non-toy applications. Grrr...

Alan8
  • 1