VB ListView 控件
View Code
Private
Sub
Command1_Click()
'
命令按钮
ListView1.View = lvwReport ' 设置ListView控件中ListItem对象的外观形式为报表,其值为3
ListView1.ColumnHeaders.Clear ' 清除原有的列标题
With ListView1.ColumnHeaders ' 添加新的列标题
.Add, "" , 10
.Add, " 第一列 " , 1000 ' 第一列标题,标题内容为“第一列”,列宽为1000
.Add, " 第二列 " , 1000 ' 第二列标题,标题内容为“第二列”,列宽为1000
End With
ListView1.ListItems.Clear ' 清除原有的记录内容
With ListView1.ListItems.Add ' 添加新记录
.SubItems( 1 ) = 1 ' 第一列内容
.SubItems( 2 ) = 2 ' 第二列内容
End With
EndSub
ListView1.View = lvwReport ' 设置ListView控件中ListItem对象的外观形式为报表,其值为3
ListView1.ColumnHeaders.Clear ' 清除原有的列标题
With ListView1.ColumnHeaders ' 添加新的列标题
.Add, "" , 10
.Add, " 第一列 " , 1000 ' 第一列标题,标题内容为“第一列”,列宽为1000
.Add, " 第二列 " , 1000 ' 第二列标题,标题内容为“第二列”,列宽为1000
End With
ListView1.ListItems.Clear ' 清除原有的记录内容
With ListView1.ListItems.Add ' 添加新记录
.SubItems( 1 ) = 1 ' 第一列内容
.SubItems( 2 ) = 2 ' 第二列内容
End With
EndSub
-
@H_502_211@Private @H_502_211@Sub Form_Load() ListView1.ListItems.Clear '清空列表 ListView1.ColumnHeaders.Clear '清空列表头 ListView1.View = lvwReport '设置列表显示方式 ListView1.GridLines = @H_502_211@True '显示网络线 ListView1.LabelEdit = lvwManual '禁止标签编辑 ListView1.FullRowSelect = @H_502_211@True '选择整行 ListView1.ColumnHeaders.Add,"ID",500 '给列表中添加列名 ListView1.ColumnHeaders.Add,"本地 IP",1500 ListView1.ColumnHeaders.Add,"本地端口",1200 ListView1.ColumnHeaders.Add,"协议",550 ListView1.ColumnHeaders.Add,"远程 IP","远程端口",900 ListView1.ColumnHeaders.Add,"当前状态","连接时间",900 '------------------------------------------------------- @H_502_211@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(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 = @H_502_211@True '对行进行程序排列,用鼠标进行排列 ListView1.Arrange = lvwAutoLeft '图标横排列 ListView1.Arrange = lvwAutoTop '图标竖排列 ListView1.FlatScrollBar = @H_502_211@False '显示滚动条 ListView1.FlatScrollBar = @H_502_211@True '隐藏滚动条 ListView1.FullRowSelect = @H_502_211@True '选择整行 ListView1.LabelEdit = lvwManual '禁止标签编辑 ListView1.GridLines = @H_502_211@True '显示网络线 ListView1.LabelWrap = @H_502_211@True '图标可以换行 ListView1.MultiSelect = @H_502_211@True '可以选择多个项目 ListView1.PictureAlignment = lvwTopLeft '图片对齐方式是左顶部,其他有右顶部(1)、左底部(2)、右底部(3)、居中(4)、平铺(5) ListView1.CheckBoxes = @H_502_211@True '显示复选框 'ListView1.DropHighlight = ListView1.ListItems.Item(2) '显示系统颜色 @H_502_211@End @H_502_211@Sub
常用操作:
获取当前行数和列数:
-
MsgBox "行数:" & ListView1.ListItems.Count & "列数:" & ListView1.ColumnHeaders.Count
-
@H_502_211@Private @H_502_211@Sub ListView1_MouseMove(Button @H_502_211@As @H_502_211@Integer,Shift @H_502_211@As @H_502_211@Integer,x @H_502_211@As @H_502_211@Single,y @H_502_211@As @H_502_211@Single) ListView1.ToolTipText = "行数:" & ListView1.ListItems.Count & "列数:" & ListView1.ColumnHeaders.Count @H_502_211@End @H_502_211@Sub
设置宽度:
-
ListView1.ColumnHeaders.Add,"备注",1500
当前选中行:
-
MsgBox ListView1.SelectedItem.Index
获取复选框的值:
-
MsgBox ListView1.ListItems(1).Checked
删除指定行:
-
ListView1.ListItems.Remove 1
读取某行第一列内容:
-
ListView1.ListItems(i).Text
-
ListView1.ListItems(Num).SubItems (4)
读取当前选中行第一列内容:
-
ListView1.ListItems(ListView1.SelectedItem.Index).Text
循环查找读取项目:
-
@H_502_211@Dim i @H_502_211@As @H_502_211@Integer @H_502_211@For i = 1 @H_502_211@To ListView1.ListItems.Count @H_502_211@If ListView1.ListItems(i).Text = 4 @H_502_211@Then MsgBox ListView1.ListItems(i).Text '第一列 @H_502_211@If ListView1.ListItems(i).SubItems(1) = 4 @H_502_211@Then MsgBox ListView1.ListItems(i).SubItems(1) '第二列 @H_502_211@Next i
清空列表头:
-
ListView1.ColumnHeaders.Clear
清空列表:
-
ListView1.ListItems.Clear
右键菜单:
-
@H_502_211@Private @H_502_211@Sub ListView1_MouseDown(Button @H_502_211@As @H_502_211@Integer,y @H_502_211@As @H_502_211@Single) @H_502_211@If Button = vbRightButton @H_502_211@Then PopupMenu CommandLst '按下鼠标右键 显示菜单 @H_502_211@End @H_502_211@Sub
当前选中判断:
-
@H_502_211@Dim N @H_502_211@If ListView1.ListItems.Count <> 0 @H_502_211@Then N = ListView1.SelectedItem.Index @H_502_211@Else MsgBox "当前没有任何主机在线!",vbInformation,"警告:": @H_502_211@Exit @H_502_211@Sub @H_502_211@If N < 1 @H_502_211@Then MsgBox "你没有选中任何主机!","警告:": @H_502_211@Exit @H_502_211@Sub
VB设置某行为选中/非选中状态: