Private Sub Form_Load() ListView1.ListItems.Clear '清空列表 ListView1.ColumnHeaders.Clear '清空列表头 ListView1.View = lvwReport '设置列表显示方式 ListView1.GridLines = True '显示网络线 ListView1.LabelEdit = lvwManual '禁止标签编辑 ListView1.FullRowSelect = True '选择整行 ListView1.ColumnHeaders.Add , , "ID", 500 '给列表中添加列名 ListView1.ColumnHeaders.Add , , "本地 IP", 1500 ListView1.ColumnHeaders.Add , , "本地端口", 1200 ListView1.ColumnHeaders.Add , , "协议", 550 ListView1.ColumnHeaders.Add , , "远程 IP", 1500 ListView1.ColumnHeaders.Add , , "远程端口", 900 ListView1.ColumnHeaders.Add , , "当前状态", 900 ListView1.ColumnHeaders.Add , , "连接时间", 900 '------------------------------------------------------- Dim x x = ListView1.ListItems.Count + 1 ListView1.ListItems.Add , , x ListView1.ListItems(x).SubItems(1) = "00:00:00" ListView1.ListItems(x).SubItems(2) = "2008-01-01" ListView1.ListItems(x).SubItems(3) = "(无)" '------------------------------------------------------- ListView1.ListItems.Clear '清空列表 ListView1.ListItems.Add , , "1" 'ListView1.ListItems.Add , , "1", , 1 '添加图标 后面那个1是ImageList1控件中的图标索引号 ListView1.ListItems(1).SubItems(1) = "00:00:00" ListView1.ListItems(1).SubItems(2) = "2008-01-01" ListView1.ListItems(1).SubItems(3) = "(无)" ListView1.ListItems.Add , , "2" ListView1.ListItems(2).SubItems(1) = "00:00:01" ListView1.ListItems(2).SubItems(2) = "2008-01-01" ListView1.ListItems(2).SubItems(3) = "(无)" '------------------------------------------------------- '下列的属性也可以 单击控件右键->属性 进行设置。 ListView1.View = lvwReport '设置显示方式为列表 ListView1.AllowColumnReorder = True '对行进行程序排列,用鼠标进行排列 ListView1.Arrange = lvwAutoLeft '图标横排列 ListView1.Arrange = lvwAutoTop '图标竖排列 ListView1.FlatScrollBar = False '显示滚动条 ListView1.FlatScrollBar = True '隐藏滚动条 ListView1.FullRowSelect = True '选择整行 ListView1.LabelEdit = lvwManual '禁止标签编辑 ListView1.GridLines = True '显示网络线 ListView1.LabelWrap = True '图标可以换行 ListView1.MultiSelect = True '可以选择多个项目 ListView1.PictureAlignment = lvwTopLeft '图片对齐方式是左顶部,其他有右顶部(1)、左底部(2)、右底部(3)、居中(4)、平铺(5) ListView1.Checkboxes = True '显示复选框 'ListView1.DropHighlight = ListView1.ListItems.Item(2) '显示系统颜色 End Sub
常用操作:
获取当前行数和列数:
-
MsgBox "行数:" & ListView1.ListItems.Count & "列数:" & ListView1.ColumnHeaders.Count
-
Private Sub ListView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) ListView1.ToolTipText = "行数:" & ListView1.ListItems.Count & "列数:" & ListView1.ColumnHeaders.Count End Sub
设置宽度:
-
ListView1.ColumnHeaders.Add , , "备注", 1500
当前选中行:
-
MsgBox ListView1.SelectedItem.Index
获取复选框的值:
-
MsgBox ListView1.ListItems(1).Checked
删除指定行:
-
ListView1.ListItems.Remove 1
去掉鼠标左键点击标签编辑:
labeledit属性改为1
读取某行第一列内容:
-
ListView1.ListItems(i).Text
读取某行第几列内容 (不包括第一列):
-
ListView1.ListItems(Num).SubItems (4)
读取当前选中行第一列内容:
-
ListView1.ListItems(ListView1.SelectedItem.Index).Text
循环查找读取项目:
-
Dim i As Integer For i = 1 To ListView1.ListItems.Count If ListView1.ListItems(i).Text = 4 Then MsgBox ListView1.ListItems(i).Text '第一列 If ListView1.ListItems(i).SubItems(1) = 4 Then MsgBox ListView1.ListItems(i).SubItems(1) '第二列 Next i
清空列表头:
-
ListView1.ColumnHeaders.Clear
清空列表:
-
ListView1.ListItems.Clear
右键菜单:
-
Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbRightButton Then PopupMenu CommandLst '按下鼠标右键 显示菜单 End Sub
当前选中判断:
-
Dim N If ListView1.ListItems.Count <> 0 Then N = ListView1.SelectedItem.Index Else MsgBox "当前没有任何主机在线!", vbInformation, "警告:": Exit Sub If N < 1 Then MsgBox "你没有选中任何主机!", vbInformation, "警告:": Exit Sub
VB设置某行为选中/非选中状态:
-
ListView.ListItems(i).Selected = True '选中第i行 ListView.ListItems(i).Selected = False '选中第i行
设置ListView item项颜色
-
ListView1.ListItems(i).ForeColor = vbRed ListView1.ListItems(i).ListSubItems.Item(1).ForeColor = vbRed ListView1.ListItems(i).ListSubItems.Item(2).ForeColor = vbRed