Jump to content

ListView and Retrieving Specific Text


Recommended Posts

Well i'm working on this script that will in the end be a User-Friendly application that will update an XML/INI file for a soon to be catalog. I've been doing pretty good on my own and with the help file/forum topics. Unfortunately i've run into a rut and have fried myself on this. The problem i'm having is being able to pick out text from a specific cell in a listview. Basically I want to be able to click the row that I want removed, click the "Remove" button and have it and read the "Item Number" column on the selected row and pick up that cell. Only reason I need this to happen is because the Ini file section keys are id'd by the Item Number var and therefore I can simply use a IniDelete to remove the key.

Well here is the script if you need a look. Hit me with a hint!

;;;;;;;;;;;;;;;;;;;
;; INCLUDE FILES;;
;;;;;;;;;;;;;;;;;;;

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>


 ;;;;;;;;;;;;;;;
;; VARIABLES;;
;;;;;;;;;;;;;;;

$MidScreenX = @DesktopWidth / 2
$MidScreenY = @DesktopHeight / 2

Load_Window()
;Sleep(2000)
GUIDelete()
Edit_Catalog_Window()




 ;;;;;;;;;;;;;;;;;;;;;;;;
;; LOAD WINDOW SCRIPT;;
;;;;;;;;;;;;;;;;;;;;;;;;

Func Load_Window()

$Main_Window = GUICreate("Website Utility", 500, 500, $MidScreenX - 250, $MidScreenY - 250, $WS_POPUPWINDOW)
$Main_Window_Picture = GUICtrlCreatePic(@ScriptDir & "\Images\Main2.jpg", 0, 0, 500, 500)
GUISetState(@SW_SHOW)
EndFunc




 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; EDIT CATALOG WINDOW SCRIPT;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func Edit_Catalog_Window()
    
    GUICreate("Website Utility - Catalog", 600, 600, $MidScreenX - 300, $MidScreenY - 300, $WS_POPUPWINDOW)
    GUISetBkColor(0x000000)
    
    
     ;;;;;;;;;;;;;
    ;; Buttons;;
;;;;;;;;;;;;;
    
    $Add_Button = GUICtrlCreateButton("A D D !", 0, 560, 200, 20)
    $Edit_Button = GUICtrlCreateButton("E D I T !", 200, 560, 200, 20)
    $Remove_Button = GUICtrlCreateButton("R E M O V E !", 400, 560, 200, 20)
    $Exit_Button = GUICtrlCreateButton("E X I T !", 0, 580, 600, 20)
    
     ;;;;;;;;;;;;;;;
    ;; List View;;
;;;;;;;;;;;;;;;
    
    $Name = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Name")
    $Description = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Description")
    $Weight = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Weight")
    $Width = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Width")
    $Height = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Height")
    $Length = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Length")
    $Number = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Number")
    $Price = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Price")
    $Image = IniReadSection(@ScriptDir & "\Catalog.ini", "Item Image")
    $Item_Box = GUICtrlCreateListView("Name|Description|Weight|Width|Height|Length|Number|Price|Image", 0, 0, 600, 560, $LVS_SORTASCENDING)
    For $i = 1 To $Name[0][0]
        For $i = 1 To $Description[0][0]
            For $i = 1 To $Weight[0][0]
                For $i = 1 To $Width[0][0]
                    For $i = 1 To $Height[0][0]
                        For $i = 1 To $Length[0][0]
                            For $i = 1 To $Number[0][0]
                                For $i = 1 To $Price[0][0]
                                    For $i = 1 To $Image[0][0]
                                        GUICtrlCreateListViewItem($Name[$i][1] & "|" & $Description[$i][1] & "|" & $Weight[$i][1] & "|" & $Width[$i][1] & "|" & $Height[$i][1] & "|" & $Length[$i][1] & "|" & $Number[$i][1] & "|$" & $Price[$i][1] & "|" & $Image[$i][1], $Item_Box)
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
        Next
    Next
    GUICtrlSetBkColor($Add_Button, 0x000000)
    GUICtrlSetBkColor($Edit_Button, 0x000000)
    GUICtrlSetBkColor($Remove_Button, 0x000000)
    GUICtrlSetBkColor($Exit_Button, 0x000000)
    GUICtrlSetBkColor($Item_Box, 0x000000)
    GUICtrlSetColor($Add_Button, 0xff6600)
    GUICtrlSetColor($Edit_Button, 0xff6600)
    GUICtrlSetColor($Remove_Button, 0xff6600)
    GUICtrlSetColor($Exit_Button, 0xff6600)
    GUICtrlSetColor($Item_Box, 0xff6600)
    GUISetState(@SW_SHOW)
    
    While 1
        $msg = GUIGetMsg()
        Select
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Load_Window()
;           Sleep(2000)
            Exit
        Case $msg = $Add_Button
            GUIDelete()
            Add_Item_Window()
            Exit
        Case $msg = $Edit_Button
            GUIDelete()
            Edit_Item_Window()
            Exit
        Case $msg = $Remove_Button
            MsgBox(0, "listview", GUICtrlRead(GUICtrlRead($Name[0][1])), 2)
        Case $msg = $Exit_Button
            Exit
        EndSelect
    WEnd
    
