2024年4月12日发(作者:)

Excel中VBA listview、imagelist控件

1、在Listview控件中,用ColumnHeaders对象来操作列,而添加新的列可以用

ColumnHeaders对象的ADD方法。具体如下:

序号,唯一的字符串标识,列标显示文字,列宽,

列的内容对齐方式,所使用的图标序号。

对齐方式有:lvwColumnLeft 、 lvwColumnCenter、lvwColumnRight

其中第1列只能左对齐,而不能采取其他对齐方式

2、Listview控件的View属性决定着它的显示的方式,具体如下:

= lvwIcon '图标显示

= lvwSmallIcon '小图标显示

= lvwList '列表显示

= lvwReport '报表显示

3、SelectedItem属性即是指当前选取的记录

4、清空listview控件中的内容:

显示数据表内容:

Private Sub UserForm_Initialize()

With ListView1 '初始化listview

. , , "学号", 60, lvwColumnLeft

. , , "姓名", 60, lvwColumnCenter

. , , "班级", 70, lvwColumnCenter

.View = lvwReport '以报表的格式显示

.LabelEdit = lvwManual '使内容不可编辑

End With

Dim cn As tion

Dim rs As set

Set cn = New tion '连接数据库

Set rs = New set

"provider=.4.0;data source=D:/"

"student", cn, adOpenKeyset, adLockBatchOptimistic '数据表student

Do While Not

With () '添加记录

.Text = ("stu_num") '添加第一列内容

.SubItems(1) = ("stu_name") '添加第二列内容

.SubItems(2) = ("stu_class") '添加第三列内容

End With

xt

Loop

Set rs = Nothing

End Sub

读取listview选择的内容:

将选中的内容分别显示在UserForm1的文本框中

With UserForm1

.stu_ = '学号

.stu_ = ms(1) '姓名

.stu_ = ms(2) '班级

End With

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

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

Private Sub UserForm_Initialize()

'定义变量数据类型

Dim ITM As ListItem

1, , "QQ号", / 3 '添加QQ号列

2, , "呢称", / 3,

lvwColumnCenter '添加呢称列

3, , "来自何处", / 3 '添加来自何

处列

= lvwReport '设置显示格式为报表格式

nes = True '显示网格线

cons = ImageList1 '建立控件和ImageList1关联

For i = 1 To 5

'添加记录

Set ITM = ()

= Cells(i, 1) '添加第1列内容

ms(1) = Cells(i, 2)

ms(2) = Cells(i, 3)

con = i '设置添加的记录图标为ImageList1中序号为i的图标.

Next i

Set ITM = Nothing

End Sub

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

Private Sub UserForm_Initialize()

= lvwIcon '设置控件显示方式为图标显示ImageList1

= ImageList1 '建立和 ImageList1控件的关联

, , "操作员001", 2

, , "操作员002", 2

, , "操作员003", 2

, , "主管", 1

, , "开发", 1

End Sub

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

listview的应用:

wSelect = True'获得选中全行

= edItems(i).SubItems(1).Text

总行数

总列数 - 1

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

Private Sub Command1_Click()

当前选择行插入行赋值

Dim lListItemCount As Long

With ew1

' lListItemCount = .

With .(. + 1, , "11")

. , , "22"

End With

End With

End Sub

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

--

Private Sub Command2_Click()

当前选择行删除行

End Sub

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

--

Private Sub Form_Load()

Dim lLoop As Long

With ew1

.View = lvwReport

With .(, , "列1")

.Width = 1260

End With

With .(, , "列2")

.Width = 2500

End With

.FullRowSelect = True

For lLoop = 1 To 20

With .(, , lLoop)

. , , lLoop * 10

End With

Next lLoop

End With

End Sub

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

清空数据

删除数据

(i)

当前是否选择

if ems(i).Selected then

添加数据在选中行上

()

listview1中导出数据

For i = 1 To

.Cells(i + 1, 1) = ems(i)

For j = 1 To 6

.Cells(i + 1, j + 1) = ems(i).SubItems(j)

Next j

Next i

表标题输入

For i = 2 To .

(1, i - 1) = .ColumnHeaders(i).Text

Next i

'添加列,列宽,

左对齐lvwColumnLeft

居中对齐lvwColumnCenter

, , "序号", / 10, lvwColumnLeft

'设置显示方式为报表格式

= lvwReport

'显示表格线

nes = True

添加记录

For i = 1 To Sheets(1).[A65536].End(xlUp).Row

Dim ITM As ListItem

Set ITM = ()

= Sheets(1).Cells(i + 1, 1)

ms(1) = Sheets(1).Cells(i + 1, 2)

。。。。。。。。

取某一行的值

.Cells(2, nn) = ems(CurrIndex).Text

.Cells(3, nn) = ems(CurrIndex).SubItems(1)

取选中行的值

. = edItem

. = ms(1)