VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • VB.NET中使用ListView控件的简单例子

ListView控件在程序开发过程中的使用是非常广泛的。因为其不支持数据库的绑定,所以在数据库程序开发领域无法与datagridview抗衡,但是ListView的确是一个非常好用的控件。下面就把一个简单的ListView的例子发出来。
 
 
 
Public Class Form6Class Form6
 
''' 
 
''' 英雄类
 
''' 
 
''' 
 
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
 
''' 
 
''' 公共属性
 
''' 
 
''' 
 
Dim arrLike() As String = {"杀人", "放火", "吸毒", "抢劫", "盗窃", "上吊",
 
"自虐", "赌博", "偷窥", "飙车"}
 
Dim heroList As New SortedList
 
Dim maxNumber As Long
 
Const firstName As String = "塞北的雪"
 
Dim s_flag As Boolean
 
''' 
 
''' 生成随即爱好
 
''' 
 
''' 
 
''' 
 
''' 
 
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
 
''' 
 
'''  装载数据
 
''' 
 
''' 
 
''' 
 
''' 
 
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
 
''' 
 
''' 选定所有项
 
''' 
 
''' 
 
''' 
 
''' 
 
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
 
''' 
 
''' 查找所有选定项
 
''' 
 
''' 
 
''' 
 
''' 
 
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
 
''' 
 
''' 删除所有
 
''' 
 
''' 
 
''' 
 
''' 
 
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
 
''' 
 
'''  添加一项
 
''' 
 
''' 
 
''' 
 
''' 
 
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
 
''' 
 
''' 删除所有选定项
 
''' 
 
''' 
 
''' 
 
''' 
 
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
————————————————
版权声明:本文为CSDN博主「gujiawei_27」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/gujiawei_27/article/details/6471455

相关教程