Jump to content

How to change a listview color item?


NDog
 Share

Recommended Posts

Hi there

I am making a program to detect if a computer is online or not. I would like the color or a circle in a listview box to change from yellow to red or yellow to green depending if online or not.

Is it possible? I have my code so far for testing with. I'm getting stuck with changing the color as it doesn't seem to be able to be done dynamically..

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ColorConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>


#include <GuiImageList.au3>



$hMain = GUICreate("Test", -1, 300, -1, -1, BitOr($WS_SIZEBOX, $WS_SYSMENU, $WS_MAXIMIZEBOX, $WS_MINIMIZEBOX), $WS_EX_ACCEPTFILES);x il drag & drop

;;top
GUICtrlCreateLabel("Listview colour", 55, 0, 275, 40)
GUICtrlSetFont(-1, 24, 400, 0, "Comic Sans MS")
GUICtrlSetColor(-1, $COLOR_GREEN)
GUICtrlCreateLabel("Testing 123", 200, 40, 120, 18)
GUICtrlSetFont(-1, 8, 40, 0, "Comic Sans MS")
GUICtrlSetColor(-1, $COLOR_BLUE)

;;body
GUICtrlCreateLabel("User:", 25, 68, 30, 16)
$inUser        = GUICtrlCreateInput("", 56, 66, 83, 21)

$btnLookup     = GUICtrlCreateButton("Lookup", 150, 60, 75, 33, $BS_DEFPUSHBUTTON)
$btnGreen       = GUICtrlCreateButton("Green", 230, 60, 43, 33)

$btnRed    = GUICtrlCreateButton("Red", 275, 60, 43, 33)



$listComputers = GUICtrlCreateListView("Name      |Model                            |Serial            |OS Install date|HDD", 55, 100, 290, 100, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT, $LVS_SINGLESEL))


GUICtrlSetState($inUser, $GUI_FOCUS)
GUISetState(@SW_SHOW)


; Main GUI loop
While True
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $nMsg = $inUser
            GUICtrlSetState($inUser, $GUI_FOCUS)
        Case $nMsg = $btnLookup
            LookupButtonPressed()
        Case $nMsg = $btnGreen
            $iIndex  = _GUICtrlListView_GetSelectedIndices($listComputers)
            $machine = _GUICtrlListView_GetItemText($listComputers, Number($iIndex))
;           ; change to green?
            ;_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listComputers, 0x00FF00, 16, 16)) ; green

        Case $nMsg = $btnRed
            $iIndex  = _GUICtrlListView_GetSelectedIndices($listComputers)
            $machine = _GUICtrlListView_GetItemText($listComputers, Number($iIndex))
;           ; change to red?
            ;_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listComputers, 0xFF0000, 16, 16)) ; red

    EndSelect
WEnd


Func LookupButtonPressed()




    _GUICtrlListView_DeleteAllItems($listComputers)


    ;$aMachines  = machine_lookup($user, $searchstring)
    Local $aMachines[2][5]
    $aMachines[0][0] = "PC01"
    $aMachines[0][1] = "HP Compaq 6000"
    $aMachines[0][2] = "ABC123"
    $aMachines[0][3] = "01/01/2001"
    $aMachines[0][4] = "Toshiba HDD"

    $aMachines[1][0] = "PC02"
    $aMachines[1][1] = "HP Compaq 6000"
    $aMachines[1][2] = "XYZ456"
    $aMachines[1][3] = "01/01/2001"
    $aMachines[1][4] = "Toshiba HDD"


    ;_ArrayDisplay($aMachines)

    If Not @error Then
        If IsArray($aMachines) Then
            $sComputers = _ArrayToString($aMachines, " - ")
            $sComputers = StringReplace($sComputers, @CRLF, "|")
            ;ConsoleWrite($sComputers & @CRLF)

            For $i = 0 To UBound($aMachines) - 1
                $machine       = $aMachines[$i][0]
                $model         = $aMachines[$i][1]
                $serial        = $aMachines[$i][2]
                $OSInstallDate = $aMachines[$i][3]
                $HDD           = $aMachines[$i][4]
                _GUICtrlListView_AddItem($listComputers, $machine)
                _GUICtrlListView_AddSubItem($listComputers, $i, $model,1)
                _GUICtrlListView_AddSubItem($listComputers, $i, $serial,2)
                _GUICtrlListView_AddSubItem($listComputers, $i, $OSInstallDate,3)
                _GUICtrlListView_AddSubItem($listComputers, $i, $OSInstallDate,3)
                _GUICtrlListView_AddSubItem($listComputers, $i, $HDD,4)


                $hImage = _GUIImageList_Create(16, 16)
                _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listComputers, 0xFFFF00, 16, 16)) ; yellow

                _GUICtrlListView_SetImageList($listComputers, $hImage, 1)

            Next

            _GUICtrlListView_SetItemSelected($listComputers, 0)





        EndIf
    EndIf
