VB ListView控件各种操作详解

常用操作:
获取当前行数和列数:

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
赞(0) 打赏
未经允许不得转载:TaKaSa » VB ListView控件各种操作详解

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  • Q Q(选填)

赞助下

支付宝扫一扫打赏

微信扫一扫打赏