Un-Highlighting ComboBox Items

Tyecom

Well-known member
Joined
Aug 8, 2007
Messages
78
Programming Experience
Beginner
I have a ComboBox that items are highlighted when selected. I there a way to turned the highlight feature off?

Is it ComboBox.SectionLenght???
 
Please post in most appropriate forum, this is a Forms question rather than a general VB.Net one. Thread moved to Windows Forms forum.

You can set DrawMode and handle DrawItem event to custom paint the items, including selection style.
Here's some sample code for the event handler that draws a red rectangle around the selected item instead of the default blue backcolor:
VB.NET:
Dim combo As ComboBox = CType(sender, ComboBox)
Dim r As Rectangle = e.Bounds

'backcolor
Using back As New SolidBrush(combo.BackColor)
    e.Graphics.FillRectangle(back, r)
End Using

'text
r.Inflate(-1, -1)
Using fore As New SolidBrush(combo.ForeColor)
    e.Graphics.DrawString(combo.GetItemText(combo.Items(e.Index)), e.Font, fore, r)
End Using

'border
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
    e.Graphics.DrawRectangle(Pens.Red, r)
End If
 
Thanks for responding JohnH.
I'm not trying to change the color of the text in the combobox, I am just trying to "not" have the text "selected" (highlighted). Everything I read so far suggest using the "SelectionLength" property of the ComboBox, but I can't get it to work. This is what i have: Combobox1.SelectionLenght = 0. However, the text is still highlighted.
 
Ok, that is more difficult. It depends on when you want this to happen.

When entering the control you can use that code in the GotFocus event, the Enter event didn't work probably because after that is when the internal code selects all.

After selecting something in dropdown is even more difficult, I didn't find any event where setting SelectionLength worked for this, but I found that it was possible to send the Home key in DropDownClosed event, the same thing an accustomed user would do with keyboard:
VB.NET:
SendKeys.Send("{HOME}")
When changing the standard controls UI behaviour like this it usually best to provide the users options where they can choose if they want the default control behaviour they're used to, or your custom behaviour that may work better for the special case.
 
Back
Top