Private Sub Command1_Click() '添加数据 Set itmx = ListView1.ListItems.Add(,"hello") '文件名称栏目 itmx.SubItems(1) = "123" '文件在列表框位置 itmx.SubItems(2) = "234" ListView1.View = lvwReport '报表类型 End Sub Private Sub Command2_Click() '修改数据 Set itmx = ListView1.ListItems(ListView1.SelectedItem.Index) itmx.SubItems(1) = 1234 End Sub Private Sub Command4_Click() '移除数据 ListView1.ListItems.Remove ListView1.SelectedItem.Index End Sub Private Sub Form_Resize() ListView1.Width = Form1.Width - 2300 ListView1.Height = Form1.Height - 1000 '窗体的标题为生成可执行文件的标题ie '窗体位置在屏幕中间 ListView1.ColumnHeaders.Clear ListView1.ColumnHeaders.Add,"偏移",ListView1.Width / 7,0 ListView1.ColumnHeaders.Add,"十进制",ListView1.Width / 5,0 '第一个标题栏是“文件名称“,长度为ListView1 '宽度的三分之一,文字左对齐 Set clmX = ListView1.ColumnHeaders.Add(,"十六进制",2) '第二个标题栏是“序号“ 依次往下 Set clmX = ListView1.ColumnHeaders.Add(,"字符 ",ListView1.Width / 4,1) Set clmX = ListView1.ColumnHeaders.Add(,"创建时间",0) ListView1.View = lvwReport '报表类型 End Sub
以上是对listview的一些简单的操作,下面更深入的了解下,Listview的颜色,字体等功能。
Public Sub SetListViewBGColor(ByRef ListView As ListView,_ ByRef Index As Long,_ Optional BGColor As Long = &HB6B4FF) 'author:www.newxing.com Dim lineTop As Long,lineHeight As Integer Static Pic As PictureBox If Pic Is Nothing Then Set Pic = Controls.Add("VB.PictureBox","picbg") lineHeight = ListView.ListItems(Index).Height lineTop = lineHeight * (Index - 1) With Pic .Cls .AutoRedraw = True .Visible = False .ScaleMode = vbTwips .Font = ListView.Font .BorderStyle = 1 .Appearance = 0 .Height = lineHeight * ListView.ListItems.count + lineHeight .Width = ListView.Width End With Pic.Line (0,lineTop)-(Pic.Width,lineTop + lineHeight),BGColor,BF ListView.Picture = Pic.Image End Sub
以上这个函数就可以给listview中指定的一行设定颜色,这个默认是粉红色,可以改的
如果希望修改成,网络上流行的背景 间隔的颜色,那么只要对上面的函数稍加修改就行了。
Public Sub SetListViewBGColor(ByRef ListView As ListView,"picbg") lineHeight = ListView.ListItems(Index).Height lineTop = lineHeight * (Index - 1) With Pic .Cls .AutoRedraw = True .Visible = False .ScaleMode = vbTwips .Font = ListView.Font .BorderStyle = 1 .Appearance = 0 .Height = lineHeight * ListView.ListItems.count + lineHeight .Width = ListView.Width End With Dim i As Integer For i = 0 To ListView.ListItems.count If i Mod 2 Then Pic.Line (0,BF End If lineTop = lineTop + lineHeight Next 'Pic.Line (0,BF ListView.Picture = Pic.Image End Sub
如何给字体加上颜色呢?
Set itmx = ListView1.ListItems(ListView1.SelectedItem.Index) ListView1.ListItems(1).ForeColor = vbRed '红色 ListView1.ListItems(1).Bold = False'加粗 ListView1.ListItems(1).ListSubItems(1).ForeColor = vbRed'给指定的一个网格内,设定字体为红色 ListView1.ListItems(1).ListSubItems(1).Bold = False'给指定的一个网格内,字体设置为加粗