以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助]有四个CheckBox控件,只能选两个  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=178573)

--  作者:tygzjsl
--  发布时间:2022/7/10 20:25:00
--  [求助]有四个CheckBox控件,只能选两个
有四个CheckBox控件,只能选两个,原来代码如下

If e.Form.Controls("CheckBox1").Checked = True AndAlso e.Form.Controls("CheckBox2").Checked = True Then

    Filter = Filter & " and 班级 in (\'一班\',\'二班\') "

End If

If e.Form.Controls("CheckBox1").Checked = True AndAlso e.Form.Controls("CheckBox3").Checked = True Then

    Filter = Filter & " and 班级 in (\'一班\',\'三班\') "

End If

现在选了一个或三个以上,就会出现不想要的结果,怎么样做到选了一个或三个以上CheckBox就会弹出错误提示"只能选两个"

请各位大神帮忙,谢谢!

[此贴子已经被作者于2022/7/10 20:38:59编辑过]

--  作者:有点蓝
--  发布时间:2022/7/10 21:32:00
--  
dim str as string
dim i as integer
dim ss() as string = {"一班","二班","三班","四班"}
for j as integer =0 to ss.length - 1
if i>2 then
msgbox("只能选两个")
return
end if
if e.Form.Controls("CheckBox" & (j+1)).Checked then
str = str & ",\'" & ss(j) & "\'"
i = i  + 1
en dif
next
msgbox(str)
Filter = Filter & " and 班级 in (" & str.trim(",") & ") "

--  作者:tygzjsl
--  发布时间:2022/7/10 22:09:00
--  
非常感谢老师,只是我的表达不是很清楚,CheckBox1-4并不是和一班到四班对应的.
是这样的

If e.Form.Controls("CheckBox1").Checked = True AndAlso e.Form.Controls("CheckBox2").Checked = True Then

    Filter = Filter & " and 年级 in (\'一班\',\'二班\',\'一班或五班\',\'五班和七班\') "

End If

If e.Form.Controls("CheckBox1").Checked = True AndAlso e.Form.Controls("CheckBox3").Checked = True Then

    Filter = Filter & " and 年级 in (\'一班\',\'三班\',\'五班\',\'六班\',\'全部\') "


End If

也就是年级是一列,通过控件CheckBox选择的班级是另一列

辛苦了!


--  作者:有点蓝
--  发布时间:2022/7/10 22:19:00
--  
那就想办法把控件的名称和班级对应起来,方便自己的代码处理。如果没有办法对应,只能像上面一样处理了

判断只能选两个参考

dim i as integer
for j as integer =1 to 4
if e.Form.Controls("CheckBox" & (j+1)).Checked then
i = i+1
end if
endif
if i>2 then
msgbox("只能选两个")
return
end if
后续生成条件
[此贴子已经被作者于2022/7/10 23:00:49编辑过]

--  作者:tygzjsl
--  发布时间:2022/7/10 22:54:00
--  
我试试看辛苦啦,非常感谢!!!