ListBox Drag-n-Drop Change Background Color of just one item  
Author Message
FMorse





PostPosted: Windows Forms General, ListBox Drag-n-Drop Change Background Color of just one item Top

Is there a way to change the background color of just one item in a listbox and still allow drag-n-drop In my code I am using a list where there is only one entry that can be TRUE (or activated) and I would like to change it's background color in the listbox. I want to change the item once the user selects another item as the activated one.



Windows Forms26  
 
 
bb35





PostPosted: Windows Forms General, ListBox Drag-n-Drop Change Background Color of just one item Top

You should set the DrawMode for your listBox to OwnerDrawFixed and do your custom drawing inside an event-handler for DrawItem. The DrawItemEventArgs object that's passed to the event-handler should provide everything you need.

 
 
FMorse





PostPosted: Windows Forms General, ListBox Drag-n-Drop Change Background Color of just one item Top

Any VB 2005 example code for this
 
 
bb35





PostPosted: Windows Forms General, ListBox Drag-n-Drop Change Background Color of just one item Top

Here is a example: the following DrawItem event handler that will put a yellow background on the second item. Set the DrawMode to OwnerDrawFixed and do something similar in your DrawItem event-handler. Hope this helps.

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

'For the second item, we'll use a yellow background

If e.Index = 1 Then

e.Graphics.FillRectangle(Brushes.Yellow, e.Bounds)

Else

e.DrawBackground()

End If

' Draw the current item text based on the current Font

e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Black, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

' If the ListBox has focus, draw a focus rectangle around the selected item.

e.DrawFocusRectangle()

End Sub



 
 
FMorse





PostPosted: Windows Forms General, ListBox Drag-n-Drop Change Background Color of just one item Top

I am getting an error on this line

e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Black, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))


 
 
FMorse





PostPosted: Windows Forms General, ListBox Drag-n-Drop Change Background Color of just one item Top

Found my problem. I did a DirectCast(ListBox1.Items(e.Index),String) and it worked. Thanks!