2020年5月4日 星期一

[VB.NET] ListView的基本操作(新增、刪除、全選、上移、下移)及熱鍵操作 [ListView control(Add, Remove, SelectAll, Move Up, Move Down)]


新增
我習慣寫成一個副程式或函數,參數是資料內容,程式碼參考如下
1
Sub ListView1_AddNewItem(title As String, body As String)
2
    Dim item As New ListViewItem

3
    item.Text = title
4
    item.SubItems.Add(body)

5
    ListView1.Items.Add(item)
6
End Sub

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

3
    For i As Integer = ListView1.SelectedIndices.Count - 1 To 0 Step -1
4
        ListView1.Items.RemoveAt(ListView1.SelectedIndices(i))

5
    Next
6
End Sub

全選
for迴圈把所有項目的Selected 屬性改成True即可
1
Sub ListView1_SelectAll()
2
    '全選

3
    For Each item As ListViewItem In ListView1.Items
4
        item.Selected = True

5
    Next
6
End Sub

上移
這跟ListBox的差別在於插入與刪除的順序,ListBox可以先InsertRemoveAt,但是ListView在換位置的時候不能直接這樣做,否則會出現錯誤
要解決這個問題可以加上Clone 方法,程式碼參考如下
1
Dim index As Integer = ListView1.SelectedIndices(0)
2
Dim tmp As ListViewItem = ListView1.Items(index)

3
ListView1.Items.Insert(index - 1, tmp.Clone())
4
ListView1.Items.RemoveAt(index + 1)

或是將InsertRemoveAt的順序顛倒,程式碼參考如下
1
Dim index As Integer = ListView1.SelectedIndices(0)
2
Dim tmp As ListViewItem = ListView1.Items(index)

3
ListView1.Items.RemoveAt(index)
4
ListView1.Items.Insert(index - 1, tmp)

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

03
    If ListView1.SelectedIndices.Count > 0 Then
04
        'for迴圈由小到大去巡覽

05
        For i As Integer = 0 To ListView1.SelectedIndices.Count - 1
06
            Dim index As Integer = ListView1.SelectedIndices(i)

07
            '如果index為第一項就不需要上移
08
            If index > 0 Then

09
                '如果index-1被選取就不進行上移
10
                If ListView1.SelectedIndices.Contains(index - 1) Then

11
                    Continue For
12
                End If

13
                '進行換位置的動作
14
                Dim tmp As ListViewItem = ListView1.Items(index)

15
                ListView1.Items.RemoveAt(index)
16
                ListView1.Items.Insert(index - 1, tmp)

17
                ListView1.Items(index - 1).Focused = True
18
            End If

19
        Next
20
    End If

21
End Sub

下移
這跟上移的作法差不多,只是index的地方有些不同
01
Sub ListView1_MoveDown()
02
    '檢查有沒有選取項目

03
    If ListView1.SelectedIndices.Count > 0 Then
04
        'for迴圈由大到小去巡覽

05
        For i As Integer = ListView1.SelectedIndices.Count - 1 To 0 Step -1
06
            Dim index As Integer = ListView1.SelectedIndices(i)

07
            '如果index為最後一項就不需要下移
08
            If index < ListView1.Items.Count - 1 Then

09
                '如果index+1被選取就不進行下移
10
                If ListView1.SelectedIndices.Contains(index + 1) Then

11
                    Continue For
12
                End If

13
                '進行換位置的動作
14
                Dim tmp As ListViewItem = ListView1.Items(index)

15
                ListView1.Items.RemoveAt(index)
16
                ListView1.Items.Insert(index + 1, tmp)

17
                ListView1.Items(index + 1).Focused = True
18
            End If

19
        Next
20
    End If

21
End Sub

熱鍵操作
KeyDown事件中進行判斷,範例中對應的按鍵與指令如表格,特別需要注意的是e.Handled = True,設定後就不會把按鍵繼續送給作業系統處理
按鍵
指令
Delete
刪除
Ctrl + A
全選
Ctrl + ↑
上移
Ctrl + ↓
下移








01
Private Sub ListView1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListView1.KeyDown
02
    '熱鍵操作

03
    Select Case e.KeyCode
04
        Case Keys.Delete '刪除

05
           ListView1_DeleteItems()
06
        Case Keys.A '全選

07
            If e.Control Then
08
                ListView1_SelectAll()

09
            End If
10
        Case Keys.Up '上移

11
            If e.Control Then
12
                ListView1_MoveUp()

13
                e.Handled = True
14
            End If

15
        Case Keys.Down '下移
16
            If e.Control Then

17
                ListView1_MoveDown()
18
                e.Handled = True

19
            End If
20
    End Select

21
End Sub

沒有留言:

張貼留言