当前位置:
首页 > Python基础教程 >
-
vb.net教程之VB.NET 中 使用 ListView 控件的简单例子
ListView 控件 在 程序开发过程中的使用是非常广泛的。因为其不支持数据库的绑定,所以在数据库程序开发领域无法与datagridview抗衡 ,但是ListView的确是一个非常好用的控件。下面就把 一个简单的 ListView的例子发出来。
Public Class Form6
''' <summary>
''' 英雄类
''' </summary>
''' <remarks></remarks>
Public Class hero
Private s_name As String
Private s_age As Long
Private s_like As String
Public 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() As Long
Get
Return s_age
End Get
Set(ByVal value As Long)
s_age = value
End Set
End Property
Public 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(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(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(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(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(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(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(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(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(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(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(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博主「precipitant」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/precipitant/article/details/1441018
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式