[VB.NET] ListView的基本操作(新增、刪除、全選、上移、下移)及熱鍵操作

摘要:[VB.NET] ListView的基本操作(新增、刪除、全選、上移、下移)及其熱鍵操作

新增

我習慣寫成一個副程式或函數,參數是資料內容,程式碼參考如下

    Sub ListView1_AddNewItem(title As String, body As String)
        Dim item As New ListViewItem
        item.Text = title
        item.SubItems.Add(body)
        ListView1.Items.Add(item)
    End Sub

 

刪除

利用while迴圈去判斷有沒有選取,有選取的話就刪除第一個的選取項目,如此就可以把所有選取的項目都刪除

    Sub ListView1_DeleteItems()
        '把所有選取的項目都刪除
        For i As Integer = ListView1.SelectedIndices.Count - 1 To 0 Step -1
            ListView1.Items.RemoveAt(ListView1.SelectedIndices(i))
        Next
    End Sub

 

全選

用for迴圈把所有項目的Selected 屬性改成True即可

    Sub ListView1_SelectAll()
        '全選
        For Each item As ListViewItem In ListView1.Items
            item.Selected = True
        Next
    End Sub

 

上移

這跟ListBox的差別在於插入與刪除的順序,ListBox可以先InsertRemoveAt,但是ListView在換位置的時候不能直接這樣做,否則會出現錯誤
無法將項目 '測試項目' 加入或插入一個以上的位置。必須先將其從目前位置移除或複製。

要解決這個問題可以加上Clone 方法,程式碼參考如下

        Dim index As Integer = ListView1.SelectedIndices(0)
        Dim tmp As ListViewItem = ListView1.Items(index)
        ListView1.Items.Insert(index - 1, tmp.Clone())
        ListView1.Items.RemoveAt(index + 1)

或是將InsertRemoveAt的順序顛倒,程式碼參考如下

        Dim index As Integer = ListView1.SelectedIndices(0)
        Dim tmp As ListViewItem = ListView1.Items(index)
        ListView1.Items.RemoveAt(index)
        ListView1.Items.Insert(index - 1, tmp)

如果ListView要能MultiSelect,在做邊界處理的時候就會遇到另一個棘手的問題,當全部的選取項目都集中在最上方(或最下方),會發生選取的項目開始彼此換位置,為了解決這個問題我又加了一個條件式(如果index-1被選取就不進行上移)去處理

    Sub ListView1_MoveUp()
        '檢查有沒有選取項目
        If ListView1.SelectedIndices.Count > 0 Then
            '用for迴圈由小到大去巡覽
            For i As Integer = 0 To ListView1.SelectedIndices.Count - 1
                Dim index As Integer = ListView1.SelectedIndices(i)
                '如果index為第一項就不需要上移
                If index > 0 Then
                    '如果index-1被選取就不進行上移
                    If ListView1.SelectedIndices.Contains(index - 1) Then
                        Continue For
                    End If
                    '進行換位置的動作
                    Dim tmp As ListViewItem = ListView1.Items(index)
                    ListView1.Items.RemoveAt(index)
                    ListView1.Items.Insert(index - 1, tmp)
                    ListView1.Items(index - 1).Focused = True
                End If
            Next
        End If
    End Sub

 

下移

這跟上移的作法差不多,只是index的地方有些不同

    Sub ListView1_MoveDown()
        '檢查有沒有選取項目
        If ListView1.SelectedIndices.Count > 0 Then
            '用for迴圈由大到小去巡覽
            For i As Integer = ListView1.SelectedIndices.Count - 1 To 0 Step -1
                Dim index As Integer = ListView1.SelectedIndices(i)
                '如果index為最後一項就不需要下移
                If index < ListView1.Items.Count - 1 Then
                    '如果index+1被選取就不進行下移
                    If ListView1.SelectedIndices.Contains(index + 1) Then
                        Continue For
                    End If
                    '進行換位置的動作
                    Dim tmp As ListViewItem = ListView1.Items(index)
                    ListView1.Items.RemoveAt(index)
                    ListView1.Items.Insert(index + 1, tmp)
                    ListView1.Items(index + 1).Focused = True
                End If
            Next
        End If
    End Sub

 

熱鍵操作

KeyDown事件中進行判斷,範例中對應的按鍵與指令如表格,特別需要注意的是e.Handled = True,設定後就不會把按鍵繼續送給作業系統處理

按鍵 指令
Delete 刪除
Ctrl + A 全選
Ctrl + ↑ 上移
Ctrl + ↓ 下移

 

 

 

 

    Private Sub ListView1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListView1.KeyDown
        '熱鍵操作
        Select Case e.KeyCode
            Case Keys.Delete '刪除
               ListView1_DeleteItems()
            Case Keys.A '全選
                If e.Control Then
                    ListView1_SelectAll()
                End If
            Case Keys.Up '上移
                If e.Control Then
                    ListView1_MoveUp()
                    e.Handled = True
                End If
            Case Keys.Down '下移
                If e.Control Then
                    ListView1_MoveDown()
                    e.Handled = True
                End If
        End Select
    End Sub

 

 

範例程式下載(VB 2012): [VB.NET] ListView的基本操作及熱鍵操作.zip

文章內容僅提供技術分享,如有錯誤還請不吝指教。