Sign in to follow this  
Followers 0
adrianodj

GUICtrlCreateListView show the contents of a folder

9 posts in this topic

hi all

I have a listwiev and 3 buttons:

add file, run and deleteI

1 - I want the listview shows me the contents of a particular folder without having to give the add file button

2 - with the delete button I want to delete the file I have selectioned

thanks

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <GuiListView.au3>
#include <Constants.au3>
#include <Misc.au3>
#include <GuiToolTip.au3>
#include <Array.au3>

Global $ITEMID, $Lista1
Global $CABECERA = "Archivo                 |Ruta                     "

$Ancho = 333
$Alto = 300
#Region ### START Koda GUI section ###
$Form1 = GUICreate("Aplicacion", $Ancho, $Alto, 318, 184, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE))
;;;; LISTA
$Lista1 = GUICtrlCreateListView($CABECERA, 0, 0, $Ancho, 257)
GUICtrlSetState($Lista1, $GUI_DROPACCEPTED)
_GUICtrlListView_SetExtendedListViewStyle($Lista1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_INFOTIP))

;;;; BOTONES
$BTNAGREGAR = GUICtrlCreateButton("Add file", 15, 265, 90, 25)
GUICtrlSetTip(-1, 'Añadir archivos ')
$BTNEMPEZAR = GUICtrlCreateButton("Run", 123, 265, 90, 25)
GUICtrlSetTip(-1, 'Ejecutar')
$BTNBORARSELECT = GUICtrlCreateButton("Delete", 230, 265, 90, 25)
GUICtrlSetTip(-1, 'Borrar')

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
   $msg = GUIGetMsg()
   Switch $msg
      Case $GUI_EVENT_CLOSE
         Exit
      Case $BTNAGREGAR
         _Agregararchivo()
      Case $BTNEMPEZAR
         _SelectArchivo()
      Case $BTNBORARSELECT
         _BorrarSeleccionado()
   EndSwitch
WEnd



