Autoit global scope
A global scope problem is found in programming. The global variable cannot be incremented normally in the while... Wend, and only the value before the loop can be obtained.
1; filename variablescope01.au3
2; Purpose: scope of global variables
3; created by: @ kysnail Il at home
4; note:
5; the global variable $ row is defined here, but if $ ROW = $ row + 1 is not added in the while... Wend loop, it will always stay on a row for Refresh.
6;
7; Resource:
8;
9;
10; Press ESC to terminate script, pause/break to "pause"
11
12 # include <excel. au3>
13 # include <guiconstantsex. au3>
14 # include <guiconstants. au3>
15
16; set the hotkey-> win + O
17 hotkeyset ("# O", "nextrecord ")
18
19 global $ ROW = 2
20 global $ oexcel = _ excelbooknew (); Create a worksheet
21 _ excelwritecell ($ oexcel, "No.", 1, 1)
22
23 # region ### start GUI section ###
24 $ form1 = guicreate ("Start entry", 150, 80,-1,-1)
25 global $ bon1 = guictrlcreatebutton ("Next group", 30, 24, 97, 33, 0)
26
27 guisetstate (@ sw_show)
28 # endregion ### end GUI section ###
29
30 While 1
31 $ nmsg = guigetmsg ()
32
33 switch $ nmsg
34 case $ gui_event_close
35 exit
36 case $ bon1
37 nextrecord ($ row)
38; $ ROW = $ row + 1
39 msgbox (0, "$ row value", "$ row value =" & $ row)
40 endswitch
41 Wend
42
43 func nextrecord ($ row)
44 _ ExcelWriteCell ($ oExcel, $ row-1, $ row, 1)
45 $ row = $ row + 1
46; MsgBox (0, "-------------" & $ row ,"----------------------------")
47 EndFunc
Operation Analysis
1. Click Run.
2. Second click
3. Third click
4. Fourth click
5. Use the shortcut key "win + o" this time to run properly.
To run the command correctly, modify the following code:
1 While 1
2 $nMsg = GUIGetMsg()
3
4 Switch $nMsg
5 Case $GUI_EVENT_CLOSE
6 Exit
7 Case $Bon1
8 NextRecord($row)
9 $row = $row + 1
10 MsgBox(0, "$row value", "$row value = " & $row)
11 EndSwitch
12 WEnd
In this way, you can obtain or modify the value of the global variable $ row when you click the button.