Jump to content

how to create image list or icon on the listview


Recommended Posts

hi to all. i have this listview where data came from a text file. my problem is, i want to have an image or icon to the listview data but i can't do it right. i have read on the helpfile but still could not make it work. here is what i have right now...

anyone can help me. greatly appreciate any ideas from you guys...thanks...

text file....

192.168.1.25 | ACER1 | user1|Denver McCain|12:24:51 PM|10/05/2009
192.168.1.26 | ACER2 | user1|Denver McCain|12:25:19 PM|10/05/2009
192.168.1.35 | ACER3 | user1|Denver McCain|2:14:33 PM|10/05/2009
192.168.1.37 | ACER4 | user1|Denver McCain|2:16:32 PM|10/05/2009
192.168.1.38 | ACER5 | user1|Denver McCain|2:17:38 PM|10/05/2009
192.168.1.40 | ACER6 | user1|Denver McCain|2:19:25 PM|10/05/2009

script...

#include <WindowsConstants.au3>
#Include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#Include <GuiImageList.au3>


Opt("GUIOnEventMode", 1)

Listview()

While 1
    Sleep(200)
Wend

Func Listview()
        
    $ListviewGUILast = GUICreate("Log ", 547, 150, -1, -1, $WS_SYSMENU)  
    $Listview1 = GUICtrlCreateListView("", 10, 10, 520, 100, $LVS_REPORT)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
        
    $hImage = _GUIImageList_Create(11, 11)
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($Listview1, 0xFF0000, 11, 11))    
    _GUICtrlListView_AddColumn ($ListView1, "IP Address", 120, 2)
    _GUICtrlListView_AddColumn ($ListView1, "Computer Name", 100, 2)    
    _GUICtrlListView_AddColumn ($ListView1, "user", 100, 2) 
    _GUICtrlListView_AddColumn ($ListView1, "name", 100, 2)
     _GUICtrlListView_AddColumn ($ListView1, "date", 100, 2)
     _GUICtrlListView_SetImageList($Listview1, $hImage, 1)
    $inilogarray = StringSplit(FileRead(@DesktopDir & "\log.txt"), @CRLF, 1)    
    
    For $i = 1 To $inilogarray[0] - 1       
        GUICtrlCreateListViewItem($inilogarray[$i], $Listview1)
        
    Next
    
    GUISetState(@SW_SHOW)   
    
EndFunc

Func Close()    
;GUIDelete()
    Exit
EndFunc
Link to comment
Share on other sites

Hi, you have to set the item to have the image. Try this:

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

$test_text = "192.168.1.25 | ACER1 | user1 | Denver McCain | 12:24:51 PM | 10/05/2009" & @CRLF & _
        "192.168.1.26 | ACER2 | user1 | Denver McCain | 12:25:19 PM | 10/05/2009" & @CRLF & _
        "192.168.1.35 | ACER3 | user1 | Denver McCain | 2:14:33 PM | 10/05/2009" & @CRLF & _
        "192.168.1.37 | ACER4 | user1 | Denver McCain | 2:16:32 PM | 10/05/2009" & @CRLF & _
        "192.168.1.38 | ACER5 | user1 | Denver McCain | 2:17:38 PM | 10/05/2009" & @CRLF & _
        "192.168.1.40 | ACER6 | user1 | Denver McCain | 2:19:25 PM | 10/05/2009"

Opt("GUIOnEventMode", 1)

Listview()

While 1
    Sleep(200)
WEnd

Func Listview()

    $ListviewGUILast = GUICreate("Log ", 547, 150, -1, -1, $WS_SYSMENU)
    $Listview1 = GUICtrlCreateListView("", 10, 10, 520, 100, $LVS_REPORT)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Close")

    $hImage = _GUIImageList_Create()
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($Listview1, 0xFF0000, 16, 16))
    _GUICtrlListView_AddColumn($Listview1, "IP Address", 120, 2)
    _GUICtrlListView_AddColumn($Listview1, "Computer Name", 100, 2)
    _GUICtrlListView_AddColumn($Listview1, "user", 100, 2)
    _GUICtrlListView_AddColumn($Listview1, "name", 100, 2)
    _GUICtrlListView_AddColumn($Listview1, "date", 100, 2)
    _GUICtrlListView_SetImageList($Listview1, $hImage, 1)
;$inilogarray = StringSplit(FileRead(@DesktopDir & "\log.txt"), @CRLF, 1)
    $inilogarray = StringSplit($test_text, @CRLF, 1)

    For $i = 1 To UBound($inilogarray) - 1
        $split2 = StringSplit($inilogarray[$i], " | ", 1)
        $indx = _GUICtrlListView_AddItem($Listview1, $split2[1], 0)
    ;ConsoleWrite($i & @CRLF)
        For $x = 2 To UBound($split2) - 1
        ;ConsoleWrite(@TAB & $x & @CRLF)
            _GUICtrlListView_AddSubItem($Listview1, $indx, $split2[$x], $x - 1, 0)
        Next
    Next

    GUISetState(@SW_SHOW)

EndFunc  ;==>Listview

Func Close()
;GUIDelete()
    Exit
EndFunc  ;==>Close

Cheers,

Brett

Link to comment
Share on other sites

thnks brett. thats great. but i want to read directly from the text file instead of putting it on the script......i have put the _GUICTLRLISTVIEW_Additem instead of GUICTLRCreateListviewitem but could not put it right into the listview...

see the edited script...appreciate if you can help me split the data so that it will match exactly on the listview...

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



Opt("GUIOnEventMode", 1)

Listview()

While 1
    Sleep(200)
WEnd

Func Listview()

    $ListviewGUILast = GUICreate("Log ", 547, 150, -1, -1, $WS_SYSMENU)
    $Listview1 = GUICtrlCreateListView("", 10, 10, 520, 100, $LVS_REPORT)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Close")

    $hImage = _GUIImageList_Create()
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($Listview1, 0xFF0000, 16, 16))
    _GUICtrlListView_AddColumn($Listview1, "IP Address", 120, 2)
    _GUICtrlListView_AddColumn($Listview1, "Computer Name", 100, 2)
    _GUICtrlListView_AddColumn($Listview1, "user", 100, 2)
    _GUICtrlListView_AddColumn($Listview1, "name", 100, 2)
    _GUICtrlListView_AddColumn($Listview1, "date", 100, 2)
    _GUICtrlListView_SetImageList($Listview1, $hImage, 1)
$inilogarray = StringSplit(FileRead(@DesktopDir & "\log.txt"), @CRLF, 1)   
        
    $hImage = _GUIImageList_Create(11, 11)
    _GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($Listview1, 0xFF0000, 11, 11))    
    _GUICtrlListView_AddColumn ($ListView1, "IP Address", 120, 2)
    _GUICtrlListView_AddColumn ($ListView1, "Computer Name", 100, 2)     
    _GUICtrlListView_AddColumn ($ListView1, "user", 100, 2) 
    _GUICtrlListView_AddColumn ($ListView1, "name", 100, 2)
     _GUICtrlListView_AddColumn ($ListView1, "date", 100, 2)
     _GUICtrlListView_SetImageList($Listview1, $hImage, 1)
    $inilogarray = StringSplit(FileRead(@DesktopDir & "\log.txt"), @CRLF, 1)    
    
    For $i = 1 To $inilogarray[0] - 1        
       ;GUICtrlCreateListViewItem($inilogarray[$i], $Listview1)
        _GUICtrlListView_AddItem($Listview1, $inilogarray[$i], 0)
    Next
    
    GUISetState(@SW_SHOW)   
    
EndFunc

Func Close()    

    Exit
EndFunc
Link to comment
Share on other sites

Okay listen up cause I will only say this once!

This is one row in your list view.

192.168.1.25 | ACER1 | user1|Denver McCain|12:24:51 PM|10/05/2009

Some are deliminated by " | " and others by "|". You want to make them all the same.

I would replace " | " with |, as it is the "easiest" option.

The next step is to add the first column in the row. That returns the index, which we can use for adding the sub items (the other columns). Basically just like I did with my loop was this:

1) Split the file line by line.

2) For each line in the file do this:

3) Split each column.

4) Add first, then subsequent columns.

Cheers,

Brett

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