Func _Agregararchivo()
   $Carpeta = FileOpenDialog('Add file, '', 'All (*)', 5, '', $Form1)  ;<-------------???????????????? I want the listview shows me the contents of a particular folder without having to give the add file button       C:\Pruebas\ .... .exe

   $Filediv = StringSplit($Carpeta, '|')
   If $Filediv[0] <> 1 Then ;
      For $N = 2 To $Filediv[0]
         $ITEMID = _GUICtrlListView_AddItem($Lista1, StringTrimRight($Filediv[$N], 4))
         _GUICtrlListView_AddSubItem($Lista1, $ITEMID, $Filediv[1] & '\' & $Filediv[$N], 1)
      Next
   Else
      $Filediv = StringSplit($Carpeta, '\') ;Añadir un archivo
      $ITEMID = _GUICtrlListView_AddItem($Lista1, StringTrimRight($Filediv[$Filediv[0]], 4))
      _GUICtrlListView_AddSubItem($Lista1, $ITEMID, $Carpeta, 1)
   EndIf
EndFunc   ;==>_Agregararchivo


Func _SelectArchivo()
   If _GUICtrlListView_GetItemCount($Lista1) <> 0 Then
      $A = 0
      Do
         $A = $A + 1
         If _GUICtrlListView_GetItemSelected($Lista1, $A) = True Then
            $Archivo = $A
            _Aplicar(_GUICtrlListView_GetItemText($Lista1, $A, 1))
            $A = _GUICtrlListView_GetItemCount($Lista1)
            $Selection = 0
         Else
            $Selection = 1
         EndIf
      Until $A = _GUICtrlListView_GetItemCount($Lista1)
      If $Selection = 1 Then
         $Archivo = 0
         _Aplicar(_GUICtrlListView_GetItemText($Lista1, 0, 1))
         _GUICtrlListView_SetItemSelected($Lista1, 0)
      EndIf
   EndIf
EndFunc   ;==>_SelectArchivo


Func _Aplicar($RUTAF)
   $Aplicar = ShellExecute($RUTAF)
EndFunc   ;==>_Aplicar

Func _BorrarSeleccionado()
   $var = MsgBox("1", "Atencion", "Estas seguro que quieres Borarlo?")
   If $var = 1 then
   $ELIMINADO = _GUICtrlListView_GetSelectedCount($Lista1)
   _GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($Lista1))

   FileDelete($ELIMINADO)    ;  <--------------------???????????????????????????????????????????????????????? with the delete button I want to delete the file I have selectioned
   $ITEMID = $ITEMID - $ELIMINADO

Else
   EndIf
EndFunc   ;==>_BorrarSeleccionado

Share this post


Link to post
Share on other sites



Here is modified version of your function _BorrarSeleccionado

Func _BorrarSeleccionado()
   $var = MsgBox("1", "Atencion", "Estas seguro que quieres Borarlo?")
   If $var = "1" then
       MsgBox(0,"","test")
   $selected=_GUICtrlListView_GetSelectedIndices (GUICtrlGetHandle($Lista1),True)
   ConsoleWrite("index=" & $selected[1] & @CRLF)
   $path=_GUICtrlListView_GetItemText (GUICtrlGetHandle($Lista1),$selected[1],1)
   ConsoleWrite($path & @CRLF)
   _GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($Lista1))
   FileDelete($path)    ;  <--------------------???????????????????????????????????????????????????????? with the delete button I want to delete the file I have selectioned
;~    $ITEMID = $ITEMID - $ELIMINADO

Else
   EndIf
EndFunc   ;==>_BorrarSeleccionado

Can you explain better your first question?

Share this post


Link to post
Share on other sites

Here is modified version of your function _BorrarSeleccionado

Func _BorrarSeleccionado()
   $var = MsgBox("1", "Atencion", "Estas seguro que quieres Borarlo?")
   If $var = "1" then
       MsgBox(0,"","test")
   $selected=_GUICtrlListView_GetSelectedIndices (GUICtrlGetHandle($Lista1),True)
   ConsoleWrite("index=" & $selected[1] & @CRLF)
   $path=_GUICtrlListView_GetItemText (GUICtrlGetHandle($Lista1),$selected[1],1)
   ConsoleWrite($path & @CRLF)
   _GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($Lista1))
   FileDelete($path)    ;  <--------------------???????????????????????????????????????????????????????? with the delete button I want to delete the file I have selectioned
;~    $ITEMID = $ITEMID - $ELIMINADO

Else
   EndIf
EndFunc   ;==>_BorrarSeleccionado

Can you explain better your first question?

hello

BorrarSeleccionado;==>_ function is deleting only the name of the listview on the hdd the file is not deleted

at the first point I want when I run the script in the listview show the contents of a folder hdd C:\prueba\ many archivos.exe

without pressing AddButton and choose files

sorry for bad English

thanks

Share this post


Link to post
Share on other sites

For me it deletes files. What OS are you using?

For adding items at startup look at _FileListToArray() and create loop for creating listviewitems based on return from _FileListToArray()function.

Share this post


Link to post
Share on other sites

For me it deletes files. What OS are you using?

For adding items at startup look at _FileListToArray() and create loop for creating listviewitems based on return from _FileListToArray()function.

I have windows 7

the comand filedelet not work for me is my problem sorry

Can you explain me how I can make the loop

I've tried and I could not do anything

I am very beginner

thanks

Share this post


Link to post
Share on other sites

everything works fine thank you very much

could you please post your finished script. I am a noob trying to use these same features,

thx!

Share this post


Link to post
Share on other sites

AndyCollette,

Here is my take on how you might do it. The ####### lines show the areas I changed from the OP's first script: :oops:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <GuiListView.au3>
#include <Constants.au3>
#include <Misc.au3>
#include <GuiToolTip.au3>
#include <Array.au3>
#include <File.au3>

Global $ITEMID, $Lista1
Global $CABECERA = "Archivo              |Ruta                   "