EndFunc

 

Link to comment
Share on other sites

Hello. Maybe this?

 

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ColorConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>


#include <GuiImageList.au3>



$hMain = GUICreate("Test", -1, 300, -1, -1, BitOR($WS_SIZEBOX, $WS_SYSMENU, $WS_MAXIMIZEBOX, $WS_MINIMIZEBOX), $WS_EX_ACCEPTFILES) ;x il drag & drop

;;top
GUICtrlCreateLabel("Listview colour", 55, 0, 275, 40)
GUICtrlSetFont(-1, 24, 400, 0, "Comic Sans MS")
GUICtrlSetColor(-1, $COLOR_GREEN)
GUICtrlCreateLabel("Testing 123", 200, 40, 120, 18)
GUICtrlSetFont(-1, 8, 40, 0, "Comic Sans MS")
GUICtrlSetColor(-1, $COLOR_BLUE)

;;body
GUICtrlCreateLabel("User:", 25, 68, 30, 16)
$inUser = GUICtrlCreateInput("", 56, 66, 83, 21)

$btnLookup = GUICtrlCreateButton("Lookup", 150, 60, 75, 33, $BS_DEFPUSHBUTTON)
$btnGreen = GUICtrlCreateButton("Green", 230, 60, 43, 33)

$btnRed = GUICtrlCreateButton("Red", 275, 60, 43, 33)



$listComputers = GUICtrlCreateListView("Name      |Model                            |Serial            |OS Install date|HDD", 55, 100, 290, 100, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT, $LVS_SINGLESEL))


GUICtrlSetState($inUser, $GUI_FOCUS)
GUISetState(@SW_SHOW)


; Main GUI loop
While True
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $nMsg = $inUser
            GUICtrlSetState($inUser, $GUI_FOCUS)
        Case $nMsg = $btnLookup
            LookupButtonPressed()
        Case $nMsg = $btnGreen
            $iIndex = _GUICtrlListView_GetSelectedIndices($listComputers)
            $machine = _GUICtrlListView_GetItemText($listComputers, Number($iIndex))
            _GUICtrlListView_SetItemImage($listComputers, $iIndex, 1)
            ;           ; change to green?
            ;_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listComputers, 0x00FF00, 16, 16)) ; green

        Case $nMsg = $btnRed
            $iIndex = _GUICtrlListView_GetSelectedIndices($listComputers)
            $machine = _GUICtrlListView_GetItemText($listComputers, Number($iIndex))
            _GUICtrlListView_SetItemImage($listComputers, $iIndex, 2)
            ;           ; change to red?
            ;_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listComputers, 0xFF0000, 16, 16)) ; red

    EndSelect
WEnd


Func LookupButtonPressed()




    _GUICtrlListView_DeleteAllItems($listComputers)


    ;$aMachines  = machine_lookup($user, $searchstring)
    Local $aMachines[2][5]
    $aMachines[0][0] = "PC01"
    $aMachines[0][1] = "HP Compaq 6000"
    $aMachines[0][2] = "ABC123"
    $aMachines[0][3] = "01/01/2001"
    $aMachines[0][4] = "Toshiba HDD"

    $aMachines[1][0] = "PC02"
    $aMachines[1][1] = "HP Compaq 6000"
    $aMachines[1][2] = "XYZ456"
    $aMachines[1][3] = "01/01/2001"
    $aMachines[1][4] = "Toshiba HDD"


    ;_ArrayDisplay($aMachines)

    If Not @error Then
        If IsArray($aMachines) Then
            $sComputers = _ArrayToString($aMachines, " - ")
            $sComputers = StringReplace($sComputers, @CRLF, "|")
            ;ConsoleWrite($sComputers & @CRLF)

            For $i = 0 To UBound($aMachines) - 1
                $machine = $aMachines[$i][0]
                $model = $aMachines[$i][1]
                $serial = $aMachines[$i][2]
                $OSInstallDate = $aMachines[$i][3]
                $HDD = $aMachines[$i][4]
                _GUICtrlListView_AddItem($listComputers, $machine)
                _GUICtrlListView_AddSubItem($listComputers, $i, $model, 1)
                _GUICtrlListView_AddSubItem($listComputers, $i, $serial, 2)
                _GUICtrlListView_AddSubItem($listComputers, $i, $OSInstallDate, 3)
                _GUICtrlListView_AddSubItem($listComputers, $i, $OSInstallDate, 3)
                _GUICtrlListView_AddSubItem($listComputers, $i, $HDD, 4)


                $hImage = _GUIImageList_Create(16, 16)
                _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listComputers, $COLOR_YELLOW, 16, 16)) ; yellow
                _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listComputers, $COLOR_GREEN, 16, 16))
                _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listComputers, $COLOR_RED, 16, 16))
                _GUICtrlListView_SetImageList($listComputers, $hImage, 1)

            Next

            _GUICtrlListView_SetItemSelected($listComputers, 0)






        EndIf
    EndIf
EndFunc   ;==>LookupButtonPressed

Saludos

Link to comment
Share on other sites

  • Moderators

NDog,

Or you could use my GUIListViewEx UDF - the link is in my sig. Here is a simple example showing a colour change in column 1 based on a few simple conditions:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#include "GUIListViewEx.au3"

$hGUI = GUICreate("Coloured ListView Example", 500, 300)

; Create ListView
$cLV = GUICtrlCreateListView("Column 0|Colourable|Column 2|Column 3", 10, 10, 480, 260, BitOR($LVS_SINGLESEL, $LVS_SHOWSELALWAYS))
For $i = 0 To 3
    _GUICtrlListView_SetColumnWidth($cLV, $i, 100)
Next

; Initiate ListView = user colours
$iLVIndex = _GUIListViewEx_Init($cLV, "", 0, 0, True, 32)

; Set default colours to use
Global $aDefCols[4] = ["0x000000", "0xFEFEFE", "0xFFFFFF", "0x0000FF"]
_GUIListViewEx_SetDefColours($iLVIndex, $aDefCols)

; If colours used then this function must be run BEFORE GUISetState
_GUIListViewEx_MsgRegister()

GUISetState()

; Create array and fill listview
Global $aLVArray[6][4]
For $i = 0 To 5
    $sData = "Item " & $i & "-0"
    $aLVArray[$i][0] = $sData
    For $j = 1 To 3
        $sData &= "|SubItem " & $i & "-" & $j
        $aLVArray[$i][$j] = "SubItem " & $i & "-" & $j
    Next
    _GUIListViewEx_InsertSpec($iLVIndex, 0, $sData)

    ; Some simple conditions for colour
    $sColSet = ";0x00FF00"
    If Mod($i, 2) = 0 Then
        $sColSet = ";0xFFFF00"
    ElseIf Mod($i, 3) = 0 Then
        $sColSet = ";0xFF0000"
    EndIf
    ; Set item colour
    _GUIListViewEx_SetColour($iLVIndex, $sColSet, 0, 1)
Next

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    $vRet = _GUIListViewEx_EventMonitor()

WEnd

Any use?

M23

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

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...