VB.NET 中 使用 ListView 控件的简单例子
2024-07-10 13:07:08
供稿:网友
listview 控件 在 程序开发过程中的使用是非常广泛的。因为其不支持数据库的绑定,所以在数据库程序开发领域无法与datagridview抗衡 ,但是listview的确是一个非常好用的控件。下面就把 一个简单的 listview的例子发出来。
public class form6class form6
<summary>
英雄类
</summary>
<remarks></remarks>
public class heroclass hero
private s_name as string
private s_age as long
private s_like as string
public property myname()property myname() as string
get
return s_name
end get
set(byval value as string)
s_name = value
end set
end property
public property myage()property myage() as long
get
return s_age
end get
set(byval value as long)
s_age = value
end set
end property
public property mylike()property mylike() as string
get
return s_like
end get
set(byval value as string)
s_like = value
end set
end property
end class
<summary>
公共属性
</summary>
<remarks></remarks>
dim arrlike() as string = {"杀人", "放火", "吸毒", "抢劫", "盗窃", "上吊", "自虐", "赌博", "偷窥", "飙车"}
dim herolist as new sortedlist
dim maxnumber as long
const firstname as string = "塞北的雪"
dim s_flag as boolean
<summary>
生成随即爱好
</summary>
<param name="i"></param>
<returns></returns>
<remarks></remarks>
private function createlike()function createlike(byval i as long) as string
dim j as long
dim k as long
dim str as new system.text.stringbuilder
for j = arrlike.getlowerbound(0) to arrlike.getupperbound(0)
dim rd as new system.random(environment.tickcount * (i + 1) * j / 100)
k = system.math.round(rd.nextdouble * 100, 0)
if k mod 2 = 0 then
str.append(" ")
str.append(arrlike(j))
end if
next
return str.tostring
end function
<summary>
装载数据
</summary>
<param name="sender"></param>
<param name="e"></param>
<remarks></remarks>
private sub btnload_click()sub btnload_click(byval sender as system.object, byval e as system.eventargs) handles btnload.click
s_flag = true
me.lv1.checkboxes = true
dim i as integer
const maxcount as long = 10
dim iitem as listviewitem
dim ahero as hero
for i = maxnumber to maxnumber + maxcount - 1
dim rd as new system.random(environment.tickcount * i)
ahero = new hero
iitem = new listviewitem
ahero.myname = firstname & i.tostring
ahero.myage = system.math.round(rd.nextdouble * 100, 0)
ahero.mylike = createlike(i)
iitem.text = ahero.myname
iitem.tag = ahero
iitem.subitems.add(ahero.myage)
iitem.subitems.add(ahero.mylike)
me.lv1.items.add(iitem)
herolist.add(ahero.myname, ahero)
next
maxnumber = i
if me.lv1.items.count > 0 then
me.lv1.items.item(0).checked = true
end if
s_flag = false
end sub
<summary>
选定所有项
</summary>
<param name="sender"></param>
<param name="e"></param>
<remarks></remarks>
private sub btncheckall_click()sub btncheckall_click(byval sender as system.object, byval e as system.eventargs) handles btncheckall.click
s_flag = true
if me.lv1.items.count > 0 then
dim i as integer
for i = 0 to me.lv1.items.count - 1
me.lv1.items.item(i).checked = true
next
end if
s_flag = false
end sub
<summary>
查找所有选定项
</summary>
<param name="sender"></param>
<param name="e"></param>
<remarks></remarks>
private sub btnfindcheckted_click()sub btnfindcheckted_click(byval sender as system.object, byval e as system.eventargs) handles btnfindcheckted.click
dim sb as new system.text.stringbuilder
if me.lv1.items.count > 0 then
dim i as integer
for i = 0 to me.lv1.checkeditems.count - 1
sb.appendline()
sb.append(me.lv1.checkeditems.item(i).text)
next
end if
msgbox(sb.tostring)
end sub
<summary>
删除所有
</summary>
<param name="sender"></param>
<param name="e"></param>
<remarks></remarks>
private sub btndeleteall_click()sub btndeleteall_click(byval sender as system.object, byval e as system.eventargs) handles btndeleteall.click
me.lv1.items.clear()
me.herolist.clear()
maxnumber = 0
end sub
<summary>
添加一项
</summary>
<param name="sender"></param>
<param name="e"></param>
<remarks></remarks>
private sub btnaddone_click()sub btnaddone_click(byval sender as system.object, byval e as system.eventargs) handles btnaddone.click
s_flag = true
dim iitem as new listviewitem
dim ahero as new hero
dim rd as new system.random(environment.tickcount * maxnumber)
ahero.myname = firstname & maxnumber.tostring
ahero.myage = system.math.round(rd.nextdouble * 100, 0)
ahero.mylike = createlike(maxnumber)
iitem.text = ahero.myname
iitem.tag = ahero
iitem.subitems.add(ahero.myage)
iitem.subitems.add(ahero.mylike)
me.lv1.items.add(iitem)
herolist.add(ahero.myname, ahero)
s_flag = false
maxnumber += 1
end sub
private sub form6_load()sub form6_load(byval sender as object, byval e as system.eventargs) handles me.load
me.lv1.view = view.details
me.lv1.headerstyle = columnheaderstyle.clickable
me.lv1.multiselect = true
me.lv1.fullrowselect = true
me.lv1.gridlines = true
me.lv1.columns.add("姓名", 120, horizontalalignment.center)
me.lv1.columns.add("年龄", 50, horizontalalignment.right)
me.lv1.columns.add("爱好", 300, horizontalalignment.left)
dim iitem as new listviewitem
dim ahero as new hero
dim rd as new system.random(environment.tickcount)
ahero.myname = "塞北的雪"
ahero.myage = system.math.round(rd.nextdouble * 100, 0)
ahero.mylike = createlike(1)
iitem.text = ahero.myname
iitem.tag = ahero
iitem.subitems.add(ahero.myage)
iitem.subitems.add(ahero.mylike)
me.lv1.items.add(iitem)
herolist.add(ahero.myname, ahero)
me.lv1.items.item(0).selected = true
maxnumber = 0
s_flag = false
end sub
<summary>
删除所有选定项
</summary>
<param name="sender"></param>
<param name="e"></param>
<remarks></remarks>
private sub btndeleteselected_click()sub btndeleteselected_click(byval sender as system.object, byval e as system.eventargs) handles btndeleteselected.click
if me.lv1.items.count > 0 then
if me.lv1.selecteditems.count > 0 then
dim i as integer
dim dellist as new sortedlist
for i = 0 to me.lv1.selecteditems.count - 1
herolist.remove(me.lv1.selecteditems.item(i).text)
me.lv1.items.remove(me.lv1.selecteditems.item(i))
dellist.add(me.lv1.selecteditems.item(i).text, me.lv1.selecteditems.item(i))
next
for i = 0 to dellist.count - 1
me.lv1.items.remove(dellist.getbyindex(i))
next
end if
end if
end sub
private sub lv1_itemcheck()sub lv1_itemcheck(byval sender as object, byval e as system.windows.forms.itemcheckeventargs) handles lv1.itemcheck
if me.lv1.items.count > 0 then
if s_flag = false then
if e.newvalue = checkstate.checked and e.currentvalue <> checkstate.checked then
msgbox("你选择了:" & me.lv1.items.item(e.index).text)
elseif e.newvalue = checkstate.unchecked and e.currentvalue <> checkstate.unchecked then
msgbox("你取消了:" & me.lv1.items.item(e.index).text)
end if
end if
end if
end sub
private sub lv1_itemchecked1()sub lv1_itemchecked1(byval sender as object, byval e as system.windows.forms.itemcheckedeventargs) handles lv1.itemchecked
if me.lv1.items.count > 0 then
if s_flag = false then
if e.item.checked then
msgbox("你选择了:" & e.item.text)
else
msgbox("你取消了:" & e.item.text)
end if
end if
end if
end sub
private sub btnuncheckall_click()sub btnuncheckall_click(byval sender as system.object, byval e as system.eventargs) handles btnuncheckall.click
s_flag = true
if me.lv1.items.count > 0 then
dim i as integer
for i = 0 to me.lv1.items.count - 1
me.lv1.items.item(i).checked = false
next
end if
s_flag = false
end sub
end class
程序运行的截图如下: