我如何知道在选中列表框中更改了哪个项目的状态(选中/未选中)?我知道当一个项目的状态改变时如何触发一个事件,但我不知道如何告诉哪个项目。有什么建议么?
PS我正在使用带有.net 4.5的Visual Basic
我如何知道在选中列表框中更改了哪个项目的状态(选中/未选中)?我知道当一个项目的状态改变时如何触发一个事件,但我不知道如何告诉哪个项目。有什么建议么?
PS我正在使用带有.net 4.5的Visual Basic
ItemCheckEventArgs.Index
Checkout http://msdn.microsoft.com/en-us/library/system.windows.forms.itemcheckeventargs.index.aspx会参考您想要的
在底部:-
Private Sub ListView1_ItemCheck1(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
Handles ListView1.ItemCheck
If (e.CurrentValue = CheckState.Unchecked) Then
price += Double.Parse( _
Me.ListView1.Items(e.Index).SubItems(1).Text)
ElseIf (e.CurrentValue = CheckState.Checked) Then
price -= Double.Parse( _
Me.ListView1.Items(e.Index).SubItems(1).Text)
End If
' Output the price to TextBox1.
TextBox1.Text = CType(price, String)
End Sub
选中列表框的CheckedItems
集合将为您提供列表中被选中的每个项目,通常这与按钮事件相关联,如下所示:
Private Sub WhatIsChecked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WhatIsChecked.Click
For Each itemChecked In CheckedListBox1.CheckedItems
' Do something with each checked item
Next
End Sub
注意:在本例中,有一个名为 的按钮WhatIsChecked
。
如果您只想知道列表中的单个复选框何时更改,请使用以下命令:
Private Sub CheckedListBox1_ItemCheck(sender as Object, e as ItemCheckEventArgs) _
Handles CheckedListBox1.ItemCheck
Dim messageBoxVB as New System.Text.StringBuilder()
messageBoxVB.AppendFormat("{0} = {1}", "Index", e.Index)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "NewValue", e.NewValue)
messageBoxVB.AppendLine()
messageBoxVB.AppendFormat("{0} = {1}", "CurrentValue", e.CurrentValue)
messageBoxVB.AppendLine()
MessageBox.Show(messageBoxVB.ToString(),"ItemCheck Event")
End Sub