Two questions on DrawItem event

D

Dom

Just two questions that I'd like answered to complete my mental
picture.

1. Why do I need to set the DrawMode property in order to get the
DrawItem event raised. In other events, I just name the event-handler
and then write it, and the event is raised. (In VB, I just had to
write it, which still seems better). But for this one event, I have
to flag the fact that I want it raised. Any reason?

2. If I add the first items to a listbox in the form's Load event, I
get a call to DrawItem for each addition, and I can use e.Index to see
if it is the first, second, or third item. But if I add the first
items elsewhere, eg, in response to a button click, I first get a
series of calls to DrawItem in which e.Index = -1. Any reason?

Dom
 
M

Morten Wennevik [C# MVP]

Just two questions that I'd like answered to complete my mental
picture.

1. Why do I need to set the DrawMode property in order to get the
DrawItem event raised. In other events, I just name the event-handler
and then write it, and the event is raised. (In VB, I just had to
write it, which still seems better). But for this one event, I have
to flag the fact that I want it raised. Any reason?

Setting DrawMode tells the Control not to draw itself, which would lead to blank lines unless you handle the DrawItem event. Not sure how it ishandled in VB, but VB does a lot of stuff behind the scenes that isn't being done in C#, which in my opinion is very good.
2. If I add the first items to a listbox in the form's Load event, I
get a call to DrawItem for each addition, and I can use e.Index to see
if it is the first, second, or third item. But if I add the first
items elsewhere, eg, in response to a button click, I first get a
series of calls to DrawItem in which e.Index = -1. Any reason?

Dom

My guess is, since the Control hasn't drawn its controls yet, there are fewer events necessary when you change the Control's datasource in the Load event. Whenever you do it later on, everything needs to be redrawn.As for the -1, i'm not sure.

e.Index = -1 will be passed if the Control hasn't got a selection, which is handy if you want to write custom text like "Please select a value".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top