Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
4.83/5 (4 votes)
See more:
Hi there. I need some help with drawing items for a ComboBox Control.

For the most part the ComboBox draws just fine. I can scroll up and down and everything is good. The problem is when I drag the scrollbar "thumb" or box to get through the list faster. The items do not display correctly.

The ComboBox is set to DropDownList and OwnerDrawVariable.

I understand the code below is not very meaningful as I could use the windows implementation to draw the items. The program I'm working on displays certain items in bold, so the code below just illustrates the problem I have.

Thanks for your help!!

Public Class Form1<br /><br />    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br /><br />        Dim i As Integer<br /><br />        For i = 1 To 200<br />            ComboBox1.Items.Add(i)<br />        Next<br /><br />    End Sub<br /><br />    Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem<br /><br />        If e.Index = -1 Then<br />            Exit Sub<br />        End If<br /><br />        e.DrawBackground()<br /><br />        e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString(), e.Font, New SolidBrush(Color.Black), e.Bounds.X, e.Bounds.Y)<br /><br />    End Sub<br /><br />End Class
Posted

Hi,

not sure this will fix it, however it might as your current code is too expensive: for every item drawn you:
- create a new SolidBrush
- and you don't call Dispose on it, which you should as the class offers such method.

It would be better to create a brush once, and keep it handy in a class member so you can reuse it for all items. And actually, you don't even need this as there is a class offering standard brushes, so all you need is Brushes.Black

:)

 
Share this answer
 

Hi All. Thank you for your help. I actually discovered the problem. There are no "problems" with the code I have. While I understand the code could be improved definitely.

I was frustrated with the issue I was having, and tried the executable using Virtual PC with Vista and there was no problem. Discovered that the issue was with Windows 7. I am currently using the RC.

 The same issue occurs with in other programs that use OwnerDrawing for comboboxes.

 

Thanks again.

 
Share this answer
 
Do you really need OwnerDrawVariable ? try OwnerDrawFixed. And if you are using a user control derived from ComboBox, enable Double buffering by Overriding DoubleBuffered property and returning true.
 
Share this answer
 
v2


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900