【问题标题】:NSIS - Radiobutton label not matching the one selectedNSIS - 单选按钮标签与所选标签不匹配
【发布时间】:2015-09-16 20:47:14
【问题描述】:
!include "nsDialogs.nsh"
!include "LogicLib.nsh"

Name "Test "
OutFile Setup.exe

XPStyle on
Page Custom radioButton radioButtonClick
Page instfiles

var Group1Radio1
var Group1Radio2
var dialog
var hwnd
var label

Function radioButton
nsDialogs::Create 1018
Pop $dialog
${NSD_CreateLabel} 0 0 100% 6% "Please choose"
Pop $label
${NSD_CreateRadioButton} 0 12% 40% 6% "1"
Pop $Group1Radio1
${NSD_AddStyle} $Group1Radio1 ${WS_GROUP}
${NSD_OnClick} $Group1Radio1 radioButtonClick
${NSD_CreateRadioButton} 0 20% 40% 6% "2"
Pop $Group1Radio2
${NSD_OnClick} $Group1Radio2 radioButtonClick

nsDialogs::Show
FunctionEnd

Function radioButtonClick
Pop $hwnd
${If} $hwnd == $Group1Radio1
    ${NSD_CreateLabel} 0 40% 40% 6% "1 Selected"
    ${NSD_OnChange} $Group1Radio1 radioButton
${ElseIf} $hwnd == $Group1Radio2
    ${NSD_CreateLabel} 0 40% 40% 6% "2 Selected"
    ${NSD_OnChange} $Group1Radio2 radioButton

${EndIf}

FunctionEnd

Section 
SetOutPath "$DESKTOP"
SectionEnd

当此代码运行时,选择的第一个按钮的名称会更正显示在“Function radioButtonClick”中的标签上,但是当您在此之后选择其他内容时,它不会更新,并且按钮标签会被交换.

所以基本上,会发生什么:

单击单选按钮 1 -> 显示“1 个已选择” 然后, 单击单选按钮 2 -> 没有任何反应。 然后, 再次单击单选按钮 1 -> 显示“2 selected” 最后, 再次单击单选按钮 2 -> 显示“1 selected”

我该如何解决这个问题?

提前谢谢你。

【问题讨论】:

    标签: button nsis


    【解决方案1】:

    您的代码没有多大意义,每次单选按钮更改时您都会创建一个新标签,并且每次单击时都会注册一个新的 NSD_OnChange 事件处理程序,并且此处理程序会尝试在旧页面之上创建另一个页面一,这是完全不受支持的行为!

    有很多方法可以编码,这里有 3 个例子:

    !include "nsDialogs.nsh"
    !include "LogicLib.nsh"
    
    Page Custom radioButtonExamplePageMethod1
    Page Custom radioButtonExamplePageMethod2
    Page Custom radioButtonExamplePageMethod3
    Page instfiles
    
    var Group1Radio1
    var Group1Radio2
    var dialog
    var hwnd
    var label
    
    Function radioButtonExamplePageMethod1
    nsDialogs::Create 1018
    Pop $dialog
    ${NSD_CreateLabel} 0 0 100% 6% "Please choose"
    Pop $0 ; Don't care about this handle
    ${NSD_CreateRadioButton} 0 12% 40% 6% "1"
    Pop $Group1Radio1
    ${NSD_OnClick} $Group1Radio1 radioButtonClicked_Method1
    ${NSD_CreateRadioButton} 0 20% 40% 6% "2"
    Pop $Group1Radio2
    ${NSD_OnClick} $Group1Radio2 radioButtonClicked_Method1
    ${NSD_CreateLabel} 0 40% 40% 6% ""
    Pop $label
    ; You could do 
    ; SendMessage $Group1Radio1 ${BM_CLICK} 0 0
    ; here to simulate a click for the inital radio button state
    nsDialogs::Show
    FunctionEnd
    
    Function radioButtonClicked_Method1
    Pop $hwnd
    ${If} $hwnd == $Group1Radio1
        ${NSD_SetText} $label "1 Selected"
    ${ElseIf} $hwnd == $Group1Radio2
        ${NSD_SetText} $label "2 Selected"
    ${EndIf}
    FunctionEnd
    
    
    Function radioButtonExamplePageMethod2
    nsDialogs::Create 1018
    Pop $dialog
    ${NSD_CreateLabel} 0 0 100% 6% "Please choose"
    Pop $0 ; Don't care about this handle
    ${NSD_CreateLabel} 0 40% 40% 6% ""
    Pop $label
    ${NSD_CreateRadioButton} 0 12% 40% 6% "1"
    Pop $Group1Radio1
    ${NSD_OnClick} $Group1Radio1 radioButtonClicked_Method2_Radio1
    ${NSD_CreateRadioButton} 0 20% 40% 6% "2"
    Pop $Group1Radio2
    ${NSD_OnClick} $Group1Radio2 radioButtonClicked_Method2_Radio2
    nsDialogs::Show
    FunctionEnd
    
    Function radioButtonClicked_Method2_Radio1
    Pop $0
    ${NSD_SetText} $label "1 Selected"
    FunctionEnd
    Function radioButtonClicked_Method2_Radio2
    Pop $0
    ${NSD_SetText} $label "2 Selected"
    FunctionEnd
    
    
    Function radioButtonExamplePageMethod3
    nsDialogs::Create 1018
    Pop $dialog
    ${NSD_CreateLabel} 0 0 100% 6% "Please choose"
    Pop $0 ; Don't care about this handle
    ${NSD_CreateLabel} 0 40% 40% 6% ""
    Pop $label
    ${NSD_CreateRadioButton} 0 12% 40% 6% "1"
    Pop $Group1Radio1
    nsDialogs::SetUserData $Group1Radio1 "1 Selected"
    ${NSD_OnClick} $Group1Radio1 radioButtonClicked_Method3
    ${NSD_CreateRadioButton} 0 20% 40% 6% "2"
    Pop $Group1Radio2
    nsDialogs::SetUserData $Group1Radio2 "2 Selected"
    ${NSD_OnClick} $Group1Radio2 radioButtonClicked_Method3
    nsDialogs::Show
    FunctionEnd
    
    Function radioButtonClicked_Method3
    Pop $0
    nsDialogs::GetUserData $0
    Pop $1
    ${NSD_SetText} $label $1
    FunctionEnd
    

    【讨论】:

    • @niamleeson:不用说谢谢,你可以把它标记为答案;)
    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2012-10-16
    相关资源
    最近更新 更多