random thoughts and discussions on the things that interest me

Overridden method OnMeasureItem is not being invoked

I just ran into an issue when subclassing the ComboBox class whereby the OnMeasureItem method that I was overriding was not being called. I had set the DrawMode to OwnerDrawFixed in the constructor for my ComboBox which had in turn filtered through to the designer (non-default value). When I amended the value in the constructor to OwnerDrawVariable the change did not then filter through to the designer (no surprises there). The result being that my constructor had the following code:

this.DrawMode = DrawMode.OwnerDrawVariable;

However, this was clearly at odds with the designer. So in order to stop this occurring again, and to stop any modification of the DrawMode via the designer, I changed the line in the constructor of my ComboBox to reference the base like so:

base.DrawMode = DrawMode.OwnerDrawVariable;

I then added the DrawMode property with the new keyword to replace the base version (see below). You need to include the property setter as the designer will still try to assign a value to DrawMode.

public new DrawMode DrawMode
    get { return base.DrawMode; }
    set { }

After tidying up the designer code everything works like a dream.