- NSIS Discussion
- Iterate over all listbox values not just selected
Archive: Iterate over all listbox values not just selected
max0davis
17th September 2008 21:33 UTC
Iterate over all listbox values not just selected
I have an interface where I add IP addresses to a listbox using a textbox and I want to then iterate over all of those IP addresses in the "Leave" function of my page... how do I get each string in a listbox one at a time to do something with it? Here is some code I have so far:
Function VersusConcentratorPage
nsDialogs::Create /NOUNLOAD 1018
Pop $VERSUS_IP_DIALOG
${If} $VERSUS_IP_DIALOG == error
Abort
${EndIf}
${NSD_CreateGroupBox} 0u 0u 150u 140u "Versus Concentrator IP List"
${NSD_CreateListBox} 10u 15u 131u 76u ""
Pop $VERSUS_IP_LISTBOX
${NSD_CreateText} 10u 97u 131u 14u ""
Pop $VERSUS_IP_TEXTBOX
${NSD_CreateButton} 10u 117u 51u 16u "Add Conc IP"
Pop $VERSUS_IP_ADD_BUTTON
${NSD_OnClick} $VERSUS_IP_ADD_BUTTON VersusConcentratorAddIP
${NSD_CreateButton} 70u 117u 71u 16u "Remove Conc IP"
Pop $VERSUS_IP_REM_BUTTON
${NSD_OnClick} $VERSUS_IP_REM_BUTTON VersusConcentratorRemIP
${NSD_CreateGroupBox} 170u 0u 130u 140u "Instructions"
${NSD_CreateLabel} 190u 20u 91u 101u "Enter the IP of each versus conentrator here."
nsDialogs::Show
FunctionEnd
Function VersusConcentratorLeave
FunctionEnd
Function VersusConcentratorAddIP
${NSD_GetText} $VERSUS_IP_TEXTBOX $0
${NSD_LB_AddString} $VERSUS_IP_LISTBOX $0
${NSD_SetText} $VERSUS_IP_TEXTBOX ""
FunctionEnd
Function VersusConcentratorRemIP
${NSD_LB_GetSelection} $VERSUS_IP_LISTBOX $0
${NSD_LB_DelString} $VERSUS_IP_LISTBOX $0
FunctionEnd
Anders
17th September 2008 21:48 UTC
LB_GETCOUNT and LB_GETTEXT messages, see MSDN for details
max0davis
17th September 2008 22:07 UTC
Was looking at this page
Yeah, I was looking at this page:
http://msdn.microsoft.com/en-us/library/bb761313(VS.85).aspx
But I can't quite put it together.
Had something like this:
SendMessage $VERSUS_IP_LISTBOX ${LB_GETTEXT} 0 0 $1
System::Call user32::SendMessage(i$VERSUS_IP_LISTBOX,i${LB_GETTEXT},ir1,t.r1)
But it didn't seem to pull a string value out of the listbox, what am I doing wrong there? Do you see it?
I am not very familiar with the syntax of user32::SendMessage, what does that all mean? Is there a good doc on it?
max0davis
17th September 2008 22:39 UTC
Tried this:
SendMessage $VERSUS_IP_LISTBOX ${LB_GETCURSEL} 0 0 $0
${IF} $0 >= 0
IntOp $1 $0 + 0;
SendMessage $VERSUS_IP_LISTBOX ${LB_GETTEXT} $1 $0 $2
System::Call user32::SendMessage(i$VERSUS_IP_LISTBOX,i${LB_GETTEXT},i$INDEX,t.$0)
MessageBox MB_OK '0: $0, 1: $1, 2: $2'
${ENDIF}
But I got this warning:
1 warning:
unknown variable/constant "INDEX,t.$0)" detected, ignoring (C:\Documents and Settings\Max Davis\Desktop\setup.nsi:269)
And it doesn't work.
Assuming it is talking about $INDEX not being set... when I set it (Var INDEX) I got this:
1 warning:
Variable "INDEX" not referenced or never set, wasting memory!
And it still didn't work.
max0davis
17th September 2008 23:11 UTC
I was able to get the first string in the select box with this:
SendMessage $VERSUS_IP_LISTBOX ${LB_GETTEXT} 0 $0
System::Call user32::SendMessage(i$VERSUS_IP_LISTBOX,i${LB_GETTEXT},iR0,t.R0)
MessageBox MB_OK $0
What is a good way to now loop to get all of them?
Can someone give me an example syntax using:
Do[While|Until]..{ExitDo|Continue|Break}..Loop[While|Until]
From logicLib?
max0davis
17th September 2008 23:40 UTC
This is where I am currently at:
Function VersusConcentratorLeave
${NSD_LB_GetCount} $VERSUS_IP_LISTBOX $R0
IntOp $R1 $R1 & 0
IPLoop:
SendMessage $VERSUS_IP_LISTBOX ${LB_GETTEXT} 0 $0
System::Call user32::SendMessage(i$VERSUS_IP_LISTBOX,i${LB_GETTEXT},iR1,t.R1)
MessageBox MB_OK $0
IntOp $R1 $R1 + 1
StrCmp "$R1" "$R0" 0 End
GOTO IPLoop
End:
FunctionEnd
But it only outputs the last string in the listbox.
Anders
18th September 2008 13:02 UTC
in that code, the text would be in $R1, not $0. You can remove the non system plugin sendmessage, it is never going to work
max0davis
18th September 2008 17:06 UTC
The final working script
This is the final working script to add values to a listbox and then iterate over them in the leave:
Function VersusConcentratorPage
nsDialogs::Create /NOUNLOAD 1018
Pop $VERSUS_IP_DIALOG
${If} $VERSUS_IP_DIALOG == error
Abort
${EndIf}
${NSD_CreateGroupBox} 0u 0u 150u 140u "Versus Concentrator IP List"
${NSD_CreateListBox} 10u 15u 131u 76u ""
Pop $VERSUS_IP_LISTBOX
${NSD_CreateText} 10u 97u 131u 14u ""
Pop $VERSUS_IP_TEXTBOX
${NSD_CreateButton} 10u 117u 51u 16u "Add Conc IP"
Pop $VERSUS_IP_ADD_BUTTON
${NSD_OnClick} $VERSUS_IP_ADD_BUTTON VersusConcentratorAddIP
${NSD_CreateButton} 70u 117u 71u 16u "Remove Conc IP"
Pop $VERSUS_IP_REM_BUTTON
${NSD_OnClick} $VERSUS_IP_REM_BUTTON VersusConcentratorRemIP
${NSD_CreateGroupBox} 170u 0u 130u 140u "Instructions"
${NSD_CreateLabel} 190u 20u 91u 101u "Enter the IP of each versus conentrator here."
nsDialogs::Show
FunctionEnd
Function VersusConcentratorLeave
${NSD_LB_GetCount} $VERSUS_IP_LISTBOX $R3
IntOp $R2 $R2 & 0
Loop:
System::Call user32::SendMessage(i$VERSUS_IP_LISTBOX,i${LB_GETTEXT},iR2,t.R1)
messageBox MB_OK $R1 # DO SOMETHING WITH EACH ONE HERE
IntOp $R2 $R2 + 1
IntCmp $R2 $R3 0 Loop
FunctionEnd
Function VersusConcentratorAddIP
${NSD_GetText} $VERSUS_IP_TEXTBOX $0
${NSD_LB_AddString} $VERSUS_IP_LISTBOX $0
${NSD_SetText} $VERSUS_IP_TEXTBOX ""
FunctionEnd
Function VersusConcentratorRemIP
${NSD_LB_GetSelection} $VERSUS_IP_LISTBOX $0
${NSD_LB_DelString} $VERSUS_IP_LISTBOX $0
FunctionEnd
Anyone is free to reuse this code for their purpose. I used it as a way to collect IP addresses to create config file.