Jump to content

Set color of an item inside the GUI list view


Go to solution Solved by ioa747,

Recommended Posts

Hello, I want to change the color of a text inside the GUI list.

I checked the default code for _GUICtrlListView_AddItem and _GUICtrlListView_SetTextColor, but that couldn't do what I want. Let's use the default example in _GUICtrlListView_SetTextColor and I want to set a different color for each item: 

image.png.a73b1b00754f78de429a33ebaa4de291.pngimage.png.6ff10dfd43dd7dc48ed3c149db5a4fef.pngimage.png.83255f7f392f830a7c510315109bd6c6.png

If I change the color here, it is changing for all the items in the list. 

_GUICtrlListView_SetTextColor($idListview, $CLR_BLACK)

I also found GUICtrlSetColor, but all of them are the same color.

GUICtrlSetColor($idListview, $COLOR_YELLOW)

I'm sure that I shouldn't use $idListview, and I should set one item inside list, but I don't know. How can I change the color of different items in the list?

 

TY.

Link to comment
Share on other sites

  • Solution
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <ColorConstants.au3>

Example()

Func Example()
    GUICreate("listview items", 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
;~  GUISetBkColor(0x00E0FFFF) ; will change background color

    Local $idListview = GUICtrlCreateListView("col1  |col2|col3  ", 10, 10, 200, 150) ;,$LVS_SORTDESCENDING)
    Local $idItem1 = GUICtrlCreateListViewItem("item1|col22|col23", $idListview)
    Local $idItem2 = GUICtrlCreateListViewItem("item2|col12|col13", $idListview)
    Local $idItem3 = GUICtrlCreateListViewItem("item3|col32|col33", $idListview)
    GUICtrlSetColor(-1, $COLOR_RED)
    GUICtrlSetBkColor(-1, $COLOR_AQUA)

    GUICtrlSetColor($idItem1, $COLOR_DARKGREEN)
    GUICtrlSetBkColor($idItem1, $COLOR_GOLD)

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd
EndFunc   ;==>Example

 

I know that I know nothing

Link to comment
Share on other sites

Posted (edited)

This is what I'm trying to do:

Until I reach the height of my list (that takes 3 inputs), everything is good and I can see every color as I input. 

However, when I set the 4th color as green (which exceeds the height of the list view), I get a scroll bar (that is okay), but all the colors are turned into the last color. Even more, I keep adding different colors and when I mouse wheel up / down, the colors in the upper section of the list change. 

If I increase my list height from 90 to something more, it doesn't matter. Whenever I reach the original height and the scroll bar appears, the next color changes everything input before. How can I prevent it?

image.thumb.png.ac0dfb32bcf384bc09715c29ad40d752.png

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

_colorGUI()

Func _colorGUI()
    GUICreate("Color GUI", 350, 300)

    GUICtrlCreateLabel("Name", 10, 10, 30, 20)
    $hName = GUICtrlCreateInput("", 50, 10, 120, 20)

    GUICtrlCreateLabel("Color", 180, 10, 30, 20)
    $hColorCombo = GUICtrlCreateCombo("", 220, 10, 120, 20)
    GUICtrlSetData($hColorCombo, "RED|GREEN|BLUE")

    $hAddButton = GUICtrlCreateButton("Add", 125, 40, 100, 40)

    $hList = GUICtrlCreateListView("", 10, 100, 270, 90)
    _GUICtrlListView_InsertColumn($hList, 0, "Color", 140)
    _GUICtrlListView_InsertColumn($hList, 0, "Name", 100)
    ;set background color black
    GUICtrlSetBkColor($hList, 0x000000)

    GUISetState(@SW_SHOW)

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $hAddButton
                ;check the invalid inputs
                If GUICtrlRead($hName) = "" Or GUICtrlRead($hColorCombo) = "" Then
                    ContinueLoop
                EndIf
                ;set the color based on dropdown
                If GUICtrlRead($hColorCombo) = "RED" Then
                    GUICtrlSetColor($hList, 0xFF0000)
                ElseIf GUICtrlRead($hColorCombo) = "GREEN" Then
                    GUICtrlSetColor($hList, 0x00FF00)
                ElseIf GUICtrlRead($hColorCombo) = "BLUE" Then
                    GUICtrlSetColor($hList, 0x0000FF)
                Else ;if not RGB set color white
                    GUICtrlSetColor($hList, 0xFFFFFF)
                EndIf
                GUICtrlCreateListViewItem(GUICtrlRead($hName) & "|" & GUICtrlRead($hColorCombo), $hList)
        EndSwitch
    WEnd
    GUIDelete()
