Jump to content
Sign in to follow this  
Montfrooij

[SOLVED]Row colours and sorting in listview

Recommended Posts

I have a listview in my gui which gives the various columns a different colour (even / uneven).

This works fine, but when I am adding a sort with _GUICtrlListView_SortItems the colours of the rows are also sorted.

Is there a way to keep the colouring per even / uneven line and still be able to sort?

I added a simplified example of a script that does the same (borrowed from autIT help section with some minor changes)

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global $hListView, $hListView2

_Example1()

Func _Example1()
    Local $hImage, $aIcons[3] = [0, 3, 6]
    Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
    Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_CHECKBOXES, $LVS_EX_DOUBLEBUFFER)

    GUICreate("ListView Sort", 300, 200)

    $hListView = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, $iExListViewStyle)


    For $r = 1 To 10
    GUICtrlCreateListViewItem($r & "|" & random(0,90) &"|" & random(0,90),$hListView)
        If Mod($r, 2) = 0 Then GUICtrlSetBkColor(-1, 0xD0DEC7)
    Next

    GUISetState()

    _GUICtrlListView_RegisterSortCallBack($hListView)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $hListView
                ; Kick off the sort callback
                _GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))
        EndSwitch
    WEnd

    _GUICtrlListView_UnRegisterSortCallBack($hListView)
    GUIDelete()
EndFunc   ;==>_Example1

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $hWndListView2

    $hWndListView = $hListView
    $hWndListView2 = $hListView2
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
    If Not IsHWnd($hListView2) Then $hWndListView2 = GUICtrlGetHandle($hListView2)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView, $hWndListView2
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)

                    ; Kick off the sort callback
                    _GUICtrlListView_SortItems($hWndFrom, DllStructGetData($tInfo, "SubItem"))
                    ; No return value
            EndSwitch
    EndSwitch
    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Thanks in advance.

Edited by Montfrooij

Share this post


Link to post
Share on other sites

I've changed your script to fix this issue, there's a built in switch to color alternate rows for listview items. See the lines marked with ;~ <<<<<<<<<<<<<<<<<<<<<<<<<<<< for the changes.

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global $hListView, $hListView2

_Example1()

Func _Example1()
    Local $hImage, $aIcons[3] = [0, 3, 6]
    Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
    Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_CHECKBOXES, $LVS_EX_DOUBLEBUFFER)

    GUICreate("ListView Sort", 300, 200)

    $hListView = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE) ;~  <<<<<<<<<<<<<<<<<<<<<<<<< Add this line
  _GUICtrlListView_SetExtendedListViewStyle($hListView, $iExListViewStyle)


    For $r = 1 To 10
    GUICtrlCreateListViewItem($r & "|" & random(0,90) &"|" & random(0,90),$hListView)
    GUICtrlSetBkColor(-1, 0xD0DEC7) ;~  <<<<<<<<<<<<<<<<<<<<<< Change to this
    Next

    GUISetState()

    _GUICtrlListView_RegisterSortCallBack($hListView)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $hListView
                ; Kick off the sort callback
                _GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))
        EndSwitch
    WEnd

    _GUICtrlListView_UnRegisterSortCallBack($hListView)
    GUIDelete()
EndFunc   ;==>_Example1

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $hWndListView2

    $hWndListView = $hListView
    $hWndListView2 = $hListView2
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
    If Not IsHWnd($hListView2) Then $hWndListView2 = GUICtrlGetHandle($hListView2)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView, $hWndListView2
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)

                    ; Kick off the sort callback
                    _GUICtrlListView_SortItems($hWndFrom, DllStructGetData($tInfo, "SubItem"))
                    ; No return value
            EndSwitch
    EndSwitch
    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I've changed your script to fix this issue, there's a built in switch to color alternate rows for listview items. See the lines marked with ;~ <<<<<<<<<<<<<<<<<<<<<<<<<<<< for the changes.

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global $hListView, $hListView2

_Example1()

Func _Example1()
    Local $hImage, $aIcons[3] = [0, 3, 6]
    Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
    Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_GRIDLINES, $LVS_EX_CHECKBOXES, $LVS_EX_DOUBLEBUFFER)

    GUICreate("ListView Sort", 300, 200)

    $hListView = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180, -1, $iExWindowStyle)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_LV_ALTERNATE) ;~  <<<<<<<<<<<<<<<<<<<<<<<<< Add this line
  _GUICtrlListView_SetExtendedListViewStyle($hListView, $iExListViewStyle)


    For $r = 1 To 10
    GUICtrlCreateListViewItem($r & "|" & random(0,90) &"|" & random(0,90),$hListView)
    GUICtrlSetBkColor(-1, 0xD0DEC7) ;~  <<<<<<<<<<<<<<<<<<<<<< Change to this
    Next

    GUISetState()

    _GUICtrlListView_RegisterSortCallBack($hListView)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $hListView
                ; Kick off the sort callback
                _GUICtrlListView_SortItems($hListView, GUICtrlGetState($hListView))
        EndSwitch
    WEnd

    _GUICtrlListView_UnRegisterSortCallBack($hListView)
    GUIDelete()
EndFunc   ;==>_Example1

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $hWndListView2

    $hWndListView = $hListView
    $hWndListView2 = $hListView2
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
    If Not IsHWnd($hListView2) Then $hWndListView2 = GUICtrlGetHandle($hListView2)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView, $hWndListView2
            Switch $iCode
                Case $LVN_COLUMNCLICK ; A column was clicked
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)

                    ; Kick off the sort callback
                    _GUICtrlListView_SortItems($hWndFrom, DllStructGetData($tInfo, "SubItem"))
                    ; No return value
            EndSwitch
    EndSwitch
    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Thanks, that did the trick!

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...