Here I am going to show some basics of custom drawing in VB.NET.
This is not production-class code, but rather a head start for those assessing whether or not it’s feasible to custom draw anything (result vs effort).
In this sample project, I will highlight all even items of a ListBox with LightGray color. Here is an extract of how it’s done, if you don’t want to download anything.
|
|
For this to work, you need to have a ListBox
dropped on a form and set its property DrawMode to OwnerDrawFixed
or OwnerDrawVariable
. I used OwnerDrawFixed
for simplicity. The other one allows you to have items of variable height. Several notes on the above code:
- The handler is declared is Shared, which is not common practice, and just to show that it’s not instance dependent. You will probably be using a non-shared handler.
- DrawItemEventArgs class has a property State of type DrawItemState, which tells whether or not the item is currently Selected, Enabled or other. You may want to differentiate the highlight depending on that in production code. Or add some visual anchors to the item, such as custom icons etc. Keep in mind that not all states are used in all controls, so always refer to documentation when in doubt.