EndFunc

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ADD ITEM WINDOW SCRIPT;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func Add_Item_Window()
    
    GUICreate("Website Utility - Add an Item.", 500, 600, $MidScreenX - 250, $MidScreenY - 300, $WS_POPUPWINDOW)
    
    GUISetBKColor(0x000000)
    
     ;;;;;;;;;;;;
    ;; Labels;;
;;;;;;;;;;;;
    
    $IN = GUICtrlCreateLabel("Item Name:", 5, 5)
    $ID = GUICtrlCreateLabel("Item Description:", 5, 25)
    $IDD = GUICtrlCreateLabel("Item Dimensions -", 5, 185)
    $IW = GUICtrlCreateLabel("Item Weight:", 5, 205)
    $IWI = GUICtrlCreateLabel("Item Width:", 5, 225)
    $IH = GUICtrlCreateLabel("Item Height:", 5, 245)
    $IL = GUICtrlCreateLabel("Item Length:", 5, 265)
    $INU = GUICtrlCreateLabel("Item Number:", 5, 505)
    $IP = GUICtrlCreateLabel("Item Price:", 5, 525)
    $I = GUICtrlCreateLabel("$", 100, 525)
    $II = GUICtrlCreateLabel("Item Image:", 5, 545)
    GUICtrlSetColor($IN, 0xff6600)
    GUICtrlSetColor($ID, 0xff6600)
    GUICtrlSetColor($IDD, 0xff6600)
    GUICtrlSetColor($IW, 0xff6600)
    GUICtrlSetColor($IWI, 0xff6600)
    GUICtrlSetColor($IH, 0xff6600)
    GUICtrlSetColor($IL, 0xff6600)
    GUICtrlSetColor($INU, 0xff6600)
    GUICtrlSetColor($IP, 0xff6600)
    GUICtrlSetColor($I, 0xff6600)
    GUICtrlSetColor($II, 0xff6600)
    
     ;;;;;;;;;;;;
    ;; Inputs;;
;;;;;;;;;;;;
    
    $Name = GUICtrlCreateInput("", 100, 5, 395, 20)
    $Description = GUICtrlCreateInput("", 100, 25, 395, 120, $ES_MULTILINE)
    $Weight = GUICtrlCreateInput("", 100, 205, 395, 20)
    $Width = GUICtrlCreateInput("", 100, 225, 395, 20)
    $Height = GUICtrlCreateInput("", 100, 245, 395, 20)
    $Length = GUICtrlCreateInput("", 100, 265, 395, 20)
    $Number = GUICtrlCreateInput("", 100, 500, 395, 20)
    $Price = GUICtrlCreateInput("", 110, 520, 330, 20)
    $Image = GUICtrlCreateInput("", 100, 540, 340, 20)
    
     ;;;;;;;;;;;;;
    ;; Buttons;;
;;;;;;;;;;;;;
    
    $Browse_Images = GUICtrlCreateButton("B r o w s e", 440, 540, 60, 20)
    $Add_Button = GUICtrlCreateButton("A D D !", 0, 560, 500, 20)
    $Cancel_Button = GUICtrlCreateButton("C A N C E L !", 0, 580, 500, 20)
    GUICtrlSetBkColor($Cancel_Button, 0x000000)
    GUICtrlSetBkColor($Browse_Images, 0x000000)
    GUICtrlSetBkColor($Add_Button, 0x000000)
    GUICtrlSetColor($Name, 0xff6600)
    GUICtrlSetColor($Description, 0xff6600)
    GUICtrlSetColor($Weight, 0xff6600)
    GUICtrlSetColor($Width, 0xff6600)
    GUICtrlSetColor($Height, 0xff6600)
    GUICtrlSetColor($Length, 0xff6600)
    GUICtrlSetColor($Number, 0xff6600)
    GUICtrlSetColor($Price, 0xff6600)
    GUICtrlSetColor($Image, 0xff6600)
    GUICtrlSetColor($Cancel_Button, 0xff6600)
    GUICtrlSetColor($Browse_Images, 0xff6600)
    GUICtrlSetColor($Add_Button, 0xff6600)
    
     ;;;;;;;;;;;;;;;;;
    ;; Ini Holders;;
