I have a ListBox in WinForms that displays some data, and when you click on an item, it expands and displays more. This is the code:
private void historyList_SelectedIndexChanged(object sender, EventArgs e)
{
historyList.Refresh();
}
private void historyList_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
FeedHistoryItem item = (sender as ListBox).Items[e.Index] as FeedHistoryItem;
if (e.Index == historyList.SelectedIndex)
{
e.Graphics.DrawString(item.ExpandedText,
e.Font, Brushes.Black, e.Bounds);
}
else
{
e.Graphics.DrawString(item.CollapsedText,
e.Font, Brushes.Black, e.Bounds);
}
e.DrawFocusRectangle();
}
private void historyList_MeasureItem(object sender, MeasureItemEventArgs e)
{
FeedHistoryItem item = (sender as ListBox).Items[e.Index] as FeedHistoryItem;
string itemText = e.Index == historyList.SelectedIndex ? item.ExpandedText : item.CollapsedText;
int size = 15 * (itemText.Count(s => s == '\r') + 1);
e.ItemHeight = size;
}
The events are called as expected. When you click on an item, it calls Refresh(), then measure, then draw. The text expands. However, the size doesn't change.
I've verified that the first time an item is drawn, it respects when I put in ItemHeight, but when resizing, it does not - if I set a breakpoint in the DrawItem method, even though MeasureItem was just called, the height in e.Bounds does not update. Yes - I do have historyList.DrawMode = DrawMode.OwnerDrawVariable; set.
Any ideas?