I indeed started to do it like that, but As A_GuiEvent is used a lot, I started to include it by binding it to the function.
This works with a lot of convertions, but it is bad practice for clean code, as v2 has superior methods.
But in cases where one label is used for multiple events, this could become possible complex...
And I haven`t taken into account that in V1, it is also possible to call functions if the glabel does not exist, those functions should be converted to a (*) parameter.
Maybe it is a better idea to try to set global variables like A_GuiEvent before calling the function. is it possible to do that inside the onevent line?
Because a label does not work the same as a function, there are some situations that are hard to convert, certainly if a label is used inside another label.
As V2 gui works on a whole different way, it is not easy to convert complex scripts. But it is already interesting to be able to convert the simple code and accept that you need to correct the handling a little bit...
Below an example of a conversion where it goes wrong, but it is suprising how good it already works:
V1 code
Code: Select all
Gui, Add, Text,, Pick a file to launch from the list below.`nTo cancel, press ESCAPE or close this window.
Gui, Add, ListBox, vMyListBox gMyListBox w640 r10
Gui, Add, Button, Default, OK
Loop, C:\*.* ; Change this folder and wildcard pattern to suit your preferences.
{
GuiControl,, MyListBox, %A_LoopFileFullPath%
}
Gui, Show
return
MyListBox:
if (A_GuiEvent != "DoubleClick")
return
; Otherwise, the user double-clicked a list item, so treat that the same as pressing OK.
; So fall through to the next label.
ButtonOK:
GuiControlGet, MyListBox ; Retrieve the ListBox's current selection.
MsgBox, 4,, Would you like to launch the file or document below?`n`n%MyListBox%
IfMsgBox, No
return
; Otherwise, try to launch it:
Run, %MyListBox%,, UseErrorLevel
if (ErrorLevel = "ERROR")
MsgBox Could not launch the specified file. Perhaps it is not associated with anything.
return
GuiClose:
GuiEscape:
ExitApp
v2 code:
Code: Select all
myGui := Gui()
myGui.OnEvent("Close", GuiClose)
myGui.OnEvent("Escape", GuiClose)
myGui.Add("Text", , "Pick a file to launch from the list below.`nTo cancel, press ESCAPE or close this window.")
ogcMyListBox := myGui.Add("ListBox", "vMyListBox w640 r10")
ogcMyListBox.OnEvent("DoubleClick", MyListBox.Bind("DoubleClick"))
ogcButtonOK := myGui.Add("Button", "Default", "OK")
ogcButtonOK.OnEvent("Click", ButtonOK.Bind("Normal"))
Loop Files, "C:\*.*" ; Change this folder and wildcard pattern to suit your preferences.
{
ogcMyListBox.Add([A_LoopFilePath])
}
myGui.Show()
return
MyListBox(A_GuiEvent, GuiCtrlObj, Info, *)
{ ; V1toV2: Added bracket
if (A_GuiEvent != "DoubleClick")
return
} ; V1toV2: Added Bracket before label
; Otherwise, the user double-clicked a list item, so treat that the same as pressing OK.
; So fall through to the next label.
ButtonOK(A_GuiEvent, GuiCtrlObj, Info, *)
{ ; V1toV2: Added bracket
MyListBox := ogcMyListBox.Text ; Retrieve the ListBox's current selection.
msgResult := MsgBox("Would you like to launch the file or document below?`n`n" MyListBox, , 4)
if (msgResult = "No")
return
; Otherwise, try to launch it:
{ ErrorLevel := "ERROR"
Try ErrorLevel := Run(MyListBox, , "", )
}
if (ErrorLevel = "ERROR")
msgResult := MsgBox("Could not launch the specified file. Perhaps it is not associated with anything.")
return
} ; V1toV2: Added bracket before function
GuiClose(*)
{ ; V1toV2: Added bracket
GuiEscape:
ExitApp
} ; Added bracket in the end