;;;;;;;;;;;;;;;;;
            
    GUISetState(@SW_SHOW)
    
    While 1
        $msg = GUIGetMsg()
        Select
        Case $msg = $Browse_Images
            $Opened_Image = FileOpenDialog("Open Image File", @ScriptDir & "\", "Images (*.jpg;*.bmp;*.gif)", 1 + 4 )
            GUICtrlSetData($Image, $Opened_Image)
        Case $msg = $Add_Button
            $Name_Holder = GUICtrlRead($Name)
            $Description_Holder = GUICtrlRead($Description)
            $Weight_Holder = GUICtrlRead($Weight)
            $Width_Holder = GUICtrlRead($Width)
            $Height_Holder = GUICtrlRead($Height)
            $Length_Holder = GUICtrlRead($Length)
            $Number_Holder = GUICtrlRead($Number)
            $Price_Holder = GUICtrlRead($Price)
            $Image_Holder = GUICtrlRead($Image)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Name", $Number_Holder, $Name_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Description", $Number_Holder, $Description_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Weight", $Number_Holder, $Weight_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Width", $Number_Holder, $Width_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Height", $Number_Holder, $Height_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Length", $Number_Holder, $Length_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Number", $Number_Holder, $Number_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Price", $Number_Holder, $Price_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Image", $Number_Holder, $Image_Holder)
            GUIDelete()
            Edit_Catalog_Window()
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Edit_Catalog_Window()
            Exit
        Case $msg = $Cancel_Button
            GUIDelete()
            Edit_Catalog_Window()
            Exit
        EndSelect
    WEnd
    
EndFunc





 ;;;;;;;;;;;;;;;;;;;;;;
;; EDIT ITEM WINDOW;;
;;;;;;;;;;;;;;;;;;;;;;

Func Edit_Item_Window()
    
    GUICreate("Website Utility - Edit an Item.", 500, 600, $MidScreenX - 250, $MidScreenY - 300, $WS_POPUPWINDOW)
    
    GUISetBKColor(0x000000)
    
     ;;;;;;;;;;;;
    ;; Labels;;
;;;;;;;;;;;;
    
    $IN = GUICtrlCreateLabel("Item Name:", 5, 5)
    $ID = GUICtrlCreateLabel("Item Description:", 5, 25)
    $IDD = GUICtrlCreateLabel("Item Dimensions -", 5, 185)
    $IW = GUICtrlCreateLabel("Item Weight:", 5, 205)
    $IWI = GUICtrlCreateLabel("Item Width:", 5, 225)
    $IH = GUICtrlCreateLabel("Item Height:", 5, 245)
    $IL = GUICtrlCreateLabel("Item Length:", 5, 265)
    $INU = GUICtrlCreateLabel("Item Number:", 5, 505)
    $IP = GUICtrlCreateLabel("Item Price:", 5, 525)
    $I = GUICtrlCreateLabel("$", 100, 525)
    $II = GUICtrlCreateLabel("Item Image:", 5, 545)
    GUICtrlSetColor($IN, 0xff6600)
    GUICtrlSetColor($ID, 0xff6600)
    GUICtrlSetColor($IDD, 0xff6600)
    GUICtrlSetColor($IW, 0xff6600)
    GUICtrlSetColor($IWI, 0xff6600)
    GUICtrlSetColor($IH, 0xff6600)
    GUICtrlSetColor($IL, 0xff6600)
    GUICtrlSetColor($INU, 0xff6600)
    GUICtrlSetColor($IP, 0xff6600)
    GUICtrlSetColor($I, 0xff6600)
    GUICtrlSetColor($II, 0xff6600)
    
     ;;;;;;;;;;;;
    ;; Inputs;;
;;;;;;;;;;;;
    
    $Name = GUICtrlCreateInput("", 100, 5, 395, 20)
    $Description = GUICtrlCreateInput("", 100, 25, 395, 120, $ES_MULTILINE)
    $Weight = GUICtrlCreateInput("", 100, 205, 395, 20)
    $Width = GUICtrlCreateInput("", 100, 225, 395, 20)
    $Height = GUICtrlCreateInput("", 100, 245, 395, 20)
    $Length = GUICtrlCreateInput("", 100, 265, 395, 20)
    $Number = GUICtrlCreateInput("", 100, 505, 395, 20)
    $Price = GUICtrlCreateInput("", 110, 525, 330, 20)
    $Image = GUICtrlCreateInput("", 100, 545, 340, 20)
    
     ;;;;;;;;;;;;;
    ;; Buttons;;
;;;;;;;;;;;;;
    
    $Browse_Images = GUICtrlCreateButton("B R O W S E", 440, 545, 60, 20)
    $Done_Button = GUICtrlCreateButton("D O N E !", 0, 560, 500, 20)
    $Cancel_Button = GUICtrlCreateButton("C A N C E L !", 0, 580, 500, 20)
    GUICtrlSetBkColor($Cancel_Button, 0x000000)
    GUICtrlSetBkColor($Browse_Images, 0x000000)
    GUICtrlSetBkColor($Done_Button, 0x000000)
    GUICtrlSetColor($Name, 0xff6600)
    GUICtrlSetColor($Description, 0xff6600)
    GUICtrlSetColor($Weight, 0xff6600)
    GUICtrlSetColor($Width, 0xff6600)
    GUICtrlSetColor($Height, 0xff6600)
    GUICtrlSetColor($Length, 0xff6600)
    GUICtrlSetColor($Number, 0xff6600)
    GUICtrlSetColor($Price, 0xff6600)
    GUICtrlSetColor($Image, 0xff6600)
    GUICtrlSetColor($Cancel_Button, 0xff6600)
    GUICtrlSetColor($Browse_Images, 0xff6600)
    GUICtrlSetColor($Done_Button, 0xff6600)
    
     ;;;;;;;;;;;;;;;;;
    ;; Ini Holders;;
;;;;;;;;;;;;;;;;;
            
    GUISetState(@SW_SHOW)
    
    While 1
        $msg = GUIGetMsg()
        Select
        Case $msg = $Browse_Images
            $Opened_Image = FileOpenDialog("Open Image File", @ScriptDir & "\", "Images (*.jpg;*.bmp;*.gif)", 1 + 4 )
            GUICtrlSetData($Image, $Opened_Image)
        Case $msg = $Done_Button
            $Name_Holder = GUICtrlRead($Name)
            $Description_Holder = GUICtrlRead($Description)
            $Weight_Holder = GUICtrlRead($Weight)
            $Width_Holder = GUICtrlRead($Width)
            $Height_Holder = GUICtrlRead($Height)
            $Length_Holder = GUICtrlRead($Length)
            $Number_Holder = GUICtrlRead($Number)
            $Price_Holder = GUICtrlRead($Price)
            $Image_Holder = GUICtrlRead($Image)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Name", $Number_Holder, $Name_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Description", $Number_Holder, $Description_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Weight", $Number_Holder, $Weight_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Width", $Number_Holder, $Width_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Height", $Number_Holder, $Height_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Length", $Number_Holder, $Length_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Number", $Number_Holder, $Number_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Price", $Number_Holder, $Price_Holder)
            IniWrite(@ScriptDir & "\Catalog.ini", "Item Image", $Number_Holder, $Image_Holder)
            GUIDelete()
            Edit_Catalog_Window()
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Edit_Catalog_Window()
            Exit
        Case $msg = $Cancel_Button
            GUIDelete()
            Edit_Catalog_Window()
            Exit
        EndSelect
    WEnd
    
EndFunc
Link to comment
Share on other sites

Each ListViewItem is identified by a controlID when it's created. In your script it appears that the first ListViewItem is control ID 8. I think the ListView itself is 7.

It seems like you might just need to find a way to reference that selected control ID with the number.

For instance you could create a new INI section when the script is run. Call it something like "ControlIDLookup":

[ControlIDLookup]

controlID=number

Everytime you build the ListViewItem, use:

$NextListViewItem = $Name[$i][1] & "|" & $Description[$i][1] & "|" & $Weight[$i][1] & "|" & $Width[$i][1] & "|" & $Height[$i][1] & "|" & $Length[$i][1] & "|" & $Number[$i][1] & "|$" & $Price[$i][1] & "|" & $Image[$i][1]

IniWrite(@ScriptDir & "\Catalog.ini", "ControlIDLookup", GUICtrlCreateListViewItem($NextListViewItem, $Item_Box), $Number[$i][1])
(this adds controlID=number to the ControlIDLookup section of your INI file)

Then, when you need to find the value of the number in the currently selected ListViewItem, you would just use:

$SelectedNumber = IniRead(@ScriptDir & "\Catalog.ini", "ControlIDLookup", GUICtrlRead($Item_Box), "")

Use GUICtrlDelete(GUICtrlRead($Item_Box)) to delete the ListViewItem itself, then to delete the ControlID=number key in the INI, then the above $SelectedNumber to delete all occurrences of that number in the INI file.

Delete the [ControlIDLookup] section before the script is started and maybe even before it exits.

My idea of how your INI is set up is based off this:

[Item Name]
411=Name1
562=Name2

[Item Description]
411=This is name1 desc
562=This is name2 desc

[Item Weight]
411=175
562=150

[Item Width]
411=36
562=56

[Item Height]
411=67
562=89

[Item Length]
411=677
562=433

[Item Number]
411=1
562=2

[Item Price]
411=54.32
562=45.64

[Item Image]
411=C:\temp\image1.jpg
562=C:\temp\yea.jpg

Is this how your's is?

Edited by MrMitchell
Link to comment
Share on other sites

look at the guilistview UDF in the help file, it will solve most, if not all, your listview issues.

in specific, look at the following for general editing of listviews:

_GUICtrlListView_GetSelectionMark()

_GUICtrlListView_GetItemText()

_GUICtrlListView_SetItemText ()

_GUICtrlListView_DeleteItem()

[u]You can download my projects at:[/u] Pulsar Software
Link to comment
Share on other sites

Look here http://www.autoitscript.com/forum/index.ph...mp;#entry644312 for graphics style etc..

Getting the selected item's row and column can go this way:

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

Dim $hGUI, $hListView, $hImageList
Dim $ListView, $Button
Dim $avItem[2] = [-1,-1]

$hGUI = GUICreate('Test', 250, 300)
$ListView = GUICtrlCreateListView('1|   2|   3|   4|   5|   6|   7|   8|   9', 0, 0, 250, 250, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES))
$hListView = GUICtrlGetHandle(-1)
$Button = GUICtrlCreateButton('R e m o v e', 85, 270, 80, 25)

