Sign in to follow this  
Followers 0
Zlow

GUI script debugging needed (simple)

7 posts in this topic

Well I'm writing a script to use as a media jukebox of sorts. I thought I had it working just the way I want, but not so much. My intent is to use this script to open my movie folder, browse for a movie, click the button to play the movie. The code does:

Prompts the user for a file to open

Sets the working directory

Sets up an array with all the file names in the folder

Creates an array with 6 items (from the main array)

Displays 6 buttons in a GUI, when clicked windows executes the file.

Displays next and previous buttons to account for more than 6 files.

The problem is, if a folder with less than 6 items is selected the programs seems to go nuts. I have put in several MSGBOX items to troubleshoot. I do not understand why it acts like $BNext is pressed from the beginning.

; Script Creeated with Autoit v3.3.0.0
;   by Me
;
; Script Function: Media browser like a jukebox

#Include <Array.au3>
#Include <GUIConstantsEx.au3>
#Include <File.au3>


SetupVars()
GetFiles()
SetupButtonArray()
SetupGUI()


WHile 1
    $Msg=GUIGetMsg()
    If $msg=-3 Then Exit
    If $msg=$BItemA1 Then ShellExecute($ExecFile[0])
    If $msg=$BItemA2 Then ShellExecute($ExecFile[1])
    If $msg=$BItemA3 Then ShellExecute($ExecFile[2])
    If $msg=$BItemB1 Then ShellExecute($ExecFile[3])
    If $msg=$BItemB2 Then ShellExecute($ExecFile[4])
    If $msg=$BItemB3 Then ShellExecute($ExecFile[5])
    If $msg=$BNext Then NextPage()
    If $msg=$BPrev Then PrevPage()
WEnd

Func SetupVars()
; Arrays
    Global $FileList, $ExecFile[6]
; Buttons
    Global $BItemA1, $BItemA2, $BItemA3, $BItemB1, $BItemB2, $BItemB3, $BNext, $BPrev
; Other Variables
    Global $Counter=0
EndFunc