$Ancho = 333
$Alto = 300
#region ### START Koda GUI section ###
    $Form1 = GUICreate("Aplicacion", $Ancho, $Alto, 318, 184, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE))
    ;;;; LISTA
    $Lista1 = GUICtrlCreateListView($CABECERA, 0, 0, $Ancho, 257)
    GUICtrlSetState($Lista1, $GUI_DROPACCEPTED)
    _GUICtrlListView_SetExtendedListViewStyle($Lista1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_INFOTIP))

    ;;;; BOTONES
    $BTNAGREGAR = GUICtrlCreateButton("Add file", 15, 265, 90, 25)
    GUICtrlSetTip(-1, 'Añadir archivos ')
    $BTNEMPEZAR = GUICtrlCreateButton("Run", 123, 265, 90, 25)
    GUICtrlSetTip(-1, 'Ejecutar')
    $BTNBORARSELECT = GUICtrlCreateButton("Delete", 230, 265, 90, 25)
    GUICtrlSetTip(-1, 'Borrar')

    GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

; ###############################################################################
Global $sFolder = FileSelectFolder("Select a folder", "")
$aList = _FileListToArray($sFolder, "*.*", 1)
For $i = 1 To $aList[0]
    $ITEMID = _GUICtrlListView_AddItem($Lista1, StringTrimRight($aList[$i], 4))
    _GUICtrlListView_AddSubItem($Lista1, $ITEMID, $sFolder & '\' & $aList[$i], 1)
Next
; ###############################################################################

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BTNAGREGAR
            _Agregararchivo()
        Case $BTNEMPEZAR
            _SelectArchivo()
        Case $BTNBORARSELECT
            _BorrarSeleccionado()
    EndSwitch
WEnd

Func _Agregararchivo()
    $Carpeta = FileOpenDialog('Add file', '', ' All(*.*) ', 5, '', $Form1)
    $Filediv = StringSplit($Carpeta, '|')
    If $Filediv[0] <> 1 Then ;
        For $N = 2 To $Filediv[0]
            $ITEMID = _GUICtrlListView_AddItem($Lista1, StringTrimRight($Filediv[$N], 4))
            _GUICtrlListView_AddSubItem($Lista1, $ITEMID, $Filediv[1] & '\' & $Filediv[$N], 1)
        Next
    Else
        $Filediv = StringSplit($Carpeta, '\') ;Añadir un archivo
        $ITEMID = _GUICtrlListView_AddItem($Lista1, StringTrimRight($Filediv[$Filediv[0]], 4))
        _GUICtrlListView_AddSubItem($Lista1, $ITEMID, $Carpeta, 1)
    EndIf
EndFunc   ;==>_Agregararchivo

Func _SelectArchivo()
    If _GUICtrlListView_GetItemCount($Lista1) <> 0 Then
        $A = 0
        Do
            $A = $A + 1
            If _GUICtrlListView_GetItemSelected($Lista1, $A) = True Then
                $Archivo = $A
                _Aplicar(_GUICtrlListView_GetItemText($Lista1, $A, 1))
                $A = _GUICtrlListView_GetItemCount($Lista1)
                $Selection = 0
            Else
                $Selection = 1
            EndIf
        Until $A = _GUICtrlListView_GetItemCount($Lista1)
        If $Selection = 1 Then
            $Archivo = 0
            _Aplicar(_GUICtrlListView_GetItemText($Lista1, 0, 1))
            _GUICtrlListView_SetItemSelected($Lista1, 0)
        EndIf
    EndIf
EndFunc   ;==>_SelectArchivo

Func _Aplicar($RUTAF)
    $Aplicar = ShellExecute($RUTAF)
EndFunc   ;==>_Aplicar

; ###############################################################################
Func _BorrarSeleccionado()
    $var = MsgBox("1", "Atencion", "Estas seguro que quieres Borarlo?")
    If $var = 1 Then
        $ELIMINADO = _GUICtrlListView_GetSelectedIndices($Lista1, True) ; Get an array of teh selected indices
        For $i = 1 To $ELIMINADO[0]
            $sText = _GUICtrlListView_GetItemText($Lista1, $ELIMINADO[$i], 1)
            FileDelete($sText)
            _GUICtrlListView_DeleteItem(GUICtrlGetHandle($Lista1), $ELIMINADO[$i])
        Next
    Else
    EndIf
EndFunc   ;==>_BorrarSeleccionado
; ###############################################################################

Please ask if you have any questions. :D

M23

1 person likes this

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

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  
Followers 0