EndFunc

 

Edited by taylansan

TY.

Link to comment
Share on other sites

Why are you setting the color to the listview and not to the listview item?

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

_colorGUI()

Func _colorGUI()
    GUICreate("Color GUI", 350, 300)

    GUICtrlCreateLabel("Name", 10, 10, 30, 20)
    $hName = GUICtrlCreateInput("", 50, 10, 120, 20)

    GUICtrlCreateLabel("Color", 180, 10, 30, 20)
    $hColorCombo = GUICtrlCreateCombo("", 220, 10, 120, 20)
    GUICtrlSetData($hColorCombo, "RED|GREEN|BLUE")

    $hAddButton = GUICtrlCreateButton("Add", 125, 40, 100, 40)

    $hList = GUICtrlCreateListView("", 10, 100, 270, 90)
    _GUICtrlListView_InsertColumn($hList, 0, "Color", 140)
    _GUICtrlListView_InsertColumn($hList, 0, "Name", 100)
    ;set background color black
    GUICtrlSetBkColor($hList, 0x000000)

    GUISetState(@SW_SHOW)

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $hAddButton
                ;check the invalid inputs
                If GUICtrlRead($hName) = "" Or GUICtrlRead($hColorCombo) = "" Then
                    ContinueLoop
                EndIf
                GUICtrlCreateListViewItem(GUICtrlRead($hName) & "|" & GUICtrlRead($hColorCombo), $hList)
                ;set the color based on dropdown
                If GUICtrlRead($hColorCombo) = "RED" Then
                    GUICtrlSetColor(-1, 0xFF0000)
                ElseIf GUICtrlRead($hColorCombo) = "GREEN" Then
                    GUICtrlSetColor(-1, 0x00FF00)
                ElseIf GUICtrlRead($hColorCombo) = "BLUE" Then
                    GUICtrlSetColor(-1, 0x0000FF)
                Else ;if not RGB set color white
                    GUICtrlSetColor(-1, 0xFFFFFF)
                EndIf
        EndSwitch
    WEnd
    GUIDelete()
EndFunc

 

When the words fail... music speaks.

Link to comment
Share on other sites

Because I didn't know how to set color to a list view. I was thinking to write something like ListView[1], ListView[2], ListView[3], etc. I didn't know -1 would work. Thank you 

TY.

Link to comment
Share on other sites

1 minute ago, taylansan said:

I didn't know -1 would work.

This means the last created control. But you can explicitly set what control you want to change color.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

_colorGUI()

Func _colorGUI()
    GUICreate("Color GUI", 350, 300)

    GUICtrlCreateLabel("Name", 10, 10, 30, 20)
    $hName = GUICtrlCreateInput("", 50, 10, 120, 20)

    GUICtrlCreateLabel("Color", 180, 10, 30, 20)
    $hColorCombo = GUICtrlCreateCombo("", 220, 10, 120, 20)
    GUICtrlSetData($hColorCombo, "RED|GREEN|BLUE")

    $hAddButton = GUICtrlCreateButton("Add", 125, 40, 100, 40)

    $hList = GUICtrlCreateListView("", 10, 100, 270, 90)
    _GUICtrlListView_InsertColumn($hList, 0, "Color", 140)
    _GUICtrlListView_InsertColumn($hList, 0, "Name", 100)
    ;set background color black
    GUICtrlSetBkColor($hList, 0x000000)

    GUISetState(@SW_SHOW)

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $hAddButton
                ;check the invalid inputs
                If GUICtrlRead($hName) = "" Or GUICtrlRead($hColorCombo) = "" Then
                    ContinueLoop
                EndIf
                $cCtrl = GUICtrlCreateListViewItem(GUICtrlRead($hName) & "|" & GUICtrlRead($hColorCombo), $hList)
                ;set the color based on dropdown
                If GUICtrlRead($hColorCombo) = "RED" Then
                    GUICtrlSetColor($cCtrl, 0xFF0000)
                ElseIf GUICtrlRead($hColorCombo) = "GREEN" Then
                    GUICtrlSetColor($cCtrl, 0x00FF00)
                ElseIf GUICtrlRead($hColorCombo) = "BLUE" Then
                    GUICtrlSetColor($cCtrl, 0x0000FF)
                Else ;if not RGB set color white
                    GUICtrlSetColor($cCtrl, 0xFFFFFF)
                EndIf
        EndSwitch
    WEnd
    GUIDelete()
EndFunc

 

When the words fail... music speaks.

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