Func GetFiles()
;~  $Open = FileSelectFolder("Select the folder to open:","c:\")
;~  $FileList=_FileListToArray($Open,"*",1)
    FileOpenDialog("Find Your Files", "@DesktopDir", "all (*.*)") ;Set Working Directory
    $FileList=_FileListToArray(@WorkingDir,"*",1)
    If @Error=1 Then
        MsgBox (0,"","No Files Found.")
        Exit
    EndIf
;   _ArrayDisplay($FileList,"$FileList")
EndFunc

Func SetupButtonArray(); Populates ExecFile array that will be displayed on the screen
    $ExecFile[0]="" ; Set to "" to display blank buttons when
    $ExecFile[1]="" ; a full screen of buttons is not available
    $ExecFile[2]="" ; (at the end of the file list)
    $ExecFile[3]=""
    $ExecFile[4]=""
    $ExecFile[5]=""
    $nTrackNum=0; counter
    While $nTrackNum<=5 And $Counter<UBound($FileList)-1    ; Loop to setup buttons from original array
        $Counter+=1                                         ; changed depending on which page of files
        $ExecFile[$nTrackNum]=$FileList[$Counter]           ; is being displayed
;~      MsgBox(0,"",$Counter & " " & UBound($FileList))
        $nTrackNum+=1
    WEnd
    MSGBox(0,"","End of Setup Button Array")
EndFunc

Func SetupGUI(); Creates the GUI with the first set of Buttons
    GuiCreate("Media JukeBox",710,575)
    GuiSetFont(10)
    $BItemA1=GuiCtrlCreateButton($ExecFile[0],45,55,200,200)
    $BItemA2=GuiCtrlCreateButton($ExecFile[1],255,55,200,200)
    $BItemA3=GuiCtrlCreateButton($ExecFile[2],465,55,200,200)
    $BItemB1=GuiCtrlCreateButton($ExecFile[3],45,265,200,200)
    $BItemB2=GuiCtrlCreateButton($ExecFile[4],255,265,200,200)
    $BItemB3=GuiCtrlCreateButton($ExecFile[5],465,265,200,200)
    If $FileList[0]>=7 Then $BNext=GuiCtrlCreateButton("Next >>",600,485,65,35)
    If $FileList[0]>=7 Then $BPrev=GuiCtrlCreateButton("<< Prev",45,485,65,35)
    ControlDisable("Media JukeBox","","[Class:Button; Instance:8]")
    GuiSetState()
    MSGBOX(0,"","End SetupGUI")
EndFunc

Func ChangeGUI(); Creates buttons, called after the array has been setup
    MSGBOX(0,"","Start ChangeGUI")
    $BItemA1=GuiCtrlCreateButton($ExecFile[0],45,55,200,200)
    $BItemA2=GuiCtrlCreateButton($ExecFile[1],255,55,200,200)
    $BItemA3=GuiCtrlCreateButton($ExecFile[2],465,55,200,200)
    $BItemB1=GuiCtrlCreateButton($ExecFile[3],45,265,200,200)
    $BItemB2=GuiCtrlCreateButton($ExecFile[4],255,265,200,200)
    $BItemB3=GuiCtrlCreateButton($ExecFile[5],465,265,200,200)
EndFunc

Func NextPage(); Deletes existing buttons, calls SetupButtonArray, calls ChangeGUI, calls CounterCap
    GUICtrlDelete($BItemA1)
    GUICtrlDelete($BItemA2)
    GUICtrlDelete($BItemA3)
    GUICtrlDelete($BItemB1)
    GUICtrlDelete($BItemB2)
    GUICtrlDelete($BItemB3)
    SetupButtonArray()
    ChangeGUI()
    ControlEnable("Media JukeBox","","[Class:Button; Instance:2]")
    If $Counter>=UBound($FileList)-1 Then ControlDisable("Media JukeBox","","[Class:Button; Instance:1]")
    CounterCap()
EndFunc

Func PrevPage(); Deletes existing buttons, calls SetupButtonArray for previous page, calls ChangeGUI
    GUICtrlDelete($BItemA1)
    GUICtrlDelete($BItemA2)
    GUICtrlDelete($BItemA3)
    GUICtrlDelete($BItemB1)
    GUICtrlDelete($BItemB2)
    GUICtrlDelete($BItemB3)
    $Counter-=12
    SetupButtonArray()
    ChangeGUI()
    If $Counter=6 Then ControlDisable("Media JukeBox","","[Class:Button; Instance:2]")
    ControlEnable("Media JukeBox","","[Class:Button; Instance:1]")
EndFunc

Func CounterCap(); Checks end of array has been reached, if so, this function adds to $Counter so previous page calls work correctly.
    $New=Number(($Counter)/6);Devides Counter by 6
    $New=StringTrimLeft($New, StringLen(Floor($New))+1); Removes the intiger and decimal point
    $New=StringLeft($New, 1)                        ; Extracts first decimal Number
    If $New="1" Then $Counter=$Counter+5; Sets $Counter up 1-5 so that the Previous Page function
    If $New="3" Then $Counter=$Counter+4; will populate the correct data.
    If $New="5" Then $Counter=$Counter+3
    If $New="6" Then $Counter=$Counter+2
    If $New="8" Then $Counter=$Counter+1
EndFunc

Any help troubleshooting this would be awesome. Also if there is a better way to set the working directly please help ^_^

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You don't need to delete the controls just to change their text, use GUICtrlSetData instead. Also, you need to take a look at the example of GUICtrCreateButton in the help file to see how to build the message loop because if a case is satisfied you don't need to continue checking any other possibilities of $msg against the other events or controls you defined.

Edit: Actually, it's GUICtrlCreateButton and not GUICreate...

Edited by Authenticity

Share this post


Link to post
Share on other sites

Thanks for the response. I have changed the while loop to include a Select/Case statement.

WHile 1
    $Msg=GUIGetMsg()
    Select 
        Case $msg=$Gui_Event_Close
            ExitLoop
        Case $msg=$BItemA1 
            ShellExecute($ExecFile[0])
        Case $msg=$BItemA2
            ShellExecute($ExecFile[1])
        Case $msg=$BItemA3
            ShellExecute($ExecFile[2])
        Case $msg=$BItemB1
            ShellExecute($ExecFile[3])
        Case $msg=$BItemB2
            ShellExecute($ExecFile[4])
        Case $msg=$BItemB3
            ShellExecute($ExecFile[5])
        Case $msg=$BNext
            NextPage()
        Case $msg=$BPrev
            PrevPage()
    EndSelect
WEnd

However I still have the same problem when less than 6 files are selected. If I comment out the last two Case statements the script works perfectly for less than 6 files. If I put in a MSGBOX() in the NextPage() function, I see the message appear. Eventhough that function should not run unless the Next button is pressed. Why are my functions running without being called?

/Zlow

Share this post


Link to post
Share on other sites

First, it's more instructive to use Switch than Select for your case.

Second, you need to use some arrays ... you could cut down on your code.

Third, if there's less than 6 items then $BNext is not set so will most like equal zero. Zero is what GUIGetMsg() returns when there's no message. That's why your function is called.

WBD

Share this post


Link to post
Share on other sites

Thanks WBD. I was slowly working my way to that conclusion. I didn't know I needed to or even could define those variables I used to create the buttons. I'm still very new to scripting. You said I should use more arrays but I guess I don't know how I could further implement arrays in my script. Can you provide an example for me to learn from? Also I do not understand the difference between Switch and Select. I've read the help file on them both but it doesn't describe any pros/cons of using either.

WHile 1
    $Msg=GUIGetMsg()
    Switch $Msg
        Case $Gui_Event_Close
            ExitLoop
        Case $BItemA1 
            ShellExecute($ExecFile[0])
        Case $BItemA2
            ShellExecute($ExecFile[1])
        Case $BItemA3
            ShellExecute($ExecFile[2])
        Case $BItemB1
            ShellExecute($ExecFile[3])
        Case $BItemB2
            ShellExecute($ExecFile[4])
        Case $BItemB3
            ShellExecute($ExecFile[5])
        Case $BNext
            NextPage()
        Case $BPrev
            PrevPage()
    EndSwitch
WEnd

Is this better? why?

/Zlow

Share this post


Link to post
Share on other sites

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#Include <File.au3>

Global $hGui, $BItem[7], $BNext, $BPrev, $biX = 45, $biY = 55, $FSF, $x
Global $aFilter = StringSplit("avi|mpg|wmv|wma|mp3|mp4|mkv|wav|mid", "|")
Global $ExecFile = GetFiles()

$hGui = GuiCreate("Media JukeBox", 710, 575)
GuiSetFont(10)
For $i = 1 To 6
    Local $FileName = ""
    If $i <= $ExecFile[0] Then $FileName = $ExecFile[$i]
    $BItem[$i] = GuiCtrlCreateButton($FileName,$biX,$biY,200,200, $BS_MULTILINE)
    $biX += 210
    If Not Mod($i, 3) Then
        $biX = 45
        $biY = 265
    EndIf
Next
$BNext = GuiCtrlCreateButton("Next >>",600,485,65,35)
If $ExecFile[0] <= 6 Then 
    GUICtrlSetState(-1, $GUI_HIDE)
    $Cnt = $ExecFile[0]
Else    
    $Cnt = 6 
EndIf   
$BPrev = GuiCtrlCreateButton("<< Prev",45,485,65,35)
GUICtrlSetState(-1, $GUI_HIDE)
GuiSetState(@SW_SHOW, $hGui)

WHile 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BItem[1] To $BItem[6]
            Local $Execute = $FSF & "\" & GUICtrlRead($msg)
            If Not StringInStr(FileGetAttrib($Execute), "D") And FileExists($Execute) Then ShellExecute($Execute)
        Case $BNext
            GUICtrlSetState($BPrev, $GUI_SHOW)
            $x = 1
            For $i = ($Cnt+1) To ($Cnt+6)
                Local $FileName = ""
                If $i <= $ExecFile[0] Then $FileName = $ExecFile[$i]    
                GUICtrlSetData($BItem[$x], $FileName)
                $x += 1
            Next    
            $Cnt += 6
            If $Cnt >= $ExecFile[0] Then GUICtrlSetState($BNext, $GUI_HIDE)
        Case $BPrev
            GUICtrlSetState($BNext, $GUI_SHOW)
            $x = 1
            For $i = ($Cnt-11) To ($Cnt-6)
                GUICtrlSetData($BItem[$x], $ExecFile[$i])
                $x += 1
            Next    
            $Cnt -= 6
            If $Cnt = 6 Then GUICtrlSetState($BPrev, $GUI_HIDE)         
    EndSwitch
WEnd    

Func GetFiles()
    Local $sRet = ""
    $FSF = FileSelectFolder("Find Your Files", "", 2, "", $hGui)
    If Not $FSF Then Exit
    $FileList = _FileListToArray($FSF,"*",1)
    If @error Then
        MsgBox (0,"","No Files Found.")
        Exit
    EndIf
    For $i = 1 To  $FileList[0]
        For $j = 1 To $aFilter[0]
            If StringRight($FileList[$i], 4) = "." & $aFilter[$j] Then $sRet &= $FileList[$i] & "|"
        Next
    Next
    If $sRet = "" Then Exit
    Return StringSplit(StringTrimRight($sRet, 1), "|")
EndFunc

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Well you managed to drop 50 lines of code off, but its complicated enough I don't have a clue what some of it is doing. I'll start looking up some of the code to learn wth is going on lol. I've never used things like Mod or GuiCtrlSetState

oh, and Thanks!

Edit: wow... I guess I'm too dense or too much of a novice to think the way you do, I'm hoping for the latter. Thank you for showing me more efficient ways of designing this, I never would of thought of the paths you took. Now I have code I understand to reference in future projects. Thank you. I'll see if I can make it look pretty.

/Zlow

Edited by Zlow

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