add and Alignment Buttons at Gui Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

add and Alignment Buttons at Gui

03 May 2018, 12:44

Welcome dear colleagues
I would add on this Gui a number of buttons in the right side as in the attached picture.
Please help.
Thank you very much

Code: Select all

Gui Add, ListBox, w950 r20 vfiles1, % "Dir1 = " dir1 "| |" list1
Gui Add, ListBox, w950 r20 vfiles2, % "Dir2 = " dir2 "| |" list2
Gui Add, Button, w75 gGuiOK default, Exit
Gui,Add, Text, yp+5 x200,Dir1
Gui,Add, Edit, yp-3 x230 w300 vDir1,%dir1%
Gui,Add, Text, yp+2 x540,Dir2
Gui,Add, Edit, yp-3 x570 w300 vDir2,%dir2%
Gui,Add, Text, yp+2 x880,Mask
Gui,Add, Edit, yp-3 x910 w60 vFileMask,%fileMask%
Gui Add, Button, yp x980 w75 gGo, Go
Gui Show, w1070
Return

Go:
    Gui, Submit, NoHide 
    gosub, DoTheWork
    GuiControl,,files1,% "|Dir1 = " dir1 "| |" list1
    GuiControl,,files2,% "|Dir2 = " dir2 "| |" list2
return

GuiOK:
GuiClose:
GuiEscape:
ExitApp

DoTheWork:
filelist1 := filelist2 := ""
list1 := list2 := ""
fnb1 := fnb2 := 0
add buttons.png
add buttons.png (1.92 KiB) Viewed 1683 times
tmplinshi
Posts: 1557
Joined: 01 Oct 2013, 14:57

Re: add and Alignment Buttons at Gui

03 May 2018, 13:58

Code: Select all

Gui Add, ListBox, w950 r20 vfiles1, % "Dir1 = " dir1 "| |" list1
Gui Add, ListBox, xm w950 r20 vfiles2, % "Dir2 = " dir2 "| |" list2
Gui Add, Button, w75 gGuiOK default, Exit
Gui,Add, Text, yp+5 x200,Dir1
Gui,Add, Edit, yp-3 x230 w300 vDir1,%dir1%
Gui,Add, Text, yp+2 x540,Dir2
Gui,Add, Edit, yp-3 x570 w300 vDir2,%dir2%
Gui,Add, Text, yp+2 x880,Mask
Gui,Add, Edit, yp-3 x910 w60 vFileMask,%fileMask%
Gui Add, Button, yp x980 w75 gGo, Go
Gosub, AddRightSideButtons
Gui Show, w1070
Return

AddRightSideButtons:
    GuiControlGet, ctl, Pos, files1

    x := ctlX + ctlW + 10
    Gui, Add, Button, x%x% y%ctlY% w90 h30, Button 1
    Loop, 9
        Gui, Add, Button, wp hp, % "Button " A_Index+1
return

Go:
    Gui, Submit, NoHide 
    gosub, DoTheWork
    GuiControl,,files1,% "|Dir1 = " dir1 "| |" list1
    GuiControl,,files2,% "|Dir2 = " dir2 "| |" list2
return

GuiOK:
GuiClose:
GuiEscape:
ExitApp

DoTheWork:
filelist1 := filelist2 := ""
list1 := list2 := ""
fnb1 := fnb2 := 0
FanaticGuru
Posts: 1401
Joined: 30 Sep 2013, 22:25

Re: add and Alignment Buttons at Gui  Topic is solved

03 May 2018, 14:02

Code: Select all

Gui Add, ListBox, w950 r20 vfiles1, % "Dir1 = " dir1 "| |" list1
Gui Add, ListBox, w950 r20 vfiles2, % "Dir2 = " dir2 "| |" list2
Gui Add, Button, w75 gGuiOK default, Exit
Gui,Add, Text, yp+5 x200,Dir1
Gui,Add, Edit, yp-3 x230 w300 vDir1,%dir1%
Gui,Add, Text, yp+2 x540,Dir2
Gui,Add, Edit, yp-3 x570 w300 vDir2,%dir2%
Gui,Add, Text, yp+2 x880,Mask
Gui,Add, Edit, yp-3 x910 w60 vFileMask,%fileMask%
Gui Add, Button, yp x980 w75 gGo, Go
y := 10
Loop 4
{
	Gui Add, Button, y%y% x980 w75 gButton, % "Button " A_Index
	y += 30
}	
Gui Show, w1070
Return

Go:
    Gui, Submit, NoHide 
    gosub, DoTheWork
    GuiControl,,files1,% "|Dir1 = " dir1 "| |" list1
    GuiControl,,files2,% "|Dir2 = " dir2 "| |" list2
return

Button:
	MsgBox % A_GuiControl " was Clicked"
return

GuiOK:
GuiClose:
GuiEscape:
ExitApp

DoTheWork:
filelist1 := filelist2 := ""
list1 := list2 := ""
fnb1 := fnb2 := 0
Just use xy coordinates and put buttons where ever you want.

Used a loop but could be done individually. Or you could use something like this to name them more uniquely.

Code: Select all

y := 10
for Index, Name in ["New", "Old", "Parent", "Child"]
{
	Gui Add, Button, y%y% x980 w75 gButton, % Name
	y += 30
}	
Repetitive Gui elements often can be automated with just some math.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: add and Alignment Buttons at Gui

03 May 2018, 16:19

Mr. tmplinshi, Mr.FanaticGuru
Thank you very much for this quick initiative and consistency, you deserve all the appreciation
I think this method is professional,
this is the final code;

Code: Select all

Gui Add, ListBox, w950 r20 vfiles1, % "Dir1 = " dir1 "| |" list1
Gui Add, ListBox, xm w950 r20 vfiles2, % "Dir2 = " dir2 "| |" list2
Gui Add, Button, w75 gGuiOK default, Exit
Gui,Add, Text, yp+5 x200,Dir1
Gui,Add, Edit, yp-3 x230 w300 vDir1,%dir1%
Gui,Add, Text, yp+2 x540,Dir2
Gui,Add, Edit, yp-3 x570 w300 vDir2,%dir2%
Gui,Add, Text, yp+2 x880,Mask
Gui,Add, Edit, yp-3 x910 w60 vFileMask,%fileMask%
Gui Add, Button, yp x980 w75 gGo, Go
Gosub, AddRightSideButtons
Gui Show, w1070
Return

AddRightSideButtons:
    GuiControlGet, ctl, Pos, files1

    x := ctlX + ctlW + 10
    Gui, Add, Button, x%x% y%ctlY% w90 h30, Button 1
    Loop, 9
        Gui, Add, Button, wp hp, % "Button " A_Index+1
return

Go:
    Gui, Submit, NoHide 
    gosub, DoTheWork
    GuiControl,,files1,% "|Dir1 = " dir1 "| |" list1
    GuiControl,,files2,% "|Dir2 = " dir2 "| |" list2
return

GuiOK:
GuiClose:
GuiEscape:
ExitApp

DoTheWork:
filelist1 := filelist2 := ""
list1 := list2 := ""
fnb1 := fnb2 := 0 ; <<<<< added: initialize the counters
Loop %dir1%\%fileMask%
{
If ( A_LoopFileName ~= "~\$")
        continue
    fileList1 .= A_LoopFileName . "`n"
    fnb1++ ; <<<<< changed: don't use A_Index because some files may be skipped
}
StringTrimRight fileList1, fileList1, 1
Loop %dir2%\%fileMask%
{
    If ( A_LoopFileName ~= "~\$")
        continue
    fileList2 .= A_LoopFileName . "`n"
    fnb2++ ; <<<<< changed: don't use A_Index because some files may be skipped
}
StringTrimRight fileList2, fileList2, 1

If (fnb1 = 0) && (fnb2 = 0) ; <<<<< added: early return
    Return

Sort fileList1
Sort fileList2
StringSplit files_1_, fileList1, `n
StringSplit files_2_, fileList2, `n
idxF1 := idxF2 := 1

Loop
{
    If (idxF1 > fnb1) ; <<<<< moved to the top of the loop
    {
        Loop % fnb2 - idxF2 + 1
        {
            list1 .= " |"   ; Empty
            list2 .= files_2_%idxF2% . "|"
            idxF2++
        }
        Break
    }
    If (idxF2 > fnb2) ; <<<<< moved to the top of the loop
    {
        Loop % fnb1 - idxF1 + 1
        {
            list1 .= files_1_%idxF1% . "|"
            idxF1++
            list2 .= " |"   ; Empty
        }
        Break
    }
    If (files_1_%idxF1% = files_2_%idxF2%)
    {
        ; We are in synch
        list1 .= files_1_%idxF1% . "|"
        idxF1++
        list2 .= files_2_%idxF2% . "|"
        idxF2++
    }
    Else
    {
        ; Either file at idxF1 or at idxF3 is missing, we must find which one
        If (files_1_%idxF1% > files_2_%idxF2%)
        {
            ; Missing in first list
            list1 .= " |"   ; Empty
            list2 .= files_2_%idxF2% . "|"
            idxF2++
        }
        Else
        {
            ; Missing in second list
            list1 .= files_1_%idxF1% . "|"
            idxF1++
            list2 .= " |"   ; Empty
        }
    }
}
StringTrimRight list1, list1, 1
StringTrimRight list2, list2, 1
Return

;;---------------------------------------------------  events 1
#IfWinActive, Comparison
1::
ControlSetText, Edit1, E:\ask\work
ControlSetText, Edit2, E:\ask\backup
ControlSetText, Edit3, *.docx
ControlClick, Go, Comparison  
return
;;---------------------------------------------------  events 2

2::
ControlSetText, Edit1, E:\ask\sales
ControlSetText, Edit2, E:\ask\work
ControlSetText, Edit3, *.pdf
ControlClick, Go, Comparison  
return
;;---------------------------------------------------  events 3

3::
ControlSetText, Edit1, E:\ask\sales
ControlSetText, Edit2, E:\ask\backup
ControlSetText, Edit3, *.txt
ControlClick, Go, Comparison  
return
I have a bunch of similar events,
I used the previous 3 hotkeys: one, two, three
My question is
How to dedicate the button 1 for event 1

All thanks, gratitude and appreciation
FanaticGuru
Posts: 1401
Joined: 30 Sep 2013, 22:25

Re: add and Alignment Buttons at Gui

03 May 2018, 17:46

asad41163 wrote: I have a bunch of similar events,
I used the previous 3 hotkeys: one, two, three
My question is
How to dedicate the button 1 for event 1
If you look at my code you can see an example of how to use A_GuiControl to have all the buttons trigger the same subroutine and then sort out from there which button was clicked.

Instead of just displaying the button named clicked you could use some if/then logic to call the hotkeys using gosub.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: add and Alignment Buttons at Gui

03 May 2018, 18:01

asad41163 wrote:How to dedicate the button 1 for event 1
Elaborate what you meant by "event".

You likely want something to happen upon clicking it - gLabel option comes with help. However, since you are creating the buttons in a loop, you'll likely need to force expression to create this "dynamically". Same applies if you want to give it a vGuIVariable, or alternatively save hwnd down somewhere.

Here's sample:

Code: Select all

Gui, Add, Button, % "wp hp vMyButton" A_Index, % "Button " A_Index+1
As result, each button's "variable" will be MyButton1, MyButton2 and so on. You can do it with gLabel similar way.
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: add and Alignment Buttons at Gui

04 May 2018, 16:29

Thank you so much to my dear colleagues
But I do not know the source of the error
What's wrong:
Why button 1 and Button 2 does not work?

Code: Select all

Gui Add, ListBox, w950 r20 vfiles1, % "Dir1 = " dir1 "| |" list1
Gui Add, ListBox, xm w950 r20 vfiles2, % "Dir2 = " dir2 "| |" list2
Gui Add, Button, w75 gGuiOK default, Exit
Gui,Add, Text, yp+5 x200,Dir1
Gui,Add, Edit, yp-3 x230 w300 vDir1,%dir1%
Gui,Add, Text, yp+2 x540,Dir2
Gui,Add, Edit, yp-3 x570 w300 vDir2,%dir2%
Gui,Add, Text, yp+2 x880,Mask
Gui,Add, Edit, yp-3 x910 w60 vFileMask,%fileMask%
Gui Add, Button, yp x980 w75 gGo, Go
Gosub, AddRightSideButtons
Gui Show, w1070
Return

AddRightSideButtons:
    GuiControlGet, ctl, Pos, files1

    x := ctlX + ctlW + 10
    Gui, Add, Button, x%x% y%ctlY% w90 h30, Button 1
    Loop, 9
        Gui, Add, Button, wp hp, % "Button " A_Index+1
return

Go:
    Gui, Submit, NoHide 
    gosub, DoTheWork
    GuiControl,,files1,% "|Dir1 = " dir1 "| |" list1
    GuiControl,,files2,% "|Dir2 = " dir2 "| |" list2
return

GuiOK:
GuiClose:
GuiEscape:
ExitApp

DoTheWork:
filelist1 := filelist2 := ""
list1 := list2 := ""
fnb1 := fnb2 := 0


Button1:
ControlSetText, Edit1, E:\ask\work
ControlSetText, Edit2, E:\ask\backup
ControlSetText, Edit3, *.docx
ControlClick, Go, ask1  
return


Button2:
ControlSetText, Edit1, E:\ask\work
ControlSetText, Edit2, E:\ask\backup
ControlSetText, Edit3, *.docx
ControlClick, Go, ask1  
return
tmplinshi
Posts: 1557
Joined: 01 Oct 2013, 14:57

Re: add and Alignment Buttons at Gui

05 May 2018, 23:04

In order to trigger Button1: lable, your button's text should be 1. For example:

Code: Select all

Gui, Add, Button, x150 y80 w100 h40, 1
Gui, Show, w400 h200
Return

Button1:
	MsgBox, hi
return
However, since your button's text is Button 1, but using ButtonButton 1: lable would cause an error, because lable's name cannot contain space character.
In this case, you can use g option, which FanaticGuru has already told you above.
asad41163
Posts: 268
Joined: 29 Jul 2014, 14:31

Re: add and Alignment Buttons at Gui

07 May 2018, 04:15

Greetings to all
I know that all answers are correct and easy ..
But the problem is that my experience is weak
So I apologize to everyone ..
Thanks Mr tmplinshi
Thanks Mr FanaticGuru
Thanks Mr SirRFI

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Conrad_Smuts, Eureka, HiSoKa, JawGBoi, just me and 245 guests