Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

To understand this problem better, please start a VB.NET project and add a listbox (set to OwnerDrawn Fixed) to the form. Then, Copy+Paste the following into the code:

 

   Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
       e.DrawBackground()

       If e.Index < 0 Then
           e.DrawFocusRectangle()
           Exit Sub
       End If

       Dim aBrush As Brush
       Dim aBrushSelection As Brush

       e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit

       aBrushSelection = System.Drawing.Brushes.DarkBlue

       If (e.State And Windows.Forms.DrawItemState.Selected) = Windows.Forms.DrawItemState.Selected Then
           aBrush = System.Drawing.Brushes.LawnGreen

           e.Graphics.FillRectangle(aBrushSelection, e.Bounds)
           e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items.Item(e.Index)), ListBox1.Font, aBrush, e.Bounds.Left, e.Bounds.Top)
           e.Graphics.DrawRectangle(New Pen(Color.DarkRed), e.Bounds)
       Else
           aBrush = System.Drawing.Brushes.Red

           e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items.Item(e.Index)), ListBox1.Font, aBrush, e.Bounds.Left, e.Bounds.Top)
       End If

       e.DrawFocusRectangle()
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim i As Integer

       For i = 0 To 100
           ListBox1.Items.Add("This is item #" & i)
       Next
   End Sub

 

 

After running the application click once on the first item in the listbox and hold down the down arrow on your keyboard. The control will function normally.

 

Now, click once on the last item in the listbox and hold the up arrow on your keyboard. As each item is selected, the previous item does not lose its rectangle.

 

Finally, randomly click on many items and notice the remnants of the rectangles left behind.

 

Why is this happening? :confused:

  • *Experts*
Posted
You need to do a FillRectangle with SystemColors.Window to clear it. Just copy the e.Graphics.FillRectangle line from the "If it's selected" block, and paste it to the other one, changing the color to SystemColors.Window.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...