Jump to content

Create buttons, centered in GUI, in a loop


Recommended Posts

Hi everyone, I'm working on a program which scans a folder, find its subfolders and put them as buttons in my GUI using a loop.(For a further use in another context)

Basically, it works, but I would want those buttons to be centered(in width) in the GUI and all having the same distance between each other.

I tried a lot of mathematical processes but I'm not able to do what I want :huh2:

So, if one of you could help me, it would be very nice ;)

Here is my code:

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


Local $Button[2] = [0]
Local $GUIWidth = 550
Local $GUIHeight = 180

$folders = _FileListToArray(@DesktopDir & "\Test", "*", 2)
If @Error=1 Then
    MsgBox (0,"","No Folders Found.")
    Exit
EndIf

GUICreate("Test", $GUIWidth, $GUIHeight, ((@DesktopWidth / 2) -($GUIWidth / 2)), ((@DesktopHeight / 2) -($GUIHeight / 2))) ;GUI centered in the screen

$Btn_Start = GUICtrlCreateDummy()
For $i = 1 To $folders[0]
    If UBound($Button) = $i Then
        ReDim $Button[$Button[0] + 2]
    EndIf
    $Button[$i] = GUICtrlCreateButton($folders[$i], ((($GUIWidth/2)/$folders[0]) * $i), 143, 80, -1, $BS_MULTILINE) ;This line makes me crazy XD
    $Button[0] = $i
Next
ReDim $Button[$Button[0]+1]
$Btn_End = GUICtrlCreateDummy()


GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Btn_Start To $Btn_End
            For $i = 1 To UBound($Button) - 1
                If $Msg == $Button[$i] Then
                    MsgBox("", "test", $Button[$i])
                EndIf
            Next
    EndSwitch
WEnd
Link to comment
Share on other sites

Try this

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


Local $Button[2] = [0]
Local $GUIWidth = 550
Local $GUIHeight = 180
Global $button_width=80
$folders = _FileListToArray(@ScriptDir & "\Test", "*", 2)
If @Error=1 Then
    MsgBox (0,"","No Folders Found.")
    Exit
EndIf

GUICreate("Test", $GUIWidth, $GUIHeight, ((@DesktopWidth / 2) -($GUIWidth / 2)), ((@DesktopHeight / 2) -($GUIHeight / 2))) ;GUI centered in the screen
$dist_2=$GUIWidth/($folders[0]*$button_width)
$dist=($GUIWidth-($folders[0]*$button_width+($folders[0]-1)*$dist_2))/2
$Btn_Start = GUICtrlCreateDummy()
For $i = 1 To $folders[0]
    If UBound($Button) = $i Then
        ReDim $Button[$Button[0] + 2]
    EndIf
    $Button[$i] = GUICtrlCreateButton($folders[$i], $dist+($button_width+$dist_2)*($i-1), 143, 80, -1, $BS_MULTILINE) ;This line makes me crazy XD
    $Button[0] = $i
Next
ReDim $Button[$Button[0]+1]
$Btn_End = GUICtrlCreateDummy()


GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Btn_Start To $Btn_End
            For $i = 1 To UBound($Button) - 1
                If $Msg == $Button[$i] Then
                    MsgBox("", "test", $Button[$i])
                EndIf
            Next
    EndSwitch
WEnd
Link to comment
Share on other sites

Well, now I have another problem...My mathematical skills are decreasing with time ;)

Seriously, my problem is when I have more than 5-6 folders...I would want my program to create a new line for each 5 folders...

Few years ago, i would have been able to do it by myself, but now, I seriously need help :huh2:

Here is my try :

#include-once
#Include <File.au3>
#Include <Array.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>


Local $Button[2] = [0]
Local $GUIWidth = 550
Local $GUIHeight = 200
Local $ButtonTop = 143
Global $button_width=80

_LoopCreateButtons("C:\Program Files")

Func _LoopCreateButtons($path = "")
    If $path = "" Then
        Return SetError(1)
    EndIf
    $folders = _FileListToArray($path, "*", 2)
    If @Error=1 Then
        MsgBox (0,"","No Folders Found.")
        Exit
    EndIf

For $i = 1 To $folders[0]
    If $i <= 5 Then
        $c = $i
    Else
        $c = $c + 1
        If $c > 5 Then
            $c = 1
            $GUIHeight = $GUIHeight + 25
        EndIf
    EndIf
Next

GUICreate("Test", $GUIWidth, $GUIHeight, ((@DesktopWidth / 2) -($GUIWidth / 2)), ((@DesktopHeight / 2) -($GUIHeight / 2))) ;GUI centered in the screen
$dist_2=$GUIWidth/(int($folders[0]/5)*$button_width) + 10
$dist=($GUIWidth-(int($folders[0]/5)*$button_width+(int($folders[0]/5)-1)*$dist_2))/2
$Btn_Start = GUICtrlCreateDummy()
For $i = 1 To $folders[0]
    If UBound($Button) = $i Then
        ReDim $Button[$Button[0] + 2]
    EndIf
    If $i <= 5 Then
        $c = $i
    Else
        $c = $c + 1
        If $c > 5 Then
            $c = 1
            $ButtonTop = $ButtonTop + 30
        EndIf
    EndIf
    $Button[$i] = GUICtrlCreateButton($folders[$i], $dist+($button_width+$dist_2)*($c-1), $ButtonTop, $button_width, 25, $BS_MULTILINE)
    $Button[0] = $i
Next
ReDim $Button[$Button[0]+1]
$Btn_End = GUICtrlCreateDummy()

$Btn_Cancel = GUICtrlCreateButton("Annuler",(($GUIWidth/2)-($button_width/2)), ($GUIHeight - 30), $button_width, 25, $BS_MULTILINE)


GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Btn_Start To $Btn_End
            For $i = 1 To UBound($Button) - 1
                If $Msg == $Button[$i] Then
                    MsgBox("", "test", $Button[$i])
                EndIf
            Next
        Case $Btn_Cancel
            Exit
    EndSwitch
WEnd
EndFunc
Edited by ravenwish
Link to comment
Share on other sites

Try this, although it doesn't resize GUI

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


Local $Button[2] = [0]
Local $GUIWidth = 550
Local $GUIHeight = 180
Global $button_width=80, $max
;~ $folders = _FileListToArray(@ScriptDir & "\Test", "*", 2)
$folders=_FileListToArray("E:\Program files\", "*", 2)
If @Error=1 Then
    MsgBox (0,"","No Folders Found.")
    Exit
EndIf
$max=$folders[0]
GUICreate("Test", $GUIWidth, $GUIHeight+30, ((@DesktopWidth / 2) -($GUIWidth / 2)), ((@DesktopHeight / 2) -($GUIHeight / 2))) ;GUI centered in the screen
If $folders[0]>6 Then $max=6
$dist_2=$GUIWidth/($max*$button_width)
$dist=($GUIWidth-($max*$button_width+($max-1)*$dist_2))/2
$Btn_Start = GUICtrlCreateDummy()
Global $j=0, $k=0
For $i = 1 To $folders[0]
    If UBound($Button) = $i Then
        ReDim $Button[$Button[0] + 2]
    EndIf
    $k=$k+1
    If $k>6 Then
    $j=$j+1
    $k=1
    EndIf
    $Button[$i] = GUICtrlCreateButton($folders[$i], $dist+($button_width+$dist_2)*($k-1), 143-25*$j, 80, -1, $BS_MULTILINE) ;This line makes me crazy XD
    $Button[0] = $i
Next
ReDim $Button[$Button[0]+1]
$Btn_End = GUICtrlCreateDummy()
$cancel=GUICtrlCreateButton("Annuler",(($GUIWidth/2)-($button_width/2)), ($GUIHeight - 5),$button_width,25)

GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Btn_Start To $Btn_End
            For $i = 1 To UBound($Button) - 1
                If $Msg == $Button[$i] Then
                    MsgBox("", "test", $Button[$i])
                EndIf
            Next
    EndSwitch
WEnd

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...