Archive: Save listbox items on page leave


Save listbox items on page leave
  Hi,

what will be the best method to save listbox items on leaving the (custom) page so that they could be restored when the user returns to the page?

Unfortunately NSIS doesn't support arrays (and only for this I don't want to use the array plugin - can only use headers and plugins that are included in the NSIS package!).

Writing to a (temp) file is also unwanted.
Any other possibility?

Thanks,
Gunther

BTW: Is there any (understandable) explanation of nsDialogs::GetUserData?


Hello.

I had the same problem with the saving results of checkboxes. I used successfully global variables.
http://forums.winamp.com/showthread.php?t=320238


Hi,

thanks for your reply, but unfortunately it doesn't help me much ..., because if you know the number of elements in advance and you can adress each element then it will be no problem.

But i don't know how many entries an user will add to the listbox.
I can use ${NSD_LB_GetCount} to get the number but I don't know how to adress and store each entry?

Gunther


as far as addressing listboxes, go...
http://nsis.sourceforge.net/Move_data_between_ListBoxes

as far as storing the values goes... why not just write them out to a flat text file, and read them back in from that?
there's many alternatives, of course. If you don't want to use the Array plugin, for example, you could still create a pseudo-array using a string value with a delimiter (|, for example), and use StrTok from the String Functions header to get each one piecewise. It's more code than just dumping it to a text file but doesn't require file I/O.

as for SetUserData and GetUserData... they allow you to store/restore data (a string, basically) as associated with an hwnd. They're useful temporary holders, or - for example - storing a URL with an NSD_CreateLink element so that upon code review you can immediately see what URL is associated with that link (and the code handling its OnClick can simply read that value).
Of course the down side is that once the hwnd is gone (i.e. you leave the page or the element is destroyed by any other means), so is the data - so it's not good for session-wide storage :)
These macros ease working with those two functions a little:
http://nsis.sourceforge.net/NsDialogs_UserData


Animaether,

many thanks for your detailed answer - it really helped me out! :up:

Originally posted by Animaether
as far as addressing listboxes, go...
http://nsis.sourceforge.net/Move_data_between_ListBoxes
OK, found also a helpful thread in the forum - http://forums.winamp.com/showpost.ph...5&postcount=10

There are two things to take care of:
  1. set the current selection to each item by using LB_SETCURSEL
  2. use the SystemCall instead of SendMessage
I ended up with the following leave function for the page (I am using the Unicode version):

CHART_DIR_TMP_FILE

>Function Page_SetConfig_Leave
${NSD_LB_GetCount} $ListBox_SetConfig $0
${If} $0 > 0
ClearErrors
FileOpen $R0 $TEMPocpn_chart_dirs
.tmp w
IfErrors done
IntOp$0 $0 - 1
${For} $1 0 $0
SendMessage $ListBox_SetConfig${LB_SETCURSEL} $1 0
System::Call "User32::SendMessage(i $ListBox_SetConfig, i ${LB_GETTEXT}, i $1, t .r2)"
;MessageBox MB_OK|MB_ICONEXCLAMATION "Page_SetConfig_Leave$\r$\nLB_GETITEMDATA: $2" ;#DEBUG
FileWriteUTF16LE $R0 $2
FileWriteUTF16LE $R0 "$\r$\n"
${Next}
FileClose $R0
StrCpy $CHART_DIR_TMP_FILE 1
${EndIf}
done:
>FunctionEnd
>
as far as storing the values goes... why not just write them out to a flat text file, and read them back in from that?
As you can see I decided to do so. ;)
It's by far the easiest way ...

there's many alternatives, of course. If you don't want to use the Array plugin, for example, you could still create a pseudo-array using a string value with a delimiter (|, for example), and use StrTok from the String Functions header to get each one piecewise. It's more code than just dumping it to a text file but doesn't require file I/O.
Didn't checked it, but isn't there a great risk to "conflict" with NSIS_MAX_STRLEN when using a pseudo-array (path strings could be long)?

So the only drawback/ side effect when storing the items to a text file is that I somehow have to delete the file if the user cancels the installation after the file was written (that's what the variable $CHART_DIR_TMP_FILE is for).
Do you have just another tip how to achieve this best?

as for SetUserData and GetUserData...
Thanks for the explanation - think I got it now.

Gunther

Originally posted by Netsurfer24
Didn't checked it, but isn't there a great risk to "conflict" with NSIS_MAX_STRLEN when using a pseudo-array (path strings could be long)?
If you're putting paths into the listbox, then yes - not recommended :)
For more typical use, it -can- be okay.. but the temp file is the most robust alternative to the Array plugin if you don't need the Array plugin's fancy features :)

Originally posted by Netsurfer24
So the only drawback/ side effect when storing the items to a text file is that I somehow have to delete the file if the user cancels the installation after the file was written (that's what the variable $CHART_DIR_TMP_FILE is for).
Do you have just another tip how to achieve this best?
You can always just delete the file when the installer exits. Presuming you'll have a GUI (not much use for the listboxes in a silent installer), you can use .onGuiEnd. Otherwise, you can delete it in .onInstSuccess and .onInstFailed.

That said - if you put your temp file into the PluginsDir (InitPluginsDir -> $PLUGINSDIR), then it should automatically get deleted by NSIS (presuming you closed any file handles to the thing) :)

Just another question concerning storing variables (in my case path values) in a flat file.

The variables (path values) are generated by scanning the HDD of the user. Every path found (there must exist two specific files) is
- added to the listbox
- written to the file

Now the user may select certain list-items. The directories with the respective path values are then deleted.

After they were deleted I need to remove/ delete these path values also
- from the listbox (no big deal as I get the value from it)
- from the file

The latter is what I'd like to ask for.
How to delete certain values from the file?

The values are stored each one in a line followed by $\r$\n.

Thanks

Gunther


Have you tried google?

"nsis delete line from file"