am632

Read value from listview

12 posts in this topic

Hi,

I have managed to put together a script which lists fixed and removable drives using bits of code from other examples around the forum and I have it working as I want, but I cant work out how to read a value from the listview, please can anyone help? heres the code...

#include <GUIConstantsEx.au3>
#include <ListviewConstants.au3>
#include <array.au3>
#include <GUIListView.au3>

Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 1)

Local $Removable = "YES", $Drive_Set, $var

_ListDiskInfo()

$myinfos = _GetDriveInfos()

$Main = GuiCreate("List Disks", 800, 400)
GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseApplication")

$Button = GUICtrlCreateButton("TEST",100,300,100,50)
GUICtrlSetOnEvent(-1,"_GetSelectedItem")

$listview = GUICtrlCreateListView("volume|label|file system|size (gb)|used (gb)|free (gb)|caption|id|dirty", 25, 25, 750, 200)
GUICtrlSetStyle($listview,Default,$LVS_EX_CHECKBOXES)

For $i = 0 to $Drive_Set[0]-1

;$percentocc = Round(($myinfos[$i][2]-$myinfos[$i][3])*100/$myinfos[$i][2], 1)

GUICtrlCreateListViewItem(StringUpper($myinfos[$i][0]) &"|"& $myinfos[$i][6] &"|"& $myinfos[$i][7] &"|"& $myinfos[$i][4] &"|"& $myinfos[$i][2]-$myinfos[$i][3] &"|"& $myinfos[$i][3] &"|"& $myinfos[$i][1] &"|"& $myinfos[$i][5]&"|"&$myinfos[$i][8], $listview)
Next

GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 0, 60)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 1, 100)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 2, 70)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 3, 65)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 4, 65)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 5, 65)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 6, 130)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 7, 130)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 8, 80)
GUISetState()



;Functions
#region ;List Disk Info
Func _ListDiskInfo()
    $Drive_Set = ""
    $DriveList = ""
    If $Removable = "YES" Then
        $Drive_Set = DriveGetDrive("ALL")
        For $i = 1 To $Drive_Set[0]
            $DriveType = DriveGetType($Drive_Set[$i])
            $DriveStatus = DriveStatus($Drive_Set[$i])
            If $DriveType = "Fixed" Or ($DriveType = "Removable" And $DriveStatus <> "NOTREADY") Or $DriveType = "RAMDisk" Then
                $DriveList = $DriveList & $Drive_Set[$i] & "|"
                $Drive_Set[$i] = StringUpper($Drive_Set[$i])
            EndIf
        Next
        $Drive_Set = StringSplit(StringTrimRight($DriveList, 1), "|", 1)
    Else
        $Drive_Set = DriveGetDrive("FIXED")
    EndIf
EndFunc
#EndRegion

#region ;Get Drive Infos
Func _GetDriveInfos()
Dim $specifs[$Drive_Set[0]][9]
For $i = 1 to $Drive_Set[0]
Dim $Services = ObjGet('winmgmts:\\.\root\cimv2')
Dim $DiskDrives = $Services.ExecQuery("Select Caption, DeviceID From Win32_DiskDrive")
For $DiskDrive In $DiskDrives
Dim $DiskPartitions = $Services.ExecQuery("Associators of {Win32_DiskDrive.DeviceID='" & _
$DiskDrive.DeviceID & "'} Where AssocClass = Win32_DiskDriveToDiskPartition")
For $DiskPartition In $DiskPartitions
Dim $LogicalDisks = $Services.ExecQuery ("Associators of {Win32_DiskPartition.DeviceID='" & _
$DiskPartition.DeviceID & "'} Where AssocClass = Win32_LogicalDiskToPartition")
For $LogicalDisk In $LogicalDisks
If $LogicalDisk.DeviceID = $Drive_Set[$i] Then
$specifs[$i-1][0] = $Drive_Set[$i]
$specifs[$i-1][1] = $DiskDrive.Caption
$specifs[$i-1][2] = Round(DriveSpaceTotal($Drive_Set[$i])/1024,2)
$specifs[$i-1][3] = Round(DriveSpaceFree($Drive_Set[$i])/1024,2)
$specifs[$i-1][4] = Round($LogicalDisk.Size/1000000000)
$specifs[$i-1][5] = $DiskPartition.DeviceID
$specifs[$i-1][6] = $LogicalDisk.VolumeName
$specifs[$i-1][7] = $LogicalDisk.FileSystem
$specifs[$i-1][8] = $LogicalDisk.VolumeDirty

EndIf
Next
Next
Next
Next
Return $specifs
EndFunc
#endregion

