网上很多关于实现这个功能的方法,不是代码太多,就是在VBNET根本实现不了,经实验总结以下内容
1、ListView控件的HideSelection必须为True
2、失去焦点事件要用Validating,而不是Validated
3、代码如下:
'要设置:ListView1.HideSelection=True
Private SelectItem As ListViewItem = Nothing '记住选择的Item
Private Sub ListView1_Validating(sender As Object, e As CancelEventArgs) Handles ListView1.Validating
'失去焦点时高亮处理
SelectItem = sender.FocusedItem
If SelectItem Is Nothing Then Exit Sub
SelectItem.BackColor = SystemColors.Highlight
SelectItem.ForeColor = Color.White
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
'选择改变时恢复原来的Item的颜色
If SelectItem IsNot Nothing Then
SelectItem.backcolor = SystemColors.Window
SelectItem.forecolor = Color.Black
End If
End Sub