Listview items colors

The place for threads about TimoSoft ExplorerListView.
Post Reply
engee30
Lt. Commander
Posts: 54
Joined: 25 Sep 2012, 19:49
Location: Swindon, UK
Contact:

Listview items colors

Post by engee30 »

Hi TiKu.
I'm still familiarizing myself with the code of your Listview control. This time, I'd like to change the backcolor of some Listview items. I've looked into the code of your Customdraw Listview example, but couldn't find any method that would be responsible for changing the backcolor of an item in a row, and in a column. Could you direct me into a solution to achieve that? I'd much appreciate it.
Cheers,
Pete
User avatar
TiKu
Administrator
Administrator
Posts: 832
Joined: 28 Sep 2004, 21:10
Location: München
Contact:

Re: Listview items colors

Post by TiKu »

It's simple. You've probably seen in the Fonts & Color sample that it sets the TextBackColor. Well, replace TextBackColor with TextColor. TextColor and TextBackColor are ByRef parameters of the CustomDraw event.
This will set each item to red text:

Code: Select all

  Select Case drawStage
    Case CustomDrawStageConstants.cdsPrePaint
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNotifyItemDraw

    Case CustomDrawStageConstants.cdsItemPrePaint
      TextColor = RGB(255, 0, 0)
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNewFont
  End Select
This will set only the 3rd item to red text:

Code: Select all

  Select Case drawStage
    Case CustomDrawStageConstants.cdsPrePaint
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNotifyItemDraw

    Case CustomDrawStageConstants.cdsItemPrePaint
      If listItem.Index = 2 Then
        TextColor = vbRed
      End If
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNewFont
  End Select
This will set the 1st column of each item to red text:

Code: Select all

  Select Case drawStage
    Case CustomDrawStageConstants.cdsPrePaint
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNotifyItemDraw

    Case CustomDrawStageConstants.cdsItemPrePaint
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNotifySubItemDraw

    Case CustomDrawStageConstants.cdsSubItemPrePaint
      If listSubItem.Index = 0 Then
        TextColor = vbRed
      Else
        TextColor = ExLvw.ForeColor
      End If
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNewFont
  End Select
This will set the 2nd column of the 3rd item to red text:

Code: Select all

  Select Case drawStage
    Case CustomDrawStageConstants.cdsPrePaint
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNotifyItemDraw

    Case CustomDrawStageConstants.cdsItemPrePaint
      If listItem.Index = 2 Then
        furtherProcessing = CustomDrawReturnValuesConstants.cdrvNotifySubItemDraw
      End If

    Case CustomDrawStageConstants.cdsSubItemPrePaint
      If listSubItem.Index = 1 Then
        TextColor = vbRed
      Else
        TextColor = ExLvw.ForeColor
      End If
      furtherProcessing = CustomDrawReturnValuesConstants.cdrvNewFont
  End Select
Regards
TiKu
Crunching for Fab36_Folding-Division at Folding@Home. Join Fab36/Fab30! - Folding@Home and BOINC
Boycott DRM! Boycott HDCP!
engee30
Lt. Commander
Posts: 54
Joined: 25 Sep 2012, 19:49
Location: Swindon, UK
Contact:

Re: Listview items colors

Post by engee30 »

Dear me. You were right. It's really simple. I just seem to have missed that event. :oops:
Thanks again.
:D
engee30
Lt. Commander
Posts: 54
Joined: 25 Sep 2012, 19:49
Location: Swindon, UK
Contact:

Re: Listview items colors

Post by engee30 »

Yes, that was straighforward indeed.
However, I'd like to get a similar result if I, say, click on a listview item and the item's backcolor changes into a different color. That's a tricky one, to be honest.
User avatar
TiKu
Administrator
Administrator
Posts: 832
Joined: 28 Sep 2004, 21:10
Location: München
Contact:

Re: Listview items colors

Post by TiKu »

You want the TextBackColor of a selected item to be another color than the default blue?
Crunching for Fab36_Folding-Division at Folding@Home. Join Fab36/Fab30! - Folding@Home and BOINC
Boycott DRM! Boycott HDCP!
engee30
Lt. Commander
Posts: 54
Joined: 25 Sep 2012, 19:49
Location: Swindon, UK
Contact:

Re: Listview items colors

Post by engee30 »

Yes, something like that, and also maybe in cases when I want to select more than one item, not necessarily through the mouse click, but maybe through a bit of code from a CommandButton.
User avatar
TiKu
Administrator
Administrator
Posts: 832
Joined: 28 Sep 2004, 21:10
Location: München
Contact:

Re: Listview items colors

Post by TiKu »

As far as I know the native list view control of Windows, which my control is based on, does not allow a custom selection background color. Of course you could use the CustomDraw event to draw the entire item yourself if it is selected. But if you want it to look the same, just with a different background color, it will be a lot of work.
Crunching for Fab36_Folding-Division at Folding@Home. Join Fab36/Fab30! - Folding@Home and BOINC
Boycott DRM! Boycott HDCP!
engee30
Lt. Commander
Posts: 54
Joined: 25 Sep 2012, 19:49
Location: Swindon, UK
Contact:

Re: Listview items colors

Post by engee30 »

What a shame. :cry:
However, learning that your Listview control was based on the Windows Listview control got me thinking straightaway. I remembered using a module to subclass a Listview so that the user could easily change the backcolor of an item through a function. I implemented that module and used the function and voila, I've got a solution! :ugeek: :mrgreen:
Please find attached the file.
Cheers,
Pete

PS
The code is nothing to do with an item being selected, though.
Attachments
ExLvwDragDrop.zip
(155.69 KiB) Downloaded 498 times
User avatar
TiKu
Administrator
Administrator
Posts: 832
Joined: 28 Sep 2004, 21:10
Location: München
Contact:

Re: Listview items colors

Post by TiKu »

If you look carefully at the Fonts & Colors sample that comes with ExplorerListView, you'll notice that the rows have alternating background colors. The sample does this by setting the TextBackColor parameter of the CustomDraw event. Behind the scenes your module and my control do the same thing. ;)
Crunching for Fab36_Folding-Division at Folding@Home. Join Fab36/Fab30! - Folding@Home and BOINC
Boycott DRM! Boycott HDCP!
engee30
Lt. Commander
Posts: 54
Joined: 25 Sep 2012, 19:49
Location: Swindon, UK
Contact:

Re: Listview items colors

Post by engee30 »

That's right. Two different methods of doing the same thing.
But, as I did in my sample project, I wanted to change the color when the user presses the button. I couldn't do the same with your code in the Customdraw event. In my case, the function does the trick. In the case of your coding, all you can do to change the color seems to be only possible in that Customdraw event, and not through a function, declared in the Button Click event.
Pete
Post Reply