Jump to content

Using items in a ListBox as buttons (to do something)


nf67
 Share

Recommended Posts

Hi there,

Is there any way I can make a click on a listbox item execute a certain code? As if the items in the list were buttons?

Here's an example of what I mean:

#include <GUIListBox.au3>
#include <WindowsConstants.au3>

$GUI = GUICreate("GUI", 400, 400)
$ListBox = GUICtrlCreateList("", 0, 0, 400, 375)
$Button = GUICtrlCreateButton("Retrieve List of .txt Files in the Script Directory", 0, 375, 400, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button
            RetrieveList()
    EndSwitch
WEnd

Func RetrieveList()
    $FileSearch = FileFindFirstFile("*.txt")
    While 1
        $File = FileFindNextFile($FileSearch)
        If @Error Then ExitLoop
        GUICtrlSetData($ListBox, $File & "|")
    WEnd
EndFunc

Is there a way to make the .txt files open (<-for example) by clicking them in the listbox?

Or should I use a different control?

Thanks :D

Link to comment
Share on other sites

Maybe...

#include <GUIListBox.au3>
#include <WindowsConstants.au3>

$GUI = GUICreate("GUI", 400, 400)
$ListBox = GUICtrlCreateList("", 0, 0, 400, 375)
$Button = GUICtrlCreateButton("Retrieve List of .txt Files in the Script Directory", 0, 375, 400, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button
            RetrieveList()
        Case $ListBox
            ShellExecute(GUICtrlRead($ListBox))
    EndSwitch

WEnd

Func RetrieveList()
    $FileSearch = FileFindFirstFile("*.txt")
    While 1
        $File = FileFindNextFile($FileSearch)
        If @error Then ExitLoop
        GUICtrlSetData($ListBox, $File & "|")
    WEnd
EndFunc   ;==>RetrieveList

8)

NEWHeader1.png

Link to comment
Share on other sites

This function poses a potential problem. If you click the button more than once you will likely get the same items added to the list.

Func RetrieveList()
    $FileSearch = FileFindFirstFile("*.txt")
    While 1
        $File = FileFindNextFile($FileSearch)
        If @Error Then ExitLoop
        GUICtrlSetData($ListBox, $File & "|")
    WEnd
EndFunc

Better to use

Func RetrieveList()
    Local $sList = ""
    $FileSearch = FileFindFirstFile("*.txt")
    While 1
        $File = FileFindNextFile($FileSearch)
        If @Error Then ExitLoop
        $sList &= "|" & $File
    WEnd
    If $sList Then GUICtrlSetData($ListBox, $sList)
    FileClose($FileSearch);; You also forgot to close the handle to $FileSearch
EndFunc

Edit: Forgot the code tags

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I'm getting an error when trying to use this

(SCRIPT) (LINE NUMBER (marked in script)) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

;Some Code

Dim $LoadData[33]
Dim $Ing[11]
Dim $Q[11]
Dim $QT[11]

;Some code 

Func LoadSaveFile()
        $RecipePressed = GUICtrlRead($RecipeListBox)
        $LoadData=FileRead(@ScriptDir & "\PB-" & $RecipePressed & ".txt" )
        $LoadData=StringSplit($LoadData, @TAB)
        GUICtrlSetData($TitleInput, $LoadData[1])
        For $Number = 1 to 10
            $ArrayNumber=3 * $Number - 1
            GUICtrlSetData($Ing[$Number], $LoadData[$ArrayNumber]) ;<-- Line number as reported by error
            $ArrayNumber=3 * $Number
            GUICtrlSetData($Q[$Number], $LoadData[$ArrayNumber]) 
            $ArrayNumber=3 * $Number + 1
            GUICtrlSetData($QT[$Number], $LoadData[$ArrayNumber]) 
        Next
        GUICtrlSetData($InstructionBox, $LoadData[32])
EndFunc

As you can see I'm using formulas to match the inputs and the data...

The reason for this is simple, I store the following data in the .txt files(in this order):

Title Ing1 Q1 QT1 Ing2 Q2 QT2 Ing3 Q3 QT3 (etc, up to 10) Instructions <--Input data

[1] [2] [3] etc. <-- Array value

If you work it all out you can see that there's a clear pattern, I've written down the formulas in the picture below:

Posted Image

EDIT: The last formula (QT) should obviously be Array=3*Number+1 (instead of -1)

I don't really have any idea what causes the trouble, are the Dims set wrong? Is there an error in my way of writing down the formulas?

Any help would be greatly appreciated :D

Edited by nf67
Link to comment
Share on other sites

And here's the function that writes the .txt files:

Func WriteSaveFile()
                $SaveTitleData = GUICtrlRead($TitleInput)
                FileWrite(@ScriptDir & "\PB-" & $SaveTitleData &  ".txt", $SaveTitleData & @TAB) ;Save title (1)

                For $Number = 1 to 10
                $SaveIngData = GUICtrlRead($Ing[$Number])
                FileWrite(@ScriptDir & "\PB-" & $SaveTitleData &  ".txt", $SaveIngData & @TAB) ;Save Ings Qs and Qts (30)
                $SaveQData = GUICtrlRead($Q[$Number])
                FileWrite(@ScriptDir & "\PB-" & $SaveTitleData &  ".txt", $SaveQData & @TAB)
                $SaveQTData = GUICtrlRead($QT[$Number])
                FileWrite(@ScriptDir & "\PB-" & $SaveTitleData &  ".txt", $SaveQTData & @TAB)
                Next

                $SaveInstructionData = GUICtrlRead($InstructionBox)
                FileWrite(@ScriptDir & "\PB-" & $SaveTitleData &  ".txt", $SaveInstructionData) ;Save instructions (1)

                If FileExists(@ScriptDir & "\PB-" & $SaveTitleData &  ".txt") Then
                    MsgBox(0, "Success!", "Your " & $SaveTitleData & " recipe has been saved.")
                    RecipeListBoxRefresh()
                EndIf
EndFunc
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...