code once for 3 buttons

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sbrady
Posts: 105
Joined: 15 Mar 2014, 20:41
Location: Virginia Beach

code once for 3 buttons

24 May 2017, 10:22

I have a combo box with several peoples names in the list, the code I wrote works but it I duplicate the code for each person. How do I get each persons name to look at the part of the code that is the same? for example

current method
if person1
{
do this
do this
do this
email person1
}

if person2
{
do this
do this
do this
email person2
}



proposed way:
if person1
{
do function1
email person1
}

if person2
{
do function1
email person2
}


function1
{
do this
do this
do this
}
Guest

Re: code once for 3 buttons

24 May 2017, 10:31

Code: Select all

; store data
persons:={"john":"[email protected]","carol":"[email protected]"} 

; make list for listbox
for k, v in persons
	list .= k "|"

; demo gui
Gui, add, listbox, vPerson, %list%
Gui, add, button, gFunc, Submit
Gui, Show
Return

func:
Gui, submit
;do this
;do this
;do this
MsgBox % persons[Person] ; show email
ExitApp
Return
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: code once for 3 buttons

24 May 2017, 10:34

sbrady wrote:proposed way:

Code: Select all

if person1
{
do function1
email person1
}

if person2
{
do function1
email person2
}


function1
{
do this
do this
do this
}
(Codebox added)

I don't see a problem with that pseudo-code. It looks like you do function1 regardless of the person, so that can be moved outside the if.

Code: Select all

function1()

if (person1)
{
    ;email person1
}

else if (person2)
{
    ;email person2
}
return

function1()
{
    ;do this
}
sbrady
Posts: 105
Joined: 15 Mar 2014, 20:41
Location: Virginia Beach

Re: code once for 3 buttons

24 May 2017, 10:52

thank you Kon, I think your plan is going to work.

; gail
; january
; may
; september
; interactive
; news
; audio ops
; studio 5
; turning point
emailSource = january


ifequal, emailSource, Gail
{
msgbox, gail
getinfo()
}


ifequal, emailSource, january
{
msgbox, january
getinfo()
}


getinfo()
{
FormatTime, CurrentDate,, MM-dd-yy ;04-24-17
msgbox, %CurrentDate%
}
return
sbrady
Posts: 105
Joined: 15 Mar 2014, 20:41
Location: Virginia Beach

Re: code once for 3 buttons

24 May 2017, 11:01

Kon, what if in getinfo() the last thing it does if have a variable with some value. call it....importantINFO.

How do I get (or call) the variable importantINFO in the Gail or January sections.
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: code once for 3 buttons

24 May 2017, 11:06

You'd better search the Help file first, and post questions.

AutoHotkey Beginner Tutorial
https://autohotkey.com/docs/Tutorial.htm

Function
https://autohotkey.com/docs/Tutorial.htm#s5
https://autohotkey.com/docs/Functions.htm
Last edited by IMEime on 24 May 2017, 11:17, edited 2 times in total.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: code once for 3 buttons

24 May 2017, 11:17

Maybe

Code: Select all

if (emailSource = "gail")
{
    msgbox, gail
}
else if (emailSource = "january")
{
    msgbox, january
}
Var := getinfo()
MsgBox, % Var
return
    
getinfo()       
{
    FormatTime, CurrentDate,, MM-dd-yy ;04-24-17
    return CurrentDate
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, jaka1, mikeyww, RussF and 318 guests