如何实现多复选框选中时动态设置StartUp文本框的对应值?
问题与解决方案
需求说明
- 无复选框被选中时,StartUp文本框保留原有内容
- SUDAS复选框选中时,将“DAS DEGD”添加至StartUp.Value
- SUDAS和SUCM同时选中时,StartUp.Value显示“DAS DEGD, CM DEGD”
- 其他复选框(SUEW、SURDR)选中时,对应添加“EW DEGD”、“RADAR DEGD”,多选中时用逗号分隔
原始代码
If SUEW.Value = True And SUDAS.Value = False And SUCM.Value = False And SURDR.Value = False Then StartUp.Value = "EW DEGD" ElseIf SUDAS.Value = True And SUEW.Value = False And SUCM.Value = False And SURDR.Value = False Then StartUp.Value = "DAS DEGD" ElseIf SUCM.Value = True And SUEW.Value = False And SUDAS.Value = False And SURDR.Value = False Then StartUp.Value = "CM DEGD" ElseIf SURDR.Value = True And SUDAS.Value = False And SUCM.Value = False And SUEW.Value = False Then StartUp.Value = "RADAR DEGD" 'All ElseIf SUDAS.Value And SUEW.Value And SUCM.Value And SURDR = True Then StartUp.Value = "EW DEGD, DAS DEGD, CM DEGD, RADAR DEGD" '3 Checks ElseIf SUEW.Value = True And SUDAS.Value = True And SURDR.Value = True And SUCM.Value = False Then StartUp.Value = "EW DEGD, DAS DEGD, RADAR DEGD" ElseIf SUEW.Value = True And SUCM.Value = True And SURDR.Value = True And SUDAS.Value = False Then StartUp.Value = "EW DEGD, CM DEGD, RADAR DEGD" ElseIf SUDAS.Value = True And SUCM.Value = True And SURDR.Value = True And SUEW.Value = False Then StartUp.Value = "DAS DEGD, CM DEGD, RADAR DEGD" ElseIf SUEW.Value = True And SUDAS.Value = True And SUCM.Value = True And SURDR.Value = False Then StartUp.Value = "EW DEGD, DAS DEGD, CM DEGD" '2 Checks ElseIf SUDAS.Value And SUEW.Value = True Then StartUp.Value = "EW DEGD, DAS DEGD" ElseIf SUEW.Value And SUCM.Value = True Then StartUp.Value = "EW DEGD, CM DEGD" ElseIf SUDAS.Value And SUCM.Value = True Then StartUp.Value = "DAS DEGD, CM DEGD" ElseIf SURDR.Value And SUEW.Value = True Then StartUp.Value = "EW DEGD, RADAR DEGD" ElseIf SURDR.Value And SUCM.Value = True Then StartUp.Value = "CM DEGD, RADAR DEGD" ElseIf SUDAS.Value And SURDR.Value = True Then StartUp.Value = "DAS DEGD, RADAR DEGD" 'False ElseIf SUEW.Value = False Then StartUp.Value = "" ElseIf SUDAS.Value = False Then StartUp.Value = "" ElseIf SUCM.Value = False Then StartUp.Value = "" ElseIf SURDR.Value = False Then StartUp.Value = "" End If
简化尝试的代码(存在问题)
Dim SUChkBx As String SUChkBx = "" If SUDAS.Value = True Then SUChkBx = SUChkBx + " DAS DEGD" If SUEW.Value = True Then SUChkBx = SUChkBx + " EW DEGD" If SUCM.Value = True Then SUChkBx = SUChkBx + " CM DEGD" If SURDR.Value = True Then SUChkBx = SUChkBx + " RADAR DEGD" StartUp.Value = SUChkBx End If End If End If End If End If
修正后的简化代码
Dim SUChkBx As String SUChkBx = "" '逐个判断复选框,收集对应文本 If SUDAS.Value Then SUChkBx = SUChkBx & "DAS DEGD, " If SUEW.Value Then SUChkBx = SUChkBx & "EW DEGD, " If SUCM.Value Then SUChkBx = SUChkBx & "CM DEGD, " If SURDR.Value Then SUChkBx = SUChkBx & "RADAR DEGD, " '去除末尾多余的逗号和空格 If Len(SUChkBx) > 0 Then SUChkBx = Left(SUChkBx, Len(SUChkBx) - 2) Else '无复选框选中时保留原有内容 SUChkBx = StartUp.Value End If StartUp.Value = SUChkBx
代码说明
- 摒弃原始代码中大量冗余的分支判断,通过逐个检查复选框状态动态拼接文本
- 处理多选中时的格式问题,自动去除末尾多余的
, - 严格满足“无复选框选中时保留原有内容”的需求,而非清空文本框
- 修复简化尝试代码中的
If语句嵌套错误,改用独立判断逻辑,结构更清晰
内容的提问来源于stack exchange,提问作者Beau
相关产品推荐
相关产品推荐

