Where to call MyBase.OnDrawItem(e)

  • Thread starter Thread starter active
  • Start date Start date
A

active

In
Protected Overrides Sub OnDrawItem

should I call

MyBase.OnDrawItem(e)

at the top or bottom of the sub?

What might happen if I don't call it?



Thanks



I assume any answer to the above applies to

Protected Overrides Sub OnMeasureItem

right?
 
The CheckedListBox overrides this method from its base class (ListBox) and
does not call MyBase.OnDrawItem. I would expect that you will be okay if you
do the same.

Tony
 
Tony,
I'm not being argumentive, really just curious, how can you know what you
said?
I read the doc and it sound like it raises the DrawItem event instead of
calling OnDrawItem which would cause (I think) the base class to raise the
event.
The doc sounds like that but it doesn't really say that so I think you get
your info some other way.

Second question:
Is there some rule as to when to call the base "On" sub - at the your or
bottom of your overriding sub?


thanks a lot
 
1. Using Lutz Roeder's .NET Reflector, you can look at the source code of
the Base Class Libraries of the .NET Framework.
http://www.aisto.com/roeder/dotnet/
2. I don't know of any, but with the tool above, you can look at the base
method and see what would happen if you didn't.

Tony
 
active,

It depends.

First, which control are you referring to? Checking in Reflector
(http://www.aisto.com/roeder/dotnet/), this could include the ListBox,
CheckedListBox, ComboBox, ListView, MenuItem, StatusBar or TabControl, all
in the System.Windows.Forms namespace.

I'll pick the ListBox. By overriding the OnDrawItem method (event), you are
saying that you want to handle the functionality contained in the base
method to draw the ListItem yourself. If you want to add to the base
functionality, you can call MyBase.OnDrawItem first. The item is then drawn
per the base code, and then you can 'add' on top of it. If you call it at
the end, you will have overwritten the drawing you may have already
performed. If you don't call it at all, then it is completely up to you to
provide the mechanics to 'draw' the item.

I would recommend building a simple WinForms app, just a throwaway in 2005,
and testing it both ways to get a clearer picture of what I described above.

The key to calling the base method in an override is to understand what
functionality occurs in the base method, whether you want that
functionality, and therefore whether that functionality should occur before
or after the user logic in your derived method.

Hope this helps,


Steve
 
Make it very clear - thanks


PlatinumBay said:
active,

It depends.

First, which control are you referring to? Checking in Reflector
(http://www.aisto.com/roeder/dotnet/), this could include the ListBox,
CheckedListBox, ComboBox, ListView, MenuItem, StatusBar or TabControl, all
in the System.Windows.Forms namespace.

I'll pick the ListBox. By overriding the OnDrawItem method (event), you
are saying that you want to handle the functionality contained in the base
method to draw the ListItem yourself. If you want to add to the base
functionality, you can call MyBase.OnDrawItem first. The item is then
drawn per the base code, and then you can 'add' on top of it. If you call
it at the end, you will have overwritten the drawing you may have already
performed. If you don't call it at all, then it is completely up to you
to provide the mechanics to 'draw' the item.

I would recommend building a simple WinForms app, just a throwaway in
2005, and testing it both ways to get a clearer picture of what I
described above.

The key to calling the base method in an override is to understand what
functionality occurs in the base method, whether you want that
functionality, and therefore whether that functionality should occur
before or after the user logic in your derived method.

Hope this helps,


Steve
 
Back
Top