Question ListBox Selectionmode = none question

olsonpm

Well-known member
Joined
Aug 24, 2010
Messages
46
Programming Experience
Beginner
My question is why this mode doesn't seem to be working for me. I've been reading msdn and googling for around an hour and a half. I have a listbox with 'DrawMode' set to 'OwnerDrawFixed', and 'SelectionMode' set to 'none'. In my lbLogList_DrawItem method, I can go as far as to comment out the 'e.drawFocusRectangle' call, and yet it still seems to draw the focus rectangle when selection occurs. There is something i'm not understanding about how the focus rectangle is called - please let me know what i'm missing.

thank you,
Phil
 
Well, I had to write out the question before i could search the right keywords for this one. Anyway, the answer lies in the following variable's state property.

VB.NET:
ByVal e As System.Windows.Forms.DrawItemEventArgs

For anyone who might come across this problem, the fact that the item has focus isn't necessarily drawn in the 'DrawItem' method explicitly (apparently the method e.drawFocusRectangle only draws the border, not the blue highlighted background). In fact, it is drawn implicitly with the 'e.drawBackground' method. The msdn page which explains this is

DrawItemState Enumeration

Basically, the item can have a focused state without being selected. Doesn't make perfect sense to me, but I'm sure there's a good reason.

**How I fixed it
Basically I copied the drawItemEventArgs variable, and changed what I needed to - for this case, a different state and background color. If you copy over the e.state property instead of DrawItemState.Default, you get permanently blue backgrounds each time an item gains focus. If you don't choose your own background color, the same thing happens. I haven't dug too deep to find the cause, i only know the symptoms.

VB.NET:
Dim a As New DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, _
    DrawItemState.Default, e.ForeColor, color.<choose color>)
a.DrawBackground()

hope this helps someone-
Phil
 
Last edited:
Back
Top