Search code examples
nsis

${NSD_GetText} always returns the empty string


Per the manual, I should be able to get the text of a text control with code like this:

${NSD_GetText} $TextBox $0
MessageBox MB_OK "You typed:$\n$\n$0"

I always get the empty string out of this call. In the code below, the text box shows "correct" but the details always show Contents:; if I comment the call to ${NSD_GetText}, I get Contents: wrong.

!include nsDialogs.nsh
!include LogicLib.nsh

Var Dialog
Var TextBox

Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles

Function nsDialogsPage
    StrCpy $0 "wrong"

    nsDialogs::Create 1018
    Pop $Dialog

    ${If} $Dialog == error
        Abort
    ${EndIf}

    ${NSD_CreateText} 0 12u 93% 12u "correct"
    Pop $TextBox

    nsDialogs::Show
FunctionEnd

Function nsDialogsPageLeave
FunctionEnd

Section
    ${NSD_GetText} $TextBox $0

    DetailPrint "Contents: $0"
SectionEnd

So I thought maybe the control didn't exist when I was trying to print its contents, and tried updating the text as it was typed into the control; that didn't help. It's implausible that NSIS is broken in this way, so what am I doing wrong?

!include nsDialogs.nsh
!include LogicLib.nsh

Var Dialog
Var TextBox
Var Text

Page custom nsDialogsPage nsDialogsPageLeave
Page instfiles

Function nsDialogsPage
    StrCpy $0 "wrong"

    nsDialogs::Create 1018
    Pop $Dialog

    ${If} $Dialog == error
        Abort
    ${EndIf}

    ${NSD_CreateText} 0 12u 93% 12u "correct"
    Pop $TextBox
    ${NSD_OnChange} $TextBox UpdateText

    nsDialogs::Show
FunctionEnd

Function nsDialogsPageLeave
FunctionEnd

Function UpdateText
    ${NSD_GetText} $TextBox $Text
FunctionEnd

Section
    DetailPrint "Contents: $Text"
SectionEnd

Solution

  • You are correct, the control does not exist in the Section so you have to get the contents while you are on the custom page.

    Your second example should work correctly if the user changes the text but not if they don't because the change event would not fire.

    You normally just read the content in the page leave callback:

    Var Dialog
    Var TextBox
    Var Text
    
    !include LogicLib.nsh
    !include nsDialogs.nsh
    Page custom nsDialogsPage nsDialogsPageLeave
    Page instfiles
    
    Function nsDialogsPage
        nsDialogs::Create 1018
        Pop $Dialog
        ${If} $Dialog == error
            Abort
        ${EndIf}
    
        ${NSD_CreateText} 0 12u 93% 12u "correct"
        Pop $TextBox
    
        nsDialogs::Show
    FunctionEnd
    
    Function nsDialogsPageLeave
    ${NSD_GetText} $TextBox $Text
    FunctionEnd
    
    
    Section
        DetailPrint "Contents: $Text"
    SectionEnd