Archive: Check a control


Check a control
I'm using nsDialogs.. I have created a custom page where the user can load a file (with SelectFileDialog) and now I need to make a check, when the user click Next, to verify if the file selected is valid or not. If is not valid (if the name of the file doesn't contain the string "Pack") I need to open a message that ask to the user to select a correct file. Thanks for help!


You will need a callback function.
I guess you'll find all the info you need into nsDialogs documentation.


But how can I set to don't leave the custom page if a variable has particular values?


If you're using nsDialogs, then it won't leave the page anyway (that was a behavior of InstallOptions(2/Ex)). If you want to enable/disable the 'Next' button, you can do so in code as well.

The following code simply checks folder name validity - should be simple enough to adjust to check whether the specified file has 'pack' in it;


!include "winmessages.nsh"
!include "LogicLib.nsh"
!include "nsDialogs.nsh"

Outfile "test.exe"

var dialog
var hwnd
var null

!macro RedrawControl control
Push $0
StrCpy $0 ${control}
System::Call "user32::InvalidateRect(i,i,i)i (r0, 0, 1)"
Pop $0
!macroend
!define RedrawControl `!insertmacro RedrawControl`

var InstallDir ; Use a variable separate from $INSTDIR to by-pass automatic adjustments

Function .onInit
StrCpy $InstallDir "$PROGRAMFILES\SomeFolder"
FunctionEnd


Page custom page.settings
Page InstFiles

var NSD_InstallDir
var NSD_InstallDirStatus
Function page.settings
nsDialogs::Create 1018
Pop $dialog

${NSD_CreateText} 3% 3% 89% 8% "$InstallDir"
Pop $NSD_InstallDir
${NSD_OnChange} $NSD_InstallDir page.settings.installdir.onchange

${NSD_CreateButton} 92% 3% 5% 8% "..."
Pop $hwnd
${NSD_OnClick} $hwnd page.settings.installdir.onclick

${NSD_CreateLabel} 3% 12% 100% 8% "status: "
Pop $NSD_InstallDirStatus

; Initialization
Push $NSD_InstallDir
Call page.settings.installdir.onchange

nsDialogs::Show
Abort
FunctionEnd

Function page.settings.installdir.onchange
Pop $hwnd
${NSD_GetText} $hwnd $InstallDir

; Kill trailing backslash
StrCpy $0 $InstallDir 1 -1
${If} $0 == "\"
StrLen $0 $InstallDir
IntOp $0 $0 - 1
StrCpy $InstallDir $InstallDir $0
${EndIf}

StrCpy $0 $InstallDir

; Kill invalid characters
Push $InstallDir
Exch $EXEDIR
Exch $EXEDIR
Pop $InstallDir

GetDlgItem $1 $HWNDPARENT 1 ; Next button

${If} $InstallDir == ""
${NSD_SetText} $NSD_InstallDirStatus "status: No destination folder specified"
EnableWindow $1 0
${ElseIf} $InstallDir != $0
${NSD_SetText} $NSD_InstallDirStatus "status: Destination folder contains disallowed characters"
EnableWindow $1 0
${ElseIf} ${FileExists} "$InstallDir\*.*"
${NSD_SetText} $NSD_InstallDirStatus "status: OK - Destination folder exists"
EnableWindow $1 1
${Else}
${NSD_SetText} $NSD_InstallDirStatus "status: OK - Destination folder will be created"
EnableWindow $1 1
${EndIf}
FunctionEnd

Function page.settings.installdir.onclick
nsDialogs::SelectFolderDialog "Please select the destination folder"
Pop $0
${If} $0 != "error"
${NSD_SetText} $NSD_InstallDir $0 ; This will automatically trigger page.settings.installdir.onchange
${EndIf}
FunctionEnd

Section ""
SectionEnd