1、在Listview控件中,用ColumnHeaders对象来操作列,而添加新的列可以用ColumnHeaders对象的ADD方法。
具体如下:
ListView1.ColumnHeaders.Add 序号,唯一的字符串标识,列标显示文字,列宽,列的内容对齐方式,所使用的图标序号。
对齐方式有:lvwColumnLeft 、 lvwColumnCenter 、lvwColumnRight
其中第1列只能左对齐,而不能采取其他对齐方式
2、Listview控件的View 属性决定着它的显示的方式,具体如下:
1 2 3 4
| ListView1.View = lvwIcon ListView1.View = lvwSmallIcon ListView1.View = lvwList ListView1.View = lvwReport
|
3、SelectedItem属性即是指当前选取的记录
4、清空listview控件中的内容:
1
| ListView1.ListItems.Clear
|
显示数据表内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| Private Sub UserForm_Initialize() With ListView1 .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 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的文本框中
1 2 3 4 5
| With UserForm1 .stu_num.Value = ListView1.SelectedItem.Text .stu_name.Text = ListView1.SelectedItem.SubItems(1) .stu_class.Text = ListView1.SelectedItem.SubItems(2) End With
|