Jump to content

Text from List


Recommended Posts

Does anybody know how to retrieve text from a List?  That is: GUICtrlCreateList. I've found this thread and other similar that show how to retrieve text from a listview however I'm more interested in plain list control this type of controls have a feature/style called $LBS_NOTIFY that can be used somehow to get the text from the item in the list when the user clicks or double clicks the item but I have no Idea how to achieve it.

I will appreciate anyhelp in anyway.

Edited by Mannyfresh15
Link to comment
Share on other sites

Just use GuiCtrlRead in the "GuiGetMsg-Loop". Here's a small demo with 2 Listboxes: 

#include <ListBoxConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <GuiListBox.au3>
#include <GUIConstantsEx.au3>
#include <array.au3>
#include <File.au3>

Const $sCSV = @AppDataDir & "\2cbo.CSV"
Const $sElect = "bitte auswählen"
Dim $a_sCSV, $aSplit, $sLstChr = ""
_FileReadToArray($sCSV, $a_sCSV)
Dim $aCSV[$a_sCSV[0]][2]
For $i = 1 To UBound($a_sCSV) - 1
    ;ConsoleWrite($i & $a_sCSV[$i] & @CRLF)
    $aSplit = StringSplit($a_sCSV[$i], ";")
    If Not StringInStr($sLstChr, $aSplit[1]) Then $sLstChr &= $aSplit[1] & "|"
    $aCSV[$i - 1][0] = $aSplit[1]
    $aCSV[$i - 1][1] = $aSplit[2]
Next
;ConsoleWrite($sLstChr & @CRLF)

$hGui = GUICreate("2 Listen aus 1er CSV", 250, 140, 302, 218)
$idListChr = GUICtrlCreateList($sElect, 8, 8, 200, 50,BitOR($WS_BORDER, $WS_VSCROLL, $WS_TABSTOP, $LBS_NOTIFY))
GUICtrlSetData(-1, $sLstChr)
$idListChrNr = GUICtrlCreateList("", 8, 60, 200, 50)
$idBtnExit = GUICtrlCreateButton("Be&enden", 8, 110)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $idBtnExit
            Exit
        Case $idListChr
            $sVal = GUICtrlRead($idListChr)
            GUICtrlSetData($idListChrNr, "")
            If $sVal <> $sElect Then
                $aSplit = _ArrayFindAll($aCSV, $sVal, 0, 0, True, True, 0)
                $sLstChr = ""
                For $i = 0 To UBound($aSplit) - 1
                    $sLstChr &= $aCSV[$aSplit[$i]][1] & "|"
                Next
                ;ConsoleWrite($sLstChr & @CRLF)
                GUICtrlSetData($idListChrNr, $sLstChr)
                _GUICtrlListBox_SetCurSel($idListChrNr, 0)
            EndIf
    EndSwitch
WEnd

I used 2cbo.csv for populating both ListBoxes.

Link to comment
Share on other sites

Thank you very much for your reply I really appreciated. Although I get an error ):

 (13) : ==> Subscript used on non-accessible variable.:
Dim $aCSV[$a_sCSV[0]][2]
Dim $aCSV[$a_sCSV^ ERROR

 

By the way. I've found a way, way way easier than the one you posted. I will post an example in a minute.

 

Edited by Mannyfresh15
Link to comment
Share on other sites

2 hours ago, AutoBert said:

Have you downloaded the CSV-file in your  @AppDataDir?

I Changed the path and created one and still doesn't work ):

Anyways here is my version.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <ListBoxConstants.au3>
#include <GuiListBox.au3>

Global $MyFileList  = ""

#cs

Global $MyFileList  [4]

$MyFileList   [0] = "3"
$MyFileList   [1] = "Im #1"
$MyFileList  [2] = "I'm #2"
$MyFileList  [3] = "I'm #3"

#ce


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("NGT File Creator", 387, 233, 414, 146)
GUISetFont(12, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Select", 292, 17, 75, 25)
$Input1 = GUICtrlCreateInput("Enter a valid folder path with files in it.", 5, 16, 277, 28)
GUICtrlSetstate(-1, $GUI_FOCUS)
$idMylist = GUICtrlCreateList("Your files will be shown here", 5, 60, 376, 182,$WS_VSCROLL  + $LBS_NOTIFY )
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

#cs

 For $a = 0 To (UBound ( $MyFileList ) -1 )
     If $a = 0  Then
         GUICtrlSetData($idMylist,  "")
         GUICtrlSetData($idMylist,  "  Number of files in Folder: " & $MyFileList [$a] & @CRLF & @CRLF )
       Else
         GUICtrlSetData($idMylist,$a & " " & $MyFileList [$a] )
      EndIf
Next

#ce

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

         Case $Button1

            $Folder1 = FileSelectFolder("Select a Folder", @UserProfileDir ,1)

              if Not @error Then

                 If DirGetSize ($Folder1) = 0 Then
               MsgBox(262144, "Hey!", $Folder1 & @CRLF & "The folder is emty!.")
               ContinueLoop
            EndIf

 GUICtrlSetData($Input1  , $Folder1 )

$MyFileList =  _FileListToArray ( $Folder1  ,"*" , 1)

            if @error = 1 or @error = 2 or @error = 3 or @error = 4 Then
               MsgBox (262144 , "Hey!" , "Error Loading Files ):" ,0)
            EndIf

      For $a = 0 To (UBound ( $MyFileList ) -1 )
     If $a = 0  Then
        GUICtrlSetData($idMylist,  "")
         GUICtrlSetData($idMylist,  "  Number of files in Folder: " & $MyFileList [$a] )
       Else
         GUICtrlSetData($idMylist ,$a & " " & $MyFileList [$a] )
      EndIf
   Next

       EndIf



         Case $idMylist

$Item = _GUICtrlListBox_GetCurSel ( $idMylist )

If $Item > 0 Then

 For $a = 1 to UBound ( $MyFileList) -1


    if  $a = $Item Then
       ClipPut ( $MyFileList [$a])
        ExitLoop
        EndIf


Next

_WinAPI_ShowMsg("Current Selection: " & $Item  & " " & $MyFileList [$a] & @LF & "has been put in the Clipboard." )

EndIf


Case $Input1

$file1 = GUICtrlRead($Input1)

            If FileGetSize ($file1)  Then
               MsgBox(262144, "Hey!", $file1 & @CRLF & "It's not a folder!.")
               ContinueLoop
            EndIf

            If DirGetSize ($file1) = 0 Then
               MsgBox(262144, "Hey!", $file1 & @CRLF & "The folder is emty!.")
               ContinueLoop
            EndIf

        if Not FileExists ($file1) Then
           MsgBox (262144 , "Hey!" , "The folder doesn't exist!",0)
            ContinueLoop
            EndIf

$MyFileList =  _FileListToArray ( $file1  ,"*" , 1)

            if @error = 1 or @error = 2 or @error = 3 or @error = 4 Then
               MsgBox (262144 , "Hey!" , "Error Loading Files ):" ,0)
            EndIf

      For $a = 0 To (UBound ( $MyFileList ) -1 )
     If $a = 0  Then
         GUICtrlSetData($idMylist,  "")
         GUICtrlSetData($idMylist,  "  Number of files in Folder: " & $MyFileList [$a]  )
       Else
         GUICtrlSetData($idMylist,$a & " " & $MyFileList [$a] )
      EndIf
   Next


    EndSwitch
WEnd

 

Edited by Mannyfresh15
Link to comment
Share on other sites

Script works if CSV-file exists, so try this version, CSV-file will be generated if not exists:

#include <ListBoxConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <GuiListBox.au3>
#include <GUIConstantsEx.au3>
#include <array.au3>
#include <File.au3>

Const $sCSV = @AppDataDir & "\2cbo.CSV"
Const $sElect = "bitte auswählen"
Global $a_sCSV, $aSplit, $sLstChr = ""
If Not FileExists($sCSV) Then
    FileWriteLine($sCSV,'Auto;Felgen')
                FileWriteLine($sCSV,'Werkzeug;BitSätze')
    FileWriteLine($sCSV,'Werkzeug;Schraubenschlüssel')
    FileWriteLine($sCSV,'Auto;Reifen')
    FileWriteLine($sCSV,'Zubehör;Enteiserspray')
    FileWriteLine($sCSV,'Auto;Kühler')
EndIf
_FileReadToArray($sCSV, $a_sCSV)
Dim $aCSV[$a_sCSV[0]][2]
For $i = 1 To UBound($a_sCSV) - 1
    ;ConsoleWrite($i & $a_sCSV[$i] & @CRLF)
    $aSplit = StringSplit($a_sCSV[$i], ";")
    If Not StringInStr($sLstChr, $aSplit[1]) Then $sLstChr &= $aSplit[1] & "|"
    $aCSV[$i - 1][0] = $aSplit[1]
    $aCSV[$i - 1][1] = $aSplit[2]
Next
;ConsoleWrite($sLstChr & @CRLF)

$hGui = GUICreate("2 Listen aus 1er CSV", 250, 140, 302, 218)
$idListChr = GUICtrlCreateList($sElect, 8, 8, 200, 50, BitOR($WS_BORDER, $WS_VSCROLL, $WS_TABSTOP, $LBS_NOTIFY))
GUICtrlSetData(-1, $sLstChr)
$idListChrNr = GUICtrlCreateList("", 8, 60, 200, 50)
$idBtnExit = GUICtrlCreateButton("Be&enden", 8, 110)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $idBtnExit
            Exit
        Case $idListChr
            $sVal = GUICtrlRead($idListChr)
            GUICtrlSetData($idListChrNr, "")
            If $sVal <> $sElect Then
                $aSplit = _ArrayFindAll($aCSV, $sVal, 0, 0, True, True, 0)
                $sLstChr = ""
                For $i = 0 To UBound($aSplit) - 1
                    $sLstChr &= $aCSV[$aSplit[$i]][1] & "|"
                Next
                ;ConsoleWrite($sLstChr & @CRLF)
                GUICtrlSetData($idListChrNr, $sLstChr)
                _GUICtrlListBox_SetCurSel($idListChrNr, 0)
            EndIf
    EndSwitch
WEnd

 

Edited by AutoBert
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...