我有一个带有列表视图的Userform。在此列表视图中,一列定义了复选框。因此,当用户点击多行的复选框时,我想在标签的标题中显示所选的ID,例如:
"You are selected: 848, 12133, 414"
我怎样才能做到这一点?
答案 0 :(得分:1)
编辑:我刚看到您正在谈论列表视图......
以下是ExcelForum上AlphaFrog修改后的代码段:
Private Sub Listview1_ItemCheck(ByVal Item As MSComctlLib.ListItem)
Dim si As MSComctlLib.ListSubItem
For Each si In Item.ListSubItems
si.ForeColor = IIf(Item.Checked, vbBlue, vbBlack)
Next
End Sub
他所做的是根据复选框的状态(Item.Checked
)更改Fontcolour。
您可以使用下面原始回复的逻辑来获得所需的结果。
原始回复:
这是一个包含3个复选框的示例,我只会发布Checkbox1的代码以及初始化。 Checkbox1_Click中的代码一对一地进入Checkbox2_Click和Checkbox3_Click。
Dim cnt As Integer
Dim checked() As Integer
Dim tmp As Integer
Private Sub CheckBox1_Click()
cnt = 0
ReDim checked(1 To 3)
tmp = 1
For i = 1 To 3
If UserForm1.Controls("Checkbox" & i).Value = True Then
checked(tmp) = i
cnt = cnt + 1
tmp = tmp + 1
End If
Next i
Select Case cnt
Case 0
Label1.Caption = ""
Case 1
Label1.Caption = "You have selected: " & UserForm1.Controls("CheckBox" & checked(tmp - 1)).Caption
Case Is > 1
Label1.Caption = "You have selected: " & UserForm1.Controls("CheckBox" & checked(1)).Caption
For t = 2 To tmp - 1
Label1.Caption = Label1.Caption & ", " & UserForm1.Controls("CheckBox" & checked(t)).Caption
Next t
End Select
End Sub
'same code here for Checkbox2 and 3
Private Sub UserForm_Initialize()
For i = 1 To 3
UserForm1.Controls("CheckBox" & i).Value = False
Next i
Label1.Caption = ""
End Sub
概念证明: