Search code examples
nsis

Clear text field value on click the checkbox nsis


I have TextField that is editable. But when the user click on the Checkbox, i need to disabled the TextField and as well as to clear the value of textfield. I disabled the field but not able to clear the TextField value. So please let me know how to do this.

Note : Clear value means if the TextField have any value then it is should be blank onClick the checkbox.

Code :

        ${NSD_CreateText} 50u 67u 200u 12u ""
        Pop $txtUsername
        ${NSD_SetText} $txtUsername $Text_txtUsername

        ${NSD_CreatePassword} 50u 80u 200u 12u ""
        Pop $pwdPassword
        ${NSD_SetText} $pwdPassword $Text_pwdPassword

        ${NSD_CreateCheckbox} 0 96u 100% 12u "&Use Windows Login (Domain account must have 'log on as a service' right.)"
        Pop $chkCheckbox
        ${NSD_OnClick} $chkCheckbox EnDisableWindowsLoginButton

Function EnDisableWindowsLoginButton
    Pop $chkCheckbox
    ${NSD_GetState} $chkCheckbox $sqlLogin
    ${If} $sqlLogin == 1
        EnableWindow $txtUsername 0
        EnableWindow $pwdPassword 0
    ${Else}
        EnableWindow $txtUsername 1
        EnableWindow $pwdPassword 1
    ${EndIf}

FunctionEnd

Solution

  • You can set the value of a textbox with NSD_SetText. Indeed, it looks like you're already using it to initialize those fields. Try something like this:

    Function EnDisableWindowsLoginButton
        Pop $chkCheckbox
        ${NSD_GetState} $chkCheckbox $sqlLogin
        ${If} $sqlLogin == 1
            ${NSD_SetText} $txtUsername ""
            ${NSD_SetText} $pwdPassword ""
            EnableWindow $txtUsername 0
            EnableWindow $pwdPassword 0
        ${Else}
            EnableWindow $txtUsername 1
            EnableWindow $pwdPassword 1
        ${EndIf}
    FunctionEnd
    

    If you don't want to loose the values entered by the user, you can StrCpy them to some temp variables and restore them if/when user unchecks the checkbox.