Jump to content

Is it possible set color of single listview item?


Recommended Posts

I use the following code to change the color of every second entry

GUICtrlCreateListViewItem("itemtext", $hGUIUserList)
If Mod($iRec, 2) = 0 Then GUICtrlSetBkColor(-1, 0xD0DEC7)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I use the following code to change the color of every second entry

GUICtrlCreateListViewItem("itemtext", $hGUIUserList)
If Mod($iRec, 2) = 0 Then GUICtrlSetBkColor(-1, 0xD0DEC7)
Thanks for the rely, it is not working for me.

I found this thread which helped me:

http://www.autoitscript.com/forum/index.ph...mp;#entry675962

Edited by lsakizada

Be Green Now or Never (BGNN)!

Link to comment
Share on other sites

  • Moderators

lsakizada,

The syntax for alternate coloured ListViews is not easy to get from the Help file:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#Include <GuiListView.au3>

$hWin = GUICreate("Alt Coloured ListView", 500, 500, -1, -1)
    GUISetBkColor(0xCECECE)
$hList = GUICtrlCreateListView ("This is an alternate coloured ListView", 10, 10, 480, 480, $LVS_SINGLESEL)
; Set up alt line colouring
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE)
    _GUICtrlListView_SetColumnWidth($hList, 0, $LVSCW_AUTOSIZE_USEHEADER)
GUISetState()

For $i = 1 to 10
    GUICtrlCreateListViewItem("Item " & $i, $hList)
; Set up alt line colouring
        GUICtrlSetBkColor(-1, 0xCCFFCC)
    Next

While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
WEnd

M23

Edit: If you really want to customise your ListView then look here.

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 1 month later...

lsakizada,

The syntax for alternate coloured ListViews is not easy to get from the Help file:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#Include <GuiListView.au3>

$hWin = GUICreate("Alt Coloured ListView", 500, 500, -1, -1)
    GUISetBkColor(0xCECECE)
$hList = GUICtrlCreateListView ("This is an alternate coloured ListView", 10, 10, 480, 480, $LVS_SINGLESEL)
; Set up alt line colouring
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE)
    _GUICtrlListView_SetColumnWidth($hList, 0, $LVSCW_AUTOSIZE_USEHEADER)
GUISetState()

For $i = 1 to 10
    GUICtrlCreateListViewItem("Item " & $i, $hList)
; Set up alt line colouring
        GUICtrlSetBkColor(-1, 0xCCFFCC)
    Next

While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
WEnd

M23

Edit: If you really want to customise your ListView then look here.

Is it possible to use the alternate background colour when using _GuiCtrlListview_AddArray?

I can't figure it out, seeing as I'm not creating individual listviewItems, so don't have an ID for assigning background colour to the item.

Any help, greatly appreciated.

D

Link to comment
Share on other sites

Is it possible to use the alternate background colour when using _GuiCtrlListview_AddArray?

I can't figure it out, seeing as I'm not creating individual listviewItems, so don't have an ID for assigning background colour to the item.

Any help, greatly appreciated.

D

This is a snippet of what I've tried (which isn't working)

This is a snippet from a function which is passed the listview as $control, and the array as $aContent.

My fear is that I am misunderstanding the _GUICtrlListView_GetItemParam function. I *think* that it will return the control ID for a listviewItem associated with the specified line.

GUICtrlSetFont($control, 11)
    GUICtrlSetBkColor($control, $GUI_BKCOLOR_LV_ALTERNATE)  ;set alternate colours for listviewitems
    ;GUICtrlSetBkColor($control, 0xFFFFFF)
    
    _GUICtrlListView_AddArray($control, $aContent)
    
        $numItems = _GUICtrlListView_GetItemCount(GUICtrlGetHandle($control)) - 1
    for $i = $numItems to 0 Step -1
    $listviewItem = _GUICtrlListView_GetItemParam(GUICtrlGetHandle($control), $i)
        if $listviewItem Then
            GUICtrlSetBkColor($listviewItem, 0xECE9D8)
        EndIf
    Next

I have this product nearly fully tested so don't want to introduce total unknowns by using the UDF you linked to above.

Thanks,

D

Link to comment
Share on other sites

My fear is that I am misunderstanding the _GUICtrlListView_GetItemParam function. I *think* that it will return the control ID for a listviewItem associated with the specified line.

So, I *think* I misunderstood waht _GUICtrlListView_GetItemParam does. In its stead, I'm using _GUICtrlListView_MapIndexToID

GUICtrlSetFont($control, 11)
    GUICtrlSetBkColor($control, $GUI_BKCOLOR_LV_ALTERNATE)  ;set alternate colours for listviewitems
    ;GUICtrlSetBkColor($control, 0xFFFFFF)
    
    _GUICtrlListView_AddArray($control, $aContent)
    
        $numItems = _GUICtrlListView_GetItemCount(GUICtrlGetHandle($control)) - 1
    for $i = $numItems to 0 Step -1
    $listviewItem = _GUICtrlListView_MapIndexToID(GUICtrlGetHandle($control), $i)
        if $listviewItem Then
            GUICtrlSetBkColor($listviewItem, 0xECE9D8)
        EndIf
    Next

But for some reason, my listview is remaining unchanged, and a couple of totally unrelated labels are having their background set.

Any help greatly appreciated, and apologies for the ongoing soliloquy.

D

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...