VB的listview使用问题

请详细说明listview添加、修改、删除一行的语法及其相关属性,最好举例说明。

我要实现的,是用listview在左边显示一个图标(像QQ的好友列表那样有不同的多种图标,依据的是数据库记录的文件名),右边显示数据库列表记录(像QQ的好友呢称列表),然后点击图标或名称会打开新窗口。所以最好有这些的例子和说明,非常感谢。

从工程菜单选择“部件”,然后钩选Microsoft Windows Common Controls 6.0,确定
双击工具箱的ImageList控件,在窗体上出现的控件图标上点右键选属性。转到图像选项卡,为控件添加几个图片
。控件名称为ImageList1.
可以分别为每一种视图设置一个ImageList

用ListView控件在窗体上创建一个ListView1,点右键选属性,转到图像列表,然后全部指定ImageList1,转到列首选项卡,至少添加一列,为每列指定一个图标索引,其他设置自行设定,最后确定

添加表项的代码格式,以ListView1为例

ListView1.ListItems.Add [索引],[键] ,[标签文字], [大图标索引号],[小图标索引号]

添加子项的格式
ListView1.ListItems(表项索引号).ListSubItems.Add [索引],[键] ,文字标签, 详细资料图标索引号,工具提示文字

删除项
ListView1.ListItems.remove 项索引号
全部清除
ListView1.ListItems.clear

修改第一项的标签
ListView1.ListItems.Item(1).Text="新标签"

修改第一项的子项
ListView1.ListItems.Item(1).ListSubItems(1).Text ="新标签"

点右键的操作
Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim thisitem As ListItem
If Button = MouseButtonConstants.vbRightButton Then
If ListView1.HitTest(x, y) Is Nothing Then

Else
Set thisitem = ListView1.HitTest(x, y)
'比如根据所点项的索引做相应动作
Call dosomething(thisitem.Index)

End If
End If

End Sub

上面的带中括号的为可选

例如
ListView1.ListItems.Add , , "hello", 1, 1
ListView1.ListItems(1).ListSubItems.Add , , "second", 2, "hello"
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-12-24
Sub loadlove()
Dim str As String
Dim ii As Integer

Dim imgX As ListImage

ListView1.ListItems.Clear

Open "F:\软件\vb\2007\love.txt" For Input As #1
'Open App.path & "\love.txt" For Output As #1

If LOF(1) > 0 Then

While Not EOF(1)
X = DoEvents
Line Input #1, str

Dim youxilujing As String
youxilujing = cutstr(str, "[游戏路径]", ";")
youxilujing = Mid(youxilujing, InStrRev(youxilujing, "\") + 1, -InStrRev(youxilujing, "\") + InStrRev(youxilujing, ".") - 1)

If Dir("f:\模拟游戏\mame\icons\" & youxilujing & ".ico") = "" Then

youxilujing = "005"
End If
Set imgX = ImageList1.ListImages. _
Add(, , LoadPicture("f:\模拟游戏\mame\icons\" & youxilujing & ".ico"))
'添加一个图象到 ImageList2--小图标 ImageList。
Set imgX = ImageList2.ListImages. _
Add(, , LoadPicture("f:\模拟游戏\mame\icons\" & youxilujing & ".ico"))

ListView1.Icons = ImageList1
ListView1.SmallIcons = ImageList2

Set li = ListView1.ListItems.Add(Text:=cutstr(str, "[游戏名称]", ";"))

li.Icon = ImageList1.ListImages.Count '设置 ImageList1 中的一个图标。
li.SmallIcon = ImageList2.ListImages.Count '设置 ImageList2 中的一个图标。

Set si = li.ListSubItems.Add(Text:=Mid(cutstr(str, "[游戏路径]", ";"), InStrRev(cutstr(str, "[游戏路径]", ";"), "\") + 1, -InStrRev(cutstr(str, "[游戏路径]", ";"), "\") + InStrRev(cutstr(str, "[游戏路径]", ";"), ".") - 1) & ".zip")

Set si = li.ListSubItems.Add(Text:=cutstr(str, "[游戏路径]", ";"))

Set si = li.ListSubItems.Add(Text:=cutstr(str, "[模拟器]", ";"))

lindex = lindex + 1

Wend
End If

Close #1

End Sub

'''''''''''写listview的内容到指定文件
Sub writelove()
Open "F:\软件\vb\2007\love.txt" For Output As #1 'Open App.path & "\love.txt" For Output As #1
For i = 1 To ListView1.ListItems.Count
Print #1, "[游戏名称]" & ListView1.ListItems(i) & ";" & "[游戏路径]" & ListView1.ListItems(i).SubItems(2) & ";" & "[模拟器]" & ListView1.ListItems(i).SubItems(3) & ";" & Chr(10)
Next
Close #1
End Sub

我自己写的小程序的一段,自己看看吧,我是业余的咬
第2个回答  2007-12-20
这里有一个典型的例子,你看看:
http://www.ruiyuan-power.com/sfw/LC_ListView.rar
第3个回答  2007-12-20
http://www.bibidu.com/fileview-211.html
msdn for vb6.0简体中文版

安装后在VB中按F1调出MSDN
在索引标签页中 输入 ListView
然后查看对应的各项属性方法事件 中文解释说明本回答被提问者采纳
相似回答