2020年5月17日 星期日

vb.net ListBox項目上下移動 [Listbox item move up down]


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    'Move up

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Make sure our item is not the first one on the list.

        If ListBox1.SelectedIndex > 0 Then

            Dim I = ListBox1.SelectedIndex - 1

            ListBox1.Items.Insert(I, ListBox1.SelectedItem)

            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

            ListBox1.SelectedIndex = I

        End If

    End Sub

   

    'Move down

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'Make sure our item is not the last one on the list.

        If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then


           'Insert places items above the index you supply, since we want

           'to move it down the list we have to do + 2

            Dim I = ListBox1.SelectedIndex + 2

            ListBox1.Items.Insert(I, ListBox1.SelectedItem)

            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

            ListBox1.SelectedIndex = I - 1

        End If

    End Sub

沒有留言:

張貼留言