Sign in to follow this  
Followers 0
Hobbyist

Changing Multiple Listview Background Colors

13 posts in this topic

I would like to be able to change, at runtime, the background colors of the Listviews in the script.  I have copied and pasted that part of the script I am attempting to use albeit unsuccessfully.

1. choosing a color from the ColorChoice box

2. save it to my ini file

3. recall the saved color from the ini file later

4. change the background color of the Listviews during this runtime and not just at next start up.

I thought I should be making the Listview(s) into a variable to be used in the SetState and loop through to change all of them.

Very inexperienced at this, so it may look cumbersome or inefficient so any and all help is appreciated.

Thanks in advance.

Hobbyist

;used to open color choice box and pick color, write it to ini file
Case $ColorCode
    $iReturnType = 2
    $shades = _ChooseColor($iReturnType)
    _ColorChoice($shades)

then use this to record my choice from above

;write color choice to ini file
Func _ColorChoice($shades)
    IniWrite($sIni, "Clr", "Clr", $shades)
    
EndFunc   ;==>_ColorChoice

then use this to recall my choice at a later time, via  a button or something

;read color choice from ini file
Func _ColorRead(ByRef $shades)
    $shades = Int(IniRead($sIni, "Clr", "Clr", 1))
EndFunc   ;==>_ColorRead


then the below function would change the background color of ALL Listview controls


;read color choice and assign to Listview1 through Listview3
;only one Listview is seen at a time, rest hidden or disabled and hidden

Func _colorit()
    _ColorRead($shades)

    $mycolor = 1 to 3 ;used 3 in this example but not held to it
    Local $mm = String("$List") & $mycolor 
    GUICtrlSetBkColor($mm, $shades)
    GUICtrlSetState($mm, $GUI_show);not sure if this is right/correct
    Next


EndFunc   ;==>_colorit

 

Share this post


Link to post
Share on other sites



SetBkColor() needs the Handles of the Listview not the name of it (and your method if passing the name is incorrect as well).

Look at the GUICtrlCreateListView help file for how to do this.  If you want multiple listviews stored in an array, then you do it like this:

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

Example()

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

    Local $aidListview[3]

    Local $aListViewColors[3] = ["0xFF0000","0x00FF00","0x0000FF"]

    For $i = 0 to UBound($aidListview) -1
        $aidListview[$i]= GUICtrlCreateListView("col1  |col2|col3  ", 10+($i*210), 10, 200, 150) ;,$LVS_SORTDESCENDING)
        GUICtrlSetBkColor($aidListview[$i],$aListViewColors[$i])
    Next


    Local $idButton = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
    Local $idItem1 = GUICtrlCreateListViewItem("item2|col22|col23", $aidListview[0])
    Local $idItem2 = GUICtrlCreateListViewItem("item1|col12|col13", $aidListview[0])
    Local $idItem3 = GUICtrlCreateListViewItem("item3|col32|col33", $aidListview[0])
    GUICtrlCreateInput("", 20, 200, 150)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; to allow drag and dropping
    GUISetState(@SW_SHOW)
    GUICtrlSetData($idItem2, "ITEM1")
    GUICtrlSetData($idItem3, "||COL33")
    GUICtrlDelete($idItem1)



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

            Case $idButton
                MsgBox($MB_SYSTEMMODAL, "listview item", GUICtrlRead(GUICtrlRead($aidListview)), 2)

            Case $aidListview
                MsgBox($MB_SYSTEMMODAL, "listview", "clicked=" & GUICtrlGetState($aidListview), 2)

        EndSwitch
    WEnd
EndFunc   ;==>Example

 

Share this post


Link to post
Share on other sites

Before posting I had referenced the help section and found:

GUICtrlSetBkColor ( controlID, backgroundcolor )

so that seems to be in conflict with what you advised - needing the handle.

The help section, thus, impacted my direction for attempting to get each Listview color changed.

I have discovered(using the help section method) that using my attempt to "reference" each Listview does not work BUT if I hard code a Listview($List3 for instance) it does work. So using controlID does work. So basically I need to know if using a variable to define controlID is the same as hard coding a controlID.

Thanks

 

Share this post


Link to post
Share on other sites

Before posting I had referenced the help section and found:

GUICtrlSetBkColor ( controlID, backgroundcolor )

so that seems to be in conflict with what you advised - needing the handle.

The help section, thus, impacted my direction for attempting to get each Listview color changed.

I have discovered(using the help section method) that using my attempt to "reference" each Listview does not work BUT if I hard code a Listview($List3 for instance) it does work. So using controlID does work. So basically I need to know if using a variable to define controlID is the same as hard coding a controlID.

Thanks

 

Yes, I meant ControlID instead of handle.  In my head the are basically the same thing.  Hardcoding the ID does work yes, but its VERY bad practice because as you add more controls / delete them the controlID will change.  Hence using variables to store the controlID (handle)

Share this post


Link to post
Share on other sites

Yes I learned early that hard coding an ID can be a disadvantage.

Ok so specifically to the area of my script which attempts to reference the 3 Listviews ($List1, $List2, $List3)

I threw in a msg box line to see if I was getting the desired outcome and I did:

When I take the string $List and add the number(1,2 or 3) I get $List1, $List2 or $List3 in the msg box so that seems to work.

But when the variable assigned that string is used in the setbkcolor line and I show the listview, nothing happens. At a restart i get the new color, but I am trying to get it at run time. Then if I hard code the $list into the setbkcolor line it works.

so any clue on why the variable method is bunk?

$mycolor = 1 to 3 ;used 3 in this example but not held to it
    Local $mm = String("$List") & $mycolor 
    GUICtrlSetBkColor($mm, $shades)
    GUICtrlSetState($mm, $GUI_show);not sure if this is right/correct
    Next

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Yes I learned early that hard coding an ID can be a disadvantage.

Ok so specifically to the area of my script which attempts to reference the 3 Listviews ($List1, $List2, $List3)

I threw in a msg box line to see if I was getting the desired outcome and I did:

When I take the string $List and add the number(1,2 or 3) I get $List1, $List2 or $List3 in the msg box so that seems to work.

But when the variable assigned that string is used in the setbkcolor line and I show the listview, nothing happens. At a restart i get the new color, but I am trying to get it at run time. Then if I hard code the $list into the setbkcolor line it works.

so any clue on why the variable method is bunk?

$mycolor = 1 to 3 ;used 3 in this example but not held to it
    Local $mm = String("$List") & $mycolor 
    GUICtrlSetBkColor($mm, $shades)
    GUICtrlSetState($mm, $GUI_show);not sure if this is right/correct
    Next

Nothing happens because your trying to GUICtrlSetKBColor to $mm, which is just a string.  So your passing a string for the ControlID, and it fails. Autoit doesn't evaluate that string and then look for that variable, it just sees its a string, and fails.  You *HAVE* to pass the controlID, as I already said.  See my post on using an array to store the controlID's, and If you have any questions about that, ask.  The way you are trying is just flat wrong.

 

Also, your For array is missing its For.... (not that the rest of it is correct)

For $mycolor = 1 to 3 ;used 3 in this example but not held to it
    Local $mm = String("$List") & $mycolor 
    GUICtrlSetBkColor($mm, $shades)
    GUICtrlSetState($mm, $GUI_show);not sure if this is right/correct
Next

 

Edited by kaisies

Share this post


Link to post
Share on other sites

Sorry for the continuous questions, just trying hard to understand this.  I did read your post, as well as the help.

So here I have two statements out of the help:

GUICtrlSetState ( controlID, state )

GUICtrlSetBkColor ( controlID, backgroundcolor )

and put both to use in my script. THIS is the confusing part, GIVEN what you say.

the GUICtrlSetState ( controlID, state ) will work using the $mm variable as I did.  I tried it and got a listview to show and hide.

the GUICtrlSetBkColor ( controlID, backgroundcolor ) does not. But both call for the controlID.

Conflicting info for me to process.  But I do fully appreciate your time and obvious experience.

Hobbyist

Share this post


Link to post
Share on other sites

Can you please post your full script showing what you're attempting? It would make things much clearer using your script as a base rather than coming up with something that bears no resemblance to your script.


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

#9 ·  Posted (edited)

Sorry for the continuous questions, just trying hard to understand this.  I did read your post, as well as the help.

So here I have two statements out of the help:

GUICtrlSetState ( controlID, state )

GUICtrlSetBkColor ( controlID, backgroundcolor )

and put both to use in my script. THIS is the confusing part, GIVEN what you say.

the GUICtrlSetState ( controlID, state ) will work using the $mm variable as I did.  I tried it and got a listview to show and hide.

the GUICtrlSetBkColor ( controlID, backgroundcolor ) does not. But both call for the controlID.

Conflicting info for me to process.  But I do fully appreciate your time and obvious experience.

Hobbyist

Questions are how we learn :)

I am unable to reproduce GUICtrlSetState() as you say it works.  Regarless, if it does, that's a fluke, and is still the incorrect way to use that function.  

Edited by kaisies

Share this post


Link to post
Share on other sites

@kaisies

I dropped an ArrayDisplay into your example and now see exactly what you are talking about.

thanks for your help/guidance

Share this post


Link to post
Share on other sites

@kaisies

I dropped an ArrayDisplay into your example and now see exactly what you are talking about.

thanks for your help/guidance

Welcome :)

Share this post


Link to post
Share on other sites

Hobbyist,

You can achieve a sort of "dynamic" variable name using eval.  See the code below...

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#include <GuiListView.au3>

#include <array.au3>

#AutoIt3Wrapper_Add_Constants=n

local $gui010 = guicreate('')
local $aSize = WinGetclientsize($gui010)
local $list1 =  guictrlcreatelistview('Items',0,20,$aSize[0],100)
local $list2 =  guictrlcreatelistview('Items',0,140,$aSize[0],100)
local $list3 =  guictrlcreatelistview('Items',0,260,$aSize[0],100)
_GUICtrlListView_SetColumnWidth ($list1, 0,  $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth ($list2, 0,  $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth ($list3, 0,  $LVSCW_AUTOSIZE_USEHEADER)
local $btn010 = guictrlcreatebutton('Button',10,$aSize[1] - 30,$aSize[0] - 20,20)

for $i = 0 to 9
    GUICtrlCreateListViewItem('Item # ' & $i,$list1)
    GUICtrlCreateListViewItem('Item # ' & $i,$list2)
    GUICtrlCreateListViewItem('Item # ' & $i,$list3)
next

guisetstate()

while 1
    switch guigetmsg()
        case $gui_event_close
            Exit
        case $btn010
            ; set all listviews to white
            for $i = 1 to 3
                GUICtrlSetBkColor(eval('list' & $i),0xffffff)
            next
            ; set a random listview to maroon
            GUICtrlSetBkColor(eval('list' & random(1,3,1)),0xaa0000)
    EndSwitch

WEnd

kylomas

1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

@kylomas

I did not know about "eval" .  This is very handy and actually gets me to where I want to be.

The replies on this post have been VERY helpful to me in learning more about Autoit and scripting.

 

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  
Followers 0