加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

ListView 控制 VB.net

发布时间:2020-12-16 23:22:29 所属栏目:大数据 来源:网络整理
导读:Public Class ListViewOperation Shared bRowsNum As Boolean = False '初期化ListView ‘lstCTL :ListView控件变量 ’columns() :一维字符串数组,ListView的列标题 Public Shared Sub list_init(ByRef lstCTL As System.Windows.Forms.ListView,ByVal co

Public Class ListViewOperation
Shared bRowsNum As Boolean = False
'初期化ListView

‘lstCTL :ListView控件变量

’columns() :一维字符串数组,ListView的列标题
Public Shared Sub list_init(ByRef lstCTL As System.Windows.Forms.ListView,ByVal columns() As String,Optional ByVal RowNum As Boolean = False)

If lstCTL Is Nothing Then
Exit Sub
End If

Dim lstComm As System.Windows.Forms.ListView = lstCTL

'clear old data
lstComm.Items.Clear()
lstComm.Columns.Clear()
lstComm.View = View.Details
lstComm.MultiSelect = True
lstComm.FullRowSelect = True
lstComm.HideSelection = False
Dim strColumn As String

'add RowNum
bRowsNum = RowNum
If bRowsNum Then
lstComm.Columns.Add(" ",30)
End If

If columns Is Nothing Then
lstComm.View = View.List
lstComm.MultiSelect = False
Exit Sub
End If

'set column name
For Each strColumn In columns
If strColumn IsNot Nothing Then
If strColumn.Trim.Equals("") Then
Continue For
End If
lstComm.Columns.Add(strColumn,strColumn.Length * 6 + 50)
End If

If strColumn Is Nothing Then
Exit For
End If
Next

'set gridline type
lstComm.GridLines = True
End Sub

''' <summary>
''' 从ListView最后一行开始添加数据行
''' </summary>
''' <param name="lstCTL">ListView控件变量</param>
''' <param name="strItems">二维数组 第二维是行,第一维是一行的所有列</param>
''' <remarks></remarks>
Public Shared Sub list_addData(ByRef lstCTL As System.Windows.Forms.ListView,ByVal strItems()() As String)

If strItems Is Nothing Then
Exit Sub
End If

If lstCTL Is Nothing Then
Exit Sub
End If

Dim lstComm As System.Windows.Forms.ListView = lstCTL

Dim RowCounter As Integer
Dim ColCounter As Integer
Dim RowCurOrder As Integer = lstComm.Items.Count
Dim ColStartOrder As Integer
Dim bRowVaild As Boolean = False
Dim strColValue As String
Dim strInvaildRowCount As Integer = 0
' add row
For RowCounter = 0 To UBound(strItems)
If strItems(RowCounter) IsNot Nothing Then
bRowVaild = False
Dim listItem As System.Windows.Forms.ListViewItem
If bRowsNum Then ' add row number [column]
listItem = lstComm.Items.Insert(RowCounter + RowCurOrder,RowCurOrder.ToString)
ColStartOrder = 0
Else
'if row info is empty,not append it
If strItems(RowCounter).ToString.Trim.Equals("") Then
Continue For
End If

'add info to last row
strColValue = strItems(RowCounter)(0)
listItem = lstComm.Items.Insert(RowCounter + RowCurOrder - strInvaildRowCount,strColValue)
ColStartOrder = 1

If Not String.IsNullOrEmpty(strColValue) Then
bRowVaild = True
End If
End If

'add column
For ColCounter = ColStartOrder To strItems(RowCounter).Length - 1

Dim listItemsubItem As New System.Windows.Forms.ListViewItem.ListViewSubItem
strColValue = strItems(RowCounter)(ColCounter)
listItemsubItem.Text = strColValue
listItem.SubItems.Insert(ColCounter,listItemsubItem)
If Not String.IsNullOrEmpty(strColValue) Then
bRowVaild = True
End If

Next ColCounter

'if row'a all column is empty,row is invaild,so delete it
If Not bRowVaild Then
Dim count As Integer = lstComm.Items.Count
lstComm.Items.RemoveAt(count - 1)
strInvaildRowCount += 1
End If
End If
Next RowCounter
End Sub

''' <summary>
''' 取得ListView中的所有数据
''' </summary>
''' <param name="lstCTL"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function list_getData(ByRef lstCTL As System.Windows.Forms.ListView) As String()()

