显示多列内容
为了便于学习本节的内容,可以先打开CaseStudy目录下的文件“ListView.Table”,并参考该文件名为“示例三”的窗口。
前面的例子中,ListView都只显示了图标和标题,实际上ListView可以像表一样,同时显示多列内容。
本节的任务是用ListView设计一个下图所示的窗口,显示各国更详细的信息:
设计步骤:
1、在窗口中分别插入一个ListView和Button控件,以及三个RadionButton控件:
2、将按钮的Click事件代码设置为:
Dim
lvw
As
WinForm.ListView
=
e.Form.Controls("ListView1")
lvw.Columns.Clear()
'清除原来的列
lvw.Rows.Clear()
'清除原来的行
lvw.Images.Clear()
'清除原来的图片
lvw.View
=
ViewMode.Details
'显示模式为详细信息
Dim
cls()
As
String
=
{"国家","人口","面积","语言","宗教","洲"}
'定义列名
Dim
wds()
As
Integer
=
{100,100,150,80,80,80}
'定义列宽
For
i
As
Integer
=
0
To
cls.Length
-
1
Dim
c
As
WinForm.ListViewColumn
=
lvw.Columns.Add()
c.Name
=
cls(i)
'指定列名
c.Text
=
cls(i)
'指定标题,这里标题和列名相同
c.Width
=
wds(i)
'指定列宽
Next
Dim
nms()
As
String
=
{"中国","美国","英国","俄罗斯","法国"}
'定义国名
Dim
rks()
As
String
=
{"125,382","31,525","6,240",
"14,410","6,580"}
'各国人口数量
Dim
mji()
As
String
=
{"96,000,000","9,629,091","244,820",
"17,075,400","551,602"}
'各国国土面积
Dim
lan()
As
String
=
{"中文","英语","英语","俄语","法语"}
'各国语言
Dim
zhj()
As
String
=
{"佛教","基督教","基督教",
"东正教","天主教"}
'各国主要宗教
Dim
szh()
As
String
=
{"亚洲","美洲","欧洲","欧洲","欧洲"}
'各国所在洲
Dim
ics()
As
String
=
{"China","USA","UK","Russia","France"}
'各国对应的图标键值
For i As Integer = 0 To nms.Length - 1
lvw.Images.AddImage(ics(i),ics(i) & ".ico",
ics(i)
&
"48.ico")
'添加代表这个国家的一对图标
Dim
r
As
WinForm.ListViewRow
=
lvw.Rows.Add()
'增加一行
r.ImageKey
=
ics(i)
'指定图标键值
r("国家")
=
nms(i)
'设置各列的值
r("人口")
=
rks(i)
&
"
万"
r("语言")
= lan(i)
r("面积")
= mji(i)
&
" 平方公里"
r("宗教")
= zhj(i)
r("洲")
= szh(i)
Next
3、分别设置三个RadionButton控件的代码:
详细信息:
If
e.Sender.Checked
= True Then
Dim lvw
As WinForm.ListView
= e.Form.Controls("ListView1")
lvw.View
= ViewMode.Details
'显示模式切换为详细内容
End
If
大图标:
If
e.Sender.Checked
= True Then
Dim lvw
As WinForm.ListView
= e.Form.Controls("ListView1")
lvw.View
= ViewMode.LargeIcon
'显示模式切换为大图标
End
If
小图标:
If
e.Sender.Checked
= True Then
Dim lvw
As WinForm.ListView
= e.Form.Controls("ListView1")
lvw.View
= ViewMode.SmallIcon
'显示模式切换为小图标
End
If
一些提示:
1、只有显示模式为详细信息的时候,ListView才会显示多列数据。
2、当显示模式为大图标或者小图标的时候,第一列的内容作为标题使用: