Handle an array in a function with a GUI and return a value Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Handle an array in a function with a GUI and return a value

11 May 2021, 13:22

I have made a test function

Code: Select all

#Singleinstance force

aTemp := []
aTemp.guiInfo := "Outside"
; MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % aTemp.guiInfo
Result := FuncGui()
MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % "3)`nResult .: " Result
ExitApp


FuncGui()
{	Global aTemp
	Static ImgSelect

	imgName := []
	imgName.name1 := "Test"
	MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % "1)`n" imgName.name1

	Gui 2: Add, Edit, x30 y30 w50 +Center vImgSelect gImgNumber,
	Gui 2: Show
	
	WinWaitActive	; If all window parameters are blank or omitted, the "Last Found Window" will be used.
	WinWaitClose	; Same as above
	; Suspend Off		; Re-enable hotstrings that were disable above
	
	Return ValidValue

	ImgNumber:
		Gui 2: Submit
		ValidValue := " - " aTemp.guiInfo " - " imgName.name1  " - " ImgSelect
		MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % "2)`nValidValue .: `t" ValidValue
		Gui 2: Destroy
	Return
}
MsgBox1 the GUI and MsgBox2 seems to work (but not MsgBox3)
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Handle an array in a function with a GUI and return a value

11 May 2021, 13:59

Code: Select all

MsgBox, % CreateGui()
ExitApp

CreateGui() {
   static Edit1, hGui := 0
   if (A_Gui = hGui) {
      GuiControlGet, Edit1
      Gui, Destroy
      Return
   }
   Gui, New, hwndhGui
   Gui, Add, Edit, w100
   handler := Func(A_ThisFunc)
   GuiControl, +g, Edit1, % handler
   Gui, Show
   WinWaitClose, ahk_id %hGui%
   Return Edit1
}
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Handle an array in a function with a GUI and return a value

12 May 2021, 04:28

Thank you!
But I don't understand what happens (it works on some way)

But I do not know how to translate the answer - and work in my case.
(my example is simplified)

Can you write some comments - maybe I can use the solution
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Handle an array in a function with a GUI and return a value

12 May 2021, 05:02

Here is a more detailed description of my wishes

1) In the main program - the array aTemp contains information to be used in the GUI function.

2) In the GUI function, other programs is run and fill information to the array imgName and show some images in the GUI.

3) One of the images, displayed in the GUI function, must be selected (1-9) by the user - imgSelect have the value.

4) Information from aTemp / imgName / imgSelect gives a result - ValidValue

5) The content of ValidValue is then returned to the main program.
___________________________________________________
My solution can handle 1-4 above (but not point 5)
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Handle an array in a function with a GUI and return a value

12 May 2021, 05:30

What do you see if you double-click the script's tray icon after the Gui window was closed?
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Handle an array in a function with a GUI and return a value

12 May 2021, 06:47

@teadrinker
The debugging creator of the originally posted function. ;)
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Handle an array in a function with a GUI and return a value

12 May 2021, 06:56

003: aTemp := []
004: aTemp.guiInfo := "Outside"
006: Result := FuncGui()
016: imgName := []
017: imgName.name1 := "Test"
018: MsgBox,,Rad %A_LineNumber% -> %A_ScriptName%,"1)
" imgName.name1 (1.42)
020: Suspend,On
021: Gui,2: +AlwaysOnTop
022: Gui,2: +LastFound
024: Gui,2: Add,Edit,x30 y30 w50 +Center vImgSelect gImgNumber
025: Gui,2: Show (0.03)
027: WinWaitActive,2: (2.48)
034: Gui,2: Submit
035: ValidValue := " - " aTemp.guiInfo " - " imgName.name1 " - " ImgSelect
036: MsgBox,,Rad %A_LineNumber% -> %A_ScriptName%,"2)
ValidValue .: " ValidValue (2.08)
038: Return (9.25)
027: STILL WAITING (13.81): WinWaitActive,2:

Press [F5] to refresh.
Edit .:
Have I misunderstood something?
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Handle an array in a function with a GUI and return a value

12 May 2021, 13:40

@Albireo
You don't need so much arrays, one is enough. You can pass it in the function with info, and collect into it all data you need. An example:

Code: Select all

Info := {data: ["Image1", "Image2"]} ; this data will be used to create GUI
CreateGui(Info)
MsgBox, % Info.image
ExitApp

CreateGui(Info) {
   static Radio, hGui := 0
   if (A_Gui = hGui) { ; this block will be launched when OK is clicked
      Gui, Submit, NoHide
      Info.image := "Image" . Radio ; save info in the same array
      Gui, Destroy
      Return
   }
   Gui, New, hwndhGui
   Gui, Add, Radio, vRadio Checked, % Info.data.1
   Gui, Add, Radio, x+10 yp, % Info.data.2
   Gui, Add, Button, x40 w120, OK
   handler := Func(A_ThisFunc).Bind(Info) ; set this function as g-label for the button and pass Info array
   GuiControl, +g, Button3, % handler
   Gui, Show, w200
   WinWaitClose, ahk_id %hGui%
}
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Handle an array in a function with a GUI and return a value

13 May 2021, 03:32

Thank you!
The array outside the function() - in the example - contains, among other things, information on where the temporary images should be handled.
The "only thing" the function() should do, is to read images from a PDF-file (xPdfReader - pdfimages) and makeit posibility to select one of these image.

One "problem" is - The size(pixels) of the image must be handled (it can be small och big) - larger than the screen or ...
Next "problem" is - I don't know how many images it is in the PDF-file (I know it is more than one)
( Now I can't handle more than five images in my function - the computer screen does not have room for more - right now
It was complicated to make a scroll function)

At first I thought to select the image by clicking on the image (but do not know how to solve it)

Then thought it would work if only the number of the desired image is specified. (radio buttons is also nice)
Because the name of the Image is in the temporary array (in the function) as .name1 .name2 .name3 ...
It's easy to know the name of the selected file.

The last thing this function should perform, is to create a full filename of the selected image.
(and return the imagename.)

The only thing I want to know is the filename of the selected image.
(it was the last step to end the function with the GUI going wrong)
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Handle an array in a function with a GUI and return a value

13 May 2021, 03:56

Albireo wrote:Edit .:
Have I misunderstood something?
Obviously!
027: STILL WAITING (13.81): WinWaitActive,2:
You changed the test script but 2: is not the title of your Gui window.

And because you added

Code: Select all

Gui,2: +LastFound
you don't need to specify a title at all:

Code: Select all

	WinWaitActive	; If all window parameters are blank or omitted, the "Last Found Window" will be used.
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Handle an array in a function with a GUI and return a value

13 May 2021, 06:05

I still don't understand.

Does the GUI-window need a name?
If I only use WinWaitActive
the MsgBox 1) is OK
But at the same time as GUI edit is shown, MsgBox 3) is open - behind the GUI
(MsgBox 2) is never activated)
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Handle an array in a function with a GUI and return a value  Topic is solved

13 May 2021, 06:13

I don't know which code you are running. But you're originally posted script with one added line is running for me:

Code: Select all

#Singleinstance force

aTemp := []
aTemp.guiInfo := "Outside"
; MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % aTemp.guiInfo
Result := FuncGui()
MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % "3)`nResult .: " Result
ExitApp

FuncGui()
{	Global aTemp
	Static ImgSelect

	imgName := []
	imgName.name1 := "Test"
	MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % "1)`n" imgName.name1
  	Gui 2: +LastFound ; <<<<< added this line
	Gui 2: Add, Edit, x30 y30 w50 +Center vImgSelect gImgNumber,
	Gui 2: Show

	WinWaitActive	; If all window parameters are blank or omitted, the "Last Found Window" will be used.
	WinWaitClose	; Same as above
	; Suspend Off		; Re-enable hotstrings that were disable above

	Return ValidValue

	ImgNumber:
		Gui 2: Submit
		ValidValue := " - " aTemp.guiInfo " - " imgName.name1  " - " ImgSelect
		MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % "2)`nValidValue .: `t" ValidValue
		Gui 2: Destroy
	Return
}
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Handle an array in a function with a GUI and return a value

13 May 2021, 06:35

Thank you! :dance:
In all my try I have missed Gui 2: Destroy in code

Code: Select all

	ImgNumber:
		Gui 2: Submit
		ValidValue := " - " aTemp.guiInfo " - " imgName.name1  " - " ImgSelect
		MsgBox ,, Rad %A_LineNumber% -> %A_ScriptName%, % "2)`nValidValue .: `t" ValidValue
		Gui 2: Destroy
	Return
Maybe it's better to use .:

Code: Select all

	Gui 2: New, +hwndhGui
... ...
	WinWaitActive ahk_id %hGui%
than

Code: Select all

	Gui 2: +LastFound
	... ...
	WinWaitActive

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: garry, Google [Bot], marypoppins_1, OrangeCat, ShatterCoder and 131 guests