Jump to content
Sign in to follow this  
GordonFreeman

ListView Sort + Editable

Recommended Posts

GordonFreeman

Hi guys!

A help plz. I need make a Listview with "Software Name" + "Link to Download". I want sort ascending. But i will give an option to user edit. I need some ideas. The initial "messy" code is:

_UptLV_1(@DesktopDir & "\TEST.txt")

Func _UptLV_1($sFile)

Local $sCountL = _FileCountLines($sFile)

Local $aFileI[$sCountL+1]

_FileReadToArray(@DesktopDir & "\" & "TEST.txt",$aFileI)

For $i = 1 to UBound($aFileI)

Global $aLVInfo[UBound($aFileI)][2]=[["",""]]

$Array = StringSplit($aFileI[$i-1], "|")

$aLVInfo[$i][0] = $Array[1]
$aLVInfo[$i][1] = $Array[2]

Next

EndFunc ;_UptLV_1()

I thought in one text file by columm (easy to do) but it will cause desync when sort.

And i cannot see the error in code

The text file called "TEST.txt" (Names are only to test/example)

Google|www.google.com
Yahoo|www.yahoo.com
Bing|www.bing.com
Gmail|www.gmail.com

My logic is fail today :/

Edited by GordonFreeman

Share this post


Link to post
Share on other sites
Rogue5099

This should set $aLVInfo to an array that reads $aLVInfo[$i][0] = Name and $aLVInfo[$i][1] = Site.  Now just plug it into your ListView.

#include <File.au3>
#include <Array.au3>

_UptLV_1(@DesktopDir & "\TEST.txt")

Func _UptLV_1($sFile)
    Local $aFileI

    _FileReadToArray($sFile, $aFileI)           ; Creates Array $aFileI

    Local $aLVInfo[UBound($aFileI)][2]          ; Sets $aLVInfo to 2-D Array

    For $i = 1 to UBound($aFileI) - 1
        $Array = StringSplit($aFileI[$i], "|")  ; Creates Array of each line from file
        $aLVInfo[$i][0] = $Array[1]
        $aLVInfo[$i][1] = $Array[2]
    Next

    _ArrayDisplay($aLVInfo)
EndFunc ;_UptLV_1()
Edited by Rogue5099

Share this post


Link to post
Share on other sites
kylomas

GF,

You don't need to fart around with splitting and interim arrays and such.  Your data is in a format to populate a listview like this...

#include <GUIConstantsEx.au3>
#include <file.au3>
#include <ListviewConstants.au3>

local $gui010   =   guicreate('Test LV')
local $lv010    =   guictrlcreatelistview('Software Name|Link to Download',20,20,200,200, -1, _
                        $LVS_EX_GRIDLINES)
                    guisetstate()

_UptLV_1(@DesktopDir & "\TEST.txt")

while 1
    switch guigetmsg()
        case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
wend

Func _UptLV_1($sFile)

    local $aFileI
    _FileReadToArray(@DesktopDir & "\" & "TEST.txt", $aFileI)

    For $i = 1 To UBound($aFileI) - 1
        GUICtrlCreateListViewItem($aFileI[$i],$lv010)
    Next

EndFunc   ;==>_UptLV_1

As for editing and sorting, search the forum.  There are many examples of both.

kylomas


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

GF,

I figured out how to get this freak to edit cols/sub-cols in place...

#include <GUIConstantsEx.au3>
#include <file.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>

Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)
Global $hEdit, $Item = -1, $SubItem = 0

local $gui010   =   guicreate('Test LV')
local $lv010    =   guictrlcreatelistview('Software Name|Link to Download',20,20,200,200, $LVS_REPORT, _
                        $LVS_EX_GRIDLINES)
local $hlv010   =   GUICtrlGetHandle($lv010)
                    guisetstate()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

_UptLV_1(@DesktopDir & "\TEST.txt")

while 1
    switch guigetmsg()
        case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
wend

Func _UptLV_1($sFile)

    local $aFileI
    _FileReadToArray(@DesktopDir & "\" & "TEST.txt", $aFileI)

    For $i = 1 To UBound($aFileI) - 1
        GUICtrlCreateListViewItem($aFileI[$i],$lv010)
    Next

EndFunc   ;==>_UptLV_1

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iCode

    $tNMHDR     = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom   = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode      = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hlv010
            Switch $iCode
                Case $NM_DBLCLK
                    Local $aHit = _GUICtrlListView_SubItemHitTest($hlv010)
                    If ($aHit[0] <> -1)  Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        Local $iSubItemText = _GUICtrlListView_GetItemText($hlv010, $Item, $SubItem)
                        Local $aRect = _GUICtrlListView_GetSubItemRect($hlv010, $Item, $SubItem)
                        if $aHit[1] <> 0 then
                            $hEdit = _GUICtrlEdit_Create($gui010, $iSubItemText, $aRect[0]+25, $aRect[1]+20, $aRect[2] - $aRect[0]-2, 20, $Style)
                        Else
                            local $w = _GUICtrlListView_GetColumnWidth($hlv010,0)
                            $hEdit = _GUICtrlEdit_Create($gui010, $iSubItemText, $aRect[0]+25, $aRect[1]+20, $w, 20, $Style)
                        EndIf
                        _WinAPI_SetFocus($hEdit)
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $iCode = BitShift($wParam, 16)

    Switch $lParam
        Case $hEdit
            Switch $iCode
                Case $EN_KILLFOCUS
                    Local $iText = _GUICtrlEdit_GetText($hEdit)
                    _GUICtrlListView_SetItemText($hlv010, $Item, $iText, $SubItem)
                    _WinAPI_DestroyWindow($hEdit)
                    $Item = -1
                    $SubItem = 0
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc

kylomas


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

...and with a sort...

#include <GUIConstantsEx.au3>
#include <file.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>

Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)
Global $hEdit, $Item = -1, $SubItem = 0

local $gui010   =   guicreate('Test LV')
local $lv010    =   guictrlcreatelistview('Software Name|Link to Download',20,20,200,200, $LVS_REPORT, _
                        $LVS_EX_GRIDLINES)
local $hlv010   =   GUICtrlGetHandle($lv010)
                    guisetstate()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

_UptLV_1(@DesktopDir & "\TEST.txt")

while 1
    switch guigetmsg()
        case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
wend

Func _UptLV_1($sFile)

    local $aFileI
    _FileReadToArray(@DesktopDir & "\" & "TEST.txt", $aFileI)

    For $i = 1 To UBound($aFileI) - 1
        GUICtrlCreateListViewItem($aFileI[$i],$lv010)
    Next

    local $aSRT[2] = [false,0]
    _guictrllistview_simplesort($hlv010,$aSRT,0)

EndFunc   ;==>_UptLV_1

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR, $hWndFrom, $iCode

    $tNMHDR     = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom   = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode      = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hlv010
            Switch $iCode
                Case $NM_DBLCLK
                    Local $aHit = _GUICtrlListView_SubItemHitTest($hlv010)
                    If ($aHit[0] <> -1)  Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        Local $iSubItemText = _GUICtrlListView_GetItemText($hlv010, $Item, $SubItem)
                        Local $aRect = _GUICtrlListView_GetSubItemRect($hlv010, $Item, $SubItem)
                        if $aHit[1] <> 0 then
                            $hEdit = _GUICtrlEdit_Create($gui010, $iSubItemText, $aRect[0]+25, $aRect[1]+20, $aRect[2] - $aRect[0]-2, 20, $Style)
                        Else
                            local $w = _GUICtrlListView_GetColumnWidth($hlv010,0)
                            $hEdit = _GUICtrlEdit_Create($gui010, $iSubItemText, $aRect[0]+25, $aRect[1]+20, $w, 20, $Style)
                        EndIf
                        _WinAPI_SetFocus($hEdit)
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $iCode = BitShift($wParam, 16)

    Switch $lParam
        Case $hEdit
            Switch $iCode
                Case $EN_KILLFOCUS
                    Local $iText = _GUICtrlEdit_GetText($hEdit)
                    _GUICtrlListView_SetItemText($hlv010, $Item, $iText, $SubItem)
                    _WinAPI_DestroyWindow($hEdit)
                    $Item = -1
                    $SubItem = 0
                    local $aSRT[2] = [false,0]
                    _guictrllistview_simplesort($hlv010,$aSRT,0)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc

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

GF,

M23 has a UDF in his sig that has a bunch of listview functionality, including what we just worked on...

kylomas


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
Rogue5099

GF,

 

I figured out how to get this freak to edit cols/sub-cols in place...

kylomas

You can edit the controls but it doesn't auto save. Wouldn't be a problem just to string search and replace though. What would also be better would be a hyperlink in the GUI. Going straight from file to list view is great since it's already separated withe a "|".

Just curious though, could you edit the list view control to be re-sized?

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  

×