Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

Excel VBA listview控件

时 间:2012-05-16 00:05:06
作 者:欢乐小爪   ID:20149  城市:杭州
摘 要:VBA listview控件
正 文:

1Listview控件中,用ColumnHeaders对象来操作列,而添加新的列可以用ColumnHeaders对象的ADD方法。具体如下:
ListView1.ColumnHeaders.Add序号,唯一的字符串标识,列标显示文字,列宽,列的内容对齐方式,所使用的图标序号。
对齐方式有:lvwColumnLeft lvwColumnCenterlvwColumnRight

其中第1列只能左对齐,而不能采取其他对齐方式
2
Listview控件的View属性决定着它的显示的方式,具体如下:
ListView1.View = lvwIcon '
图标显示
ListView1.View = lvwSmallIcon '
小图标显示
ListView1.View = lvwList '
列表显示
ListView1.View = lvwReport '
报表显示
3
SelectedItem属性即是指当前选取的记录
4
、清空listview控件中的内容:ListView1.ListItems.Clear

显示数据表内容:
Private Sub UserForm_Initialize()
With ListView1 '
初始化listview
.ColumnHeaders.Add , , "
学号", 60, lvwColumnLeft
.ColumnHeaders.Add , , "
姓名", 60, lvwColumnCenter
.ColumnHeaders.Add , , "
班级", 70, lvwColumnCenter
.View = lvwReport '
以报表的格式显示
.LabelEdit = lvwManual '
使内容不可编辑
End With
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection '
连接数据库
Set rs = New ADODB.Recordset
cn.Open "provider=microsoft.jet.oledb.4.0;data source=D:/db1.mdb"
rs.Open "student", cn, adOpenKeyset, adLockBatchOptimistic '
数据表student
Do While Not rs.EOF
With ListView1.ListItems.Add() '
添加记录
.Text = rs.Fields("stu_num") '
添加第一列内容
.SubItems(1) = rs.Fields("stu_name") '
添加第二列内容
.SubItems(2) = rs.Fields("stu_class") '
添加第三列内容
End With
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
cn.Close
End Sub

读取listview选择的内容:
将选中的内容分别显示在UserForm1的文本框中
With UserForm1
.stu_num.Value = ListView1.SelectedItem.Text '
学号
.stu_name.Text = ListView1.SelectedItem.SubItems(1) '
姓名
.stu_class.Text = ListView1.SelectedItem.SubItems(2) '
班级
End With
--------------------------------------------------------------------------------------------------------------------
Private Sub UserForm_Initialize()
'定义变量数据类型
Dim ITM As ListItem
ListView1.ColumnHeaders.Add 1, , "QQ号", ListView1.Width / 3 '添加QQ号列
ListView1.ColumnHeaders.Add 2, , "呢称", ListView1.Width / 3, lvwColumnCenter '添加呢称列
ListView1.ColumnHeaders.Add 3, , "来自何处", ListView1.Width / 3 '添加来自何处列
ListView1.View = lvwReport '设置显示格式为报表格式
ListView1.Gridlines = True '显示网格线
ListView1.SmallIcons = ImageList1 '建立控件和ImageList1关联
For i = 1 To 5
'添加记录
Set ITM = ListView1.ListItems.Add()
ITM.Text = Cells(i, 1) '添加第1列内容
ITM.SubItems(1) = Cells(i, 2)
ITM.SubItems(2) = Cells(i, 3)
ITM.SmallIcon = i '设置添加的记录图标为ImageList1中序号为i的图标.
Next i
Set ITM = Nothing
End Sub

--------------------------------------------------------------------------------
Private Sub UserForm_Initialize()
ListView1.View = lvwIcon '设置控件显示方式为图标显示ImageList1
ListView1.Icons = ImageList1 '建立和 ImageList1控件的关联
ListView1.ListItems.Add , , "操作员001", 2
ListView1.ListItems.Add , , "操作员002", 2
ListView1.ListItems.Add , , "操作员003", 2
ListView1.ListItems.Add , , "主管", 1
ListView1.ListItems.Add , , "开发", 1
End Sub

----------------------------------------------------------------------

listview的应用:

ListView1.FullRowSelect = True'获得选中全行

TextBox1.Text = ListView1.SelectedItems(i).SubItems(1).Text

总行数ListView1.ListItems.Count

总列数ListView1.SelectedItems.Count - 1

-------------------------------------------------------------------------

Private Sub Command1_Click()
当前选择行插入行赋值
Dim lListItemCount As Long
With Me.ListView1
' lListItemCount = .ListItems.Count
With .ListItems.Add(.SelectedItem.Index + 1, , "11")
.ListSubItems.Add , , "22"
End With
End With
End Sub

---------------------------------------------------------------------------------------

Private Sub Command2_Click()
当前选择行删除行
Me.ListView1.ListItems.Remove Me.ListView1.SelectedItem.Index
End Sub

---------------------------------------------------------------------------------------

Private Sub Form_Load()
Dim lLoop As Long
With Me.ListView1
.View = lvwReport
With .ColumnHeaders.Add(, , "列1")
.Width = 1260
End With
With .ColumnHeaders.Add(, , "列2")
.Width = 2500
End With
.FullRowSelect = True
For lLoop = 1 To 20
With .ListItems.Add(, , lLoop)
.ListSubItems.Add , , lLoop * 10
End With
Next lLoop
End With
End Sub

--------------------------------------------------------------------------

清空数据
Me.ListView1.ListItems.Clear

删除数据
Me.ListView1.ListItems.Remove (i)

当前是否选择
if Me.ListView1.ListItems(i).Selected then

添加数据在选中行上
ListView1.ListItems.Add(ListView1.SelectedItem.Index)

listview1中导出数据
For i = 1 To ListView1.ListItems.Count
.Cells(i + 1, 1) = ListView1.ListItems(i)
For j = 1 To 6
.Cells(i + 1, j + 1) = ListView1.ListItems(i).SubItems(j)
Next j
Next i

表标题输入
For i = 2 To .ColumnHeaders.Count
sh.Cells(1, i - 1) = .ColumnHeaders(i).Text
Next i

'添加列,列宽,
左对齐lvwColumnLeft
居中对齐lvwColumnCenter
ListView1.ColumnHeaders.Add , , "序号", ListView1.Width / 10, lvwColumnLeft


'设置显示方式为报表格式
ListView1.View = lvwReport
'显示表格线
ListView1.Gridlines = True


添加记录
For i = 1 To Sheets(1).[A65536].End(xlUp).Row
Dim ITM As ListItem
Set ITM = ListView1.ListItems.Add()
ITM.Text = Sheets(1).Cells(i + 1, 1)
ITM.SubItems(1) = Sheets(1).Cells(i + 1, 2)
。。。。。。。。

取某一行的值
.Cells(2, nn) = ListView1.ListItems(CurrIndex).Text
.Cells(3, nn) = ListView1.ListItems(CurrIndex).SubItems(1)

取选中行的值
.TextBox1.Value = ListView1.SelectedItem
.TextBox2.Value = ListView1.SelectedItem.SubItems(1)



Access软件网QQ交流群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助