Wednesday, August 03, 2005

Combo Box Caching

Got tripped up the other day by an elementary piece of behaviour that I expect everybody
knows about except me (and the rest of my team). What do you think you will see
when you drop down the combo box in the following app?

Public Class Form1

Protected Overrides Sub OnLoad(ByVal e As EventArgs)


Dim c As New C("Marco")


c.Value = "Polo"

End Sub

End Class

Public Class C

Public Sub New(ByVal val As String)

Value = val

End Sub

Public Overrides Function
ToString() As String


End Function

Value As String

End Class

Well the title of this post gives it away; of course you see "Marco". The combo box caches the ToString results when the items are added. I have yet to find a way to tell it to update its cached strings, without removing and readding items.

I love the fact that you add objects to combos and other lists, but this behaviour just shows it up as something of a hack: we might as well still be storing strings and putting the object reference in ItemData.

This happens in VS.NET 2003 and 2005. My friends tell me Java has a much more sophisticated model with paging and everything. Come on Microsoft, time to catch up!