#region ;Exit Function

Func _CloseApplication()
    If @GUI_CtrlId = $GUI_EVENT_CLOSE Then _Exit_Command()
EndFunc   ;==>_CloseApplication

Func _Exit_Command()
    Sleep(500)
    Exit
EndFunc
#endregion

#region ;Get Selected Item
Func _GetSelectedItem()
    $var = TRUE
EndFunc

Func _GetSelectedItem1()
    Local $SelectedItem = _GUICtrlListView_GetItemTextArray
    MsgBox(0,"",$SelectedItem)
    $var = False
EndFunc
#EndRegion


While GUIGetMsg()<>$GUI_EVENT_CLOSE
    If $var = TRUE Then
        _GetSelectedItem1()
    EndIf

WEnd

The _GetSelectedItem1 function is the 1 I need help with, you can see its not finished, I think I need to use the _GUICtrlListView_GetItemTextArray function but im not exactly sure, does this seem correct?

So the ides is if I click a drive in the list view and click the 'TEST' button it will show the value of the volume lable for example, or another value like drive letter etc, am I on the right track here or is there another way to do it?

Thanks

Share this post


Link to post
Share on other sites



Replace your func _GetSelectedItem1 with this:

Func _GetSelectedItem1()
    Local $SelectedItemIndex = -1
    Local $GetCount = _GUICtrlListView_GetItemCount($listview)
    For $a = 0 To $GetCount - 1
        If _GUICtrlListView_GetItemChecked($listview, $a) == True Then
            $SelectedItemIndex = $a
        EndIf
    Next
    If $SelectedItemIndex > -1 Then
        $LineInfo = _GUICtrlListView_GetItemTextArray($listview, $SelectedItemIndex)
        _ArrayDisplay($LineInfo)
    EndIf
    $var = False
EndFunc   ;==>_GetSelectedItem1

 For _GUICtrlListView_GetItemTextArray to work it needs to know which index you are interested in.  In order to get that you have to use _GUICtrlListView_GetItemChecked on each index of $listview to see if that line is checked.  In order to do that you need a total count of items in $listview, so _GUICtrlListView_GetItemCount is needed, then look at each index to find the one that is checked.  I set $SelectedItemIndex to -1 as default in case no boxes were clicked.  What will happen if more than 1 box is clicked is anyone's guess as I only have 1 internal drive at the moment to test with.

Ian


My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.

Share this post


Link to post
Share on other sites

Hi, Thanks a lot for the help this is excellent and thanks for explaining each function, I have just 1 question, If I wanted to get just 1 value - lets say the drive letter from the $LineInfo instead of displaying all info would I use the _ArrayExtract function to a variable which I could then display in a msgbox or something? im unfamiliar with these functions but I looked in the help file and couldn't see anything else which looked like it would do it.

Thanks

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Cant test script under my XP but if your getting array to display some part of it you can use

Msgbox(0,'title',$LineInfo[5]); so $LineInfo[5] is 6-th line from array display (6-th coz array count from 0 and not from 1)

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

Thanks very much, this works perfectly, Thanks both for your help

Share this post


Link to post
Share on other sites

Actually just to extend on this while the topic is fresh, is there a way to change the default checkboxes in the listview to radio buttons instead so only 1 is selected at a time? what I found is if more than 1 checkbox is selected the script still works but it uses the last checked item as the value to read, or would it be possible to automatically deselect a box if another is checked?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

am632,

How does that differ from the default single item selection when you create the ListView without any checkboxes? :huh:

M23

Edited by Melba23
Typo

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Hi,

I didnt realise how these functions work properly yet regards the listview as im still learning about them, I thought you needed to have a checkbox to be able to select an item, but based on what you just said I just checked the help file and realised If I change the _GUICtrlListView_GetItemChecked to _GUICtrlListView_GetItemSelected and remove the $LVS_EX_CHECKBOXES from the control style then I get the result I want, so thanks for helping me realise this :)

Share this post


Link to post
Share on other sites

am632,

Glad I could help. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Is there a tutorial somewhere that show the similarities & differences between 

lists

listviews

combo boxes ?

it takes me ages to figure out what i want to use and exactly what the benefits are ...

 


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

Skysnake,

My take on the 3 control types:

- ListBoxes are used to display simple lists with only a single item per line.

- ListViews are much more complex beasts which can display several separate sub-items on the same line and which offer significantly more user interaction

- Combos are entirely different beasts and are not designed to display data but to allow the user to choose an item from a simple list without having to waste GUI real estate showing the list permanently.

I hope that helps.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

nice summary. thx :)


Skysnake

Why is the snake in the sky?

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