這是非常有用的,我用它爲我的項目,但它有點不同。我有一個列表視圖,其中第一列是固定索引,因此用戶知道列表中有多少項目,並且可以通過編號引用它們。在上下移動項目時,這是一個問題,因爲第1列中的索引編號需要保持不變並且只更改第2列中的數據。我編寫了下面的類,該類還支持添加,刪除和重命名第2列項目。
Public Class class_ListViewControl
Public ListViewControl_DebugMode As Boolean = True ' to show exceptions in debug mode
Public Sub Init_ListView(ByRef SelectedListView As ListView, ByVal FullRowSelect As Boolean, ByVal ViewMode As View, ByVal Columns() As String)
' Pass the list to modify, two common settings and an array of columns by string name
Try
SelectedListView.View = ViewMode
SelectedListView.FullRowSelect = FullRowSelect
SelectedListView.Columns.Clear()
For i As Integer = 0 To Columns.Length - 1
SelectedListView.Columns.Add(Columns(i), -2)
Next
Catch ex As Exception
If ListViewControl_DebugMode = True Then MsgBox(ex.ToString)
End Try
End Sub
Public Sub MoveListViewItem(ByRef SelectedlistView As ListView, ByVal move As ArrowDirection)
' Pass the listview to edit and the command UP/DOWN
Try
' Select between UP/DOWN arrows
If move = ArrowDirection.Up And SelectedlistView.SelectedItems.Count > 0 Then
Dim selected As ListViewItem = SelectedlistView.SelectedItems(0)
Dim indx As Integer = selected.Index
Dim totl As Integer = SelectedlistView.Items.Count
If indx > 0 Then ' Make sure it's not 0
Dim temp As String = SelectedlistView.Items(indx - 1).SubItems(1).Text ' save the string from the old one first so we can put it in the new location
' move the text in column 2 to the column 2 index below it and place the old text in the next column
SelectedlistView.Items(indx - 1).SubItems(1).Text = SelectedlistView.Items(indx).SubItems(1).Text
SelectedlistView.Items(indx).SubItems(1).Text = temp
' This clears the selection then selects the new index before existing. This allows the user to hit UP again with out having to reselect the item
SelectedlistView.SelectedItems.Clear()
SelectedlistView.Items(indx - 1).Selected = True
End If
ElseIf move = ArrowDirection.Down And SelectedlistView.SelectedItems.Count > 0 Then
Dim selected As ListViewItem = SelectedlistView.SelectedItems(0)
Dim indx As Integer = selected.Index
Dim totl As Integer = SelectedlistView.Items.Count
If indx < SelectedlistView.Items.Count - 1 Then ' if it's less than the max index you can just move the data to the next one
Dim temp As String = SelectedlistView.Items(indx + 1).SubItems(1).Text
SelectedlistView.Items(indx + 1).SubItems(1).Text = SelectedlistView.Items(indx).SubItems(1).Text
SelectedlistView.Items(indx).SubItems(1).Text = temp
' This clears the selection then selects the new index before existing. This allows the user to hit DOWN again with out having to reselect the item
SelectedlistView.SelectedItems.Clear()
SelectedlistView.Items(indx + 1).Selected = True
End If
End If
Catch ex As Exception
If ListViewControl_DebugMode = True Then MsgBox(ex.ToString)
End Try
End Sub
Public Sub EditListViewItem(ByRef SelectedListView As ListView, ByVal EditType As Char, ByVal NewText As String)
' Pass the listview to edit, EditType: ADD = 'A' DELETE = 'D', and Index of item
Try
Dim selected As ListViewItem = Nothing
Dim indx As Integer = 0
If SelectedListView.SelectedIndices.Count > 0 Then ' Only load if something is selected
selected = SelectedListView.SelectedItems(0)
indx = selected.Index
Else
' This is here because you don't need an index selected to add a new item.
selected = Nothing
indx = 0 ' set to 0
End If
Dim total As Integer = SelectedListView.Items.Count
Select Case EditType
Case "A" ' ADD NEW
Dim NewItem As New ListViewItem(CStr(total + 1))
NewItem.SubItems.Add(NewText)
SelectedListView.Items.Add(NewItem)
Case "D" ' DELETE
If selected IsNot Nothing Then ' only execute if something is selected
SelectedListView.Items.Remove(selected) ' Delete the selected item
For i As Integer = 0 To SelectedListView.Items.Count - 1
SelectedListView.Items(i).SubItems(0).Text = CStr(i + 1)
Next
End If
Case "S" ' Save edits
SelectedListView.Items(indx).SubItems(1).Text = NewText
End Select
Catch ex As Exception
If ListViewControl_DebugMode = True Then MsgBox(ex.ToString)
End Try
End Sub
末級
要使用這個類,你的表格1點聲明補充:
Private ListViewClass As New class_ListViewControl
然後在Form_Load子添加您的初始化:
ListViewClass.Init_ListView(lstv_RFPInfo_Options, True, View.Details, {"#", "Option Title"})
添加按鈕例如:
ListViewClass.EditListViewItem(listview1, "A", textbox1.Text)
刪除按鈕示例:
ListViewClass.EditListViewItem(listview1, "D", "")
Form 1 example
沒有理由來執行代碼每個'cmdDown_Click'函數執行時間。將其設置在窗體的構造函數中。 – 2012-01-14 17:07:11
根據科迪的建議編輯上述帖子。 – 2012-01-14 17:22:51