For $i = 1 To 10
    GUICtrlCreateListViewItem('1|2  |3  |4  |5  |6  |7  |8  |9  ', $ListView)
Next

$hImageList = _GUIImageList_Create(8, 8)
_GUIImageList_Add($hImageList, _GUICtrlListView_CreateSolidBitMap($hListView, 0x0078BE, 8, 8))
_GUICtrlListView_SetImageList($hListView, $hImageList, 1)

GUISetState()
GUIRegisterMsg($WM_NOTIFY, 'OnNotify')

While 1
    Switch GUIGetMsg()
        Case $Button
            If $avItem[0] > -1 Then
                _GUICtrlListView_SetItemText($hListView, $avItem[0], '', $avItem[1])
            EndIf
            
        Case -3
            ExitLoop
    EndSwitch
    Sleep(30)
WEnd

_GUICtrlListView_Destroy($hListView)
_GUIImageList_Destroy($hImageList)
GUIDelete()

Func OnNotify($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
    Local $iCol, $iRow
    Local $avArray

    If DllStructGetData($tInfo, 'hwndFrom') = $hListView And _
        DllStructGetData($tInfo, 'Code') = $NM_CLICK Then   
        _GUICtrlListView_SetItemImage($hListView, $avItem[0], -1, $avItem[1])
        
        $avArray = _GUICtrlListView_SubItemHitTest($hListView)
        If $avArray[0] > -1 Then
            $avItem[0] = $avArray[0]
            $avItem[1] = $avArray[1]
            _GUICtrlListView_SetItemImage($hListView, $avArray[0], 0, $avArray[1])
        EndIf
    EndIf
    
    Return $GUI_RUNDEFMSG
EndFunc
Edited by Authenticity
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...