If lstCTL Is Nothing Then
Return Nothing
End If

Dim iCount As Integer = lstCTL.Items.Count
Dim strItems(iCount - 1)() As String
Dim iLoop As Integer = 0
Dim iLooop As Integer = 0
'get list view data info
For iLoop = 0 To iCount - 1
Dim strItem As String()
Dim strCol As String = CStr("")
Dim start As Integer = 0

'Show RowsNumber
If bRowsNum Then
start = 1
End If
'get one row info
For iLooop = start To lstCTL.Items.Item(iLoop).SubItems.Count - 1
If iLooop <> start Then
strCol += ";"
End If
strCol += lstCTL.Items.Item(iLoop).SubItems.Item(iLooop).Text
Next
strItem = Split(strCol,";")
strItems(iLoop) = strItem
Next
Return strItems
End Function

''' <summary>
''' 取得ListView中选择的行当数据
''' </summary>
''' <param name="lstCTL"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function list_getSeledData(ByRef lstCTL As System.Windows.Forms.ListView) As String()()

If lstCTL Is Nothing Then
Return Nothing
End If

'get select row count
Dim iCount As Integer = lstCTL.SelectedItems.Count
Dim strItems(iCount - 1)() As String
Dim iLoop As Integer = 0
Dim iLooop As Integer = 0

'get select info
For iLoop = 0 To iCount - 1
Dim strItem As String()
Dim strCol As String = CStr("")
Dim start As Integer = 0

'Show RowsNumber
If bRowsNum Then
start = 1
End If
'get one row info
For iLooop = start To lstCTL.SelectedItems.Item(iLoop).SubItems.Count - 1
If iLooop <> start Then
strCol += ";"
End If
strCol += lstCTL.SelectedItems.Item(iLoop).SubItems.Item(iLooop).Text
Next
strItem = Split(strCol,";")
strItems(iLoop) = strItem
Next

Return strItems
End Function

''' <summary>
''' 删除选择的行
''' </summary>
''' <param name="lstCTL"></param>
''' <remarks></remarks>
Public Shared Sub list_DelData(ByRef lstCTL As System.Windows.Forms.ListView)
Dim selCount As Integer = lstCTL.SelectedItems.Count
Dim iLoop As Integer
If selCount >= 1 Then
For iLoop = selCount - 1 To 0 Step -1
lstCTL.SelectedItems.Item(iLoop).Remove()
Next
End If
End Sub

''' <summary>
''' 删除listView中的所有数据
''' </summary>

''' <param name="lstCTL"></param>
''' <remarks></remarks>
Public Shared Sub list_DelAllData(ByRef lstCTL As System.Windows.Forms.ListView)

Dim selCount As Integer = lstCTL.Items.Count
Dim iLoop As Integer
If selCount >= 1 Then
For iLoop = selCount - 1 To 0 Step -1
lstCTL.Items.Item(iLoop).Remove()
Next iLoop
End If
End Sub


''' <summary>
''' 更新一行数据
''' </summary>
''' <param name="lstCTL"></param>

''' <param name="Index ">行号,从0开始</param>

''' <param name="mapColValue ">修改的值对,列番号-列值一一对应</param> ''' <remarks></remarks> Public Shared Sub list_UpdateSingleData(ByRef lstCTL As System.Windows.Forms.ListView,ByVal Index As Integer,ByVal mapColValue As Map) Dim selCount As Integer = lstCTL.Items.Count Dim iLoop As Integer If Index < selCount Then For iLoop = 0 To mapColValue.count - 1 lstCTL.Items.Item(Index).SubItems.Item(iLoop).Text = mapColValue.itemValue(iLoop) ' lstCTL.Items.Item(Index).SubItems.Item(mapColValue.itemKey(iLoop)) = listItemsubItem Next iLoop End If End Sub ''' <summary> ''' delete listview all data info ''' </summary> ''' <param name="lstCTL"></param> ''' <remarks></remarks> Public Shared Function list_GetSelIndex(ByRef lstCTL As System.Windows.Forms.ListView) As Integer Dim selCount As Integer = lstCTL.Items.Count Dim iRetValue As Integer = -1 If lstCTL.SelectedItems.Count > 0 Then iRetValue = lstCTL.SelectedItems.Item(0).Index End If Return iRetValue End Function End Class

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读