Jump to content

ListViews with Icons done the "correct" way


Recommended Posts

In coding, the "right way" vs the "wrong way" is often a matter of opinion and is frequently a matter of intense "debate".  I'm calling this the "correct way" for two reasons.  

One,  if you go to the "Hlep Libraries" and look up "GUICtrlCreateListView", it will tell you: "To add items to a ListView control use GUICtrlCreateListViewItem()", it does NOT say to use "_GUICtrlListView_AddSubItem",  which seems to be the "common way".  

Second, a forum member did some testing, and determined that the "native way" (what I'm calling the "correct way"), is substantially faster than the "common way": https://www.autoitscript.com/forum/topic/67829-_guictrllistview_additem-much-slower-than-guictrlcreatelistviewitem/

The issue:  
I've built and populated my Listviews in the same ways used in all of the examples that I've found.  And, for most things, it works great!  But when I begin to explore some of the finer points of ListView manipulation, like color, things don't work quite the way that it seems they should.  I've been pulling my hair out trying to figure out why not.

This is what I have pieced together...


Background:
I use multi column Listviews with a small icon for each entry at the left.  I'm populating my lists from files.

In general, the sequence goes like this:
1)  Create the listview.  This is where the problems start.  Most working examples, with icons, will have you create the Listview like this: 

Quote

ListView = GUICtrlCreateListView("", $left, $top, $width, $height, BitOR($CHOOSE, $YOUR, $STYLES))
_GUICtrlListView_SetExtendedListViewStyle($ListView, BitOR($CHOOSE, $MORE, $STYLES))
 _GUICtrlListView_AddColumn($ListView, "Column1", 60)
_GUICtrlListView_AddColumn($ListView, "Column2", 80, 1)
_GUICtrlListView_AddColumn($ListView, "Column3", 500, 2)
_GUICtrlListView_AddColumn($ListView, "Column4", 60, 0)

However, the "correct" way to do this is like this:

Quote

$ListView = GUICtrlCreateListView("Column1|          Column2       | Column3 |Column4", $left, $top, $width, $height, BitOR($CHOOSE, $YOUR, $STYLES))
_GUICtrlListView_SetExtendedListViewStyle($ListView, BitOR($CHOOSE, $MORE, $STYLES))
_GUICtrlListView_SetColumnWidth($ListView, 2, 500)


Notice the extra spaces in Column2 and notice the SetColumnWidth command:  use whitespaces in the creation if you can, but I have one field that is much longer than the rest so I set that width separately.  


2) Initialize an imagelist:  $hImage_Temp = _GUIImageList_Create(32, 32)


3) Populate your variables.  I scan for data for each row, so this will be looped.
    Define your image for the row:  $ImageList_Icon =  $Dir\image1.bmp
    Read data:  $Column1 = IniRead($File, "Section", "key", "")
                $Column2...  etc


4) Populate your Listview.  And here is the rest of the confusion.  All of the examples that I have come across have you populating your list like this:

Quote

$RowNB = $i - 1 (zero based indexing)
_GUICtrlListView_AddItem($ListView, "", $RowNB)
_GUICtrlListView_AddSubItem($ListView, $RowNB, $Column1, 1)
_GUICtrlListView_AddSubItem($ListView, $RowNB, $Column2, 2)
_GUICtrlListView_AddSubItem($ListView, $RowNB, $Column3, 3)
_GUICtrlListView_AddSubItem($ListView, $RowNB, $Column4, 4)

The "correct" way to do this, is as follows:

Quote

$RowNB = $i - 1 (zero based indexing)

GUICtrlCreateListViewItem("           |" & $Column1 & "|" & $Column2 & "|" & $Column3 & "|" & $Column4, $ListView)
_GUICtrlListView_SetItemImage($ListView, $RowNB, $RowNB)  ;this line is essential for your icons!


5) Loop


6) Connect your imagelist file to your Listview:  _GUICtrlListView_SetImageList($ListView, $hImage_Temp, 1)


7) Set State:  GUISetState()

 

Done the original way, putting "GUICtrlSetColor(-1, $COLOR_RED)" in the loop will make the entire Listview the same color, no matter what.


Done the "correct way", I can put in a conditional and, using the same command, choose WHICH line gets which color.  In other words, I now seem to have "complete" control over my Listview.   


I hope that this helps someone, lord knows I could have used this information a long time ago!  It'll be interesting to see what else works now!
Note that the Styles chosen can have a rather dramatic affect on your listviews, so make sure that you play with them to see what they do...

Link to comment
Share on other sites

  • Moderators

I don't see a question. Is there a reason you posted this in "General Help and Support" rather than "Examples"?

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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