前面几节将ListView讲的差不多了,本节主要补充讲一些可能会很少用到的内容。
1、关于Listview中修改整行的数据:
通过 ListView1.Items(序号).BackColor和ListView1.Items(序号).ForeColor来设置背景色和前景色:
Private Sub Button6_Click(sender As Object,e As EventArgs) Handles Button6.Click If ListView1.Items.Count > 0 Then For i As Integer = 0 To ListView1.Items.Count - 1 If i Mod 2 = 0 Then ListView1.Items(i).BackColor = Color.FromArgb(255,0) ListView1.Items(i).ForeColor = Color.FromArgb(255,255,255) Else ListView1.Items(i).BackColor = Color.FromArgb(0,255) End If Next End If End Sub
只做演示,选的颜色比较突出,不过本人对颜色不敏感,大家将就看吧:
当然还可以通过Items属性 “ListViewItem 集合编辑器” 中设置。
2、修改某行某列的颜色
通过Items属性 “ListViewItem 集合编辑器”进入每一成员的“SubItems”属性进入“ListViewSubItem 集合编辑器”,即可编辑每个SubItem的背景色、前景色、字体等:
当然运行时,只能是在代码中编辑:
Private Sub Button7_Click(sender As Object,e As EventArgs) Handles Button7.Click Dim selectItem As ListViewItem For Each selectItem In ListView1.Items If selectItem.Selected = True Then '这里只设置了第一项的颜色 selectItem.SubItems(1).BackColor = Color.FromArgb(255,0) selectItem.SubItems(1).ForeColor = Color.FromArgb(255,255) Else '还原颜色 selectItem.SubItems(1).BackColor = Color.FromArgb(255,255) selectItem.SubItems(1).ForeColor = Color.FromArgb(0,0) End If Next End Sub
运行看一下:
什么!按下居然没有变色。
Private Sub Button7_Click(sender As Object,e As EventArgs) Handles Button7.Click Dim selectItem As ListViewItem For Each selectItem In ListView1.Items selectItem.UseItemStyleForSubItems = False If selectItem.Selected = True Then '这里只设置了第一项的颜色 selectItem.SubItems(1).BackColor = Color.FromArgb(255,255) selectItem.SubItems(1).Font = New Font("楷体",12) Else '还原颜色 selectItem.SubItems(1).BackColor = Color.FromArgb(255,0) selectItem.SubItems(1).Font = New Font("宋体",9) End If Next End Sub
看看运行效果:
在listview.ColumnWidthChanging事件中加入代码:
Private Sub ListView1_ColumnWidthChanging(sender As Object,e As ColumnWidthChangingEventArgs) Handles ListView1.ColumnWidthChanging e.Cancel = True '取消 e.NewWidth = ListView1.Columns(e.ColumnIndex).Width '设置现在新的宽度与调整前宽度一样 End Sub
学习更多vb.net知识,请参看 vb.net 教程 目录
原文链接:https://www.f2er.com/vb/256623.html