In my question, some buttons are created by amount given by parameter numOfButtons. When a button is clicked, its holder frame is closed and the index( begins from 0) is returned. But I found that no matter which button is clicked, only the last(in the case of numOfButtons==3, the last index is 2) is returned. So what is the problem? Thanks
import winim/com
import wNim
proc msgBox(numOfButtons:int = 1): int {.inline, discardable.} =
let
frame = Frame( title="test" )
panel = Panel(frame)
(widPanel, hiPanel) = panel.getClientSize()
sizeButton = (int(widPanel/6), hiPanel)
yDistance = 10
var
tmpButton: wButton
(lastX, lastY) = (0, 0)
clicked:int = 0
for idxButton in 0 .. (numOfButtons-1):
tmpButton = Button(panel,
label="b" & $idxButton,
pos=(lastX, lastY),
size=sizeButton
)
tmpButton.wEvent_Button do (event: wEvent):
clicked = idxButton
frame.endModal()
frame.close()
lastX += sizeButton[0] + yDistance
frame.center()
frame.showModal()
return clicked
var
app = App()
frame = Frame()
var f = msgBox(numOfButtons=3)
echo "pressed button ", f
frame.center()
frame.show()
app.mainLoop()
try this:
import winim/com
import wNim
proc msgBox(numOfButtons:int = 1): int {.inline, discardable.} =
let
frame = Frame( title="test" )
panel = Panel(frame)
(widPanel, hiPanel) = panel.getClientSize()
sizeButton = (int(widPanel/6), hiPanel)
yDistance = 10
var
tmpButton: wButton
(lastX, lastY) = (0, 0)
clicked:int = 0
for idxButton in 0 .. (numOfButtons-1):
tmpButton = Button(panel,
label="b" & $idxButton,
pos=(lastX, lastY),
size=sizeButton
)
closureScope:
var idxButtonLoc = idxButton
tmpButton.wEvent_Button do (event: wEvent):
clicked = idxButtonLoc
frame.endModal()
frame.close()
lastX += sizeButton[0] + yDistance
frame.center()
frame.showModal()
return clicked
var
app = App()
frame = Frame()
var f = msgBox(numOfButtons=3)
echo "pressed button ", f
frame.center()
frame.show()
app.mainLoop()
see: https://nim-lang.org/docs/manual.html#closures-creating-closures-in-loops