2

所以我有一个如下所示的课程:

Public Class parameters
    Public Property test As String
    Public Property test_type As String
    Public Property user_test_name As String
    Public Property meas As String
    Public Property spec_min As String
    Public Property spec_max As String
    Public Property spec_unit As String

    Public Overrides Function ToString() As String
        Return user_test_name
    End Function
End Class

我已将每个对象写入对象列表并将它们写入列表框。

我想上下移动列表框中的项目,我已通过以下代码成功完成了此操作:

Private Sub up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up.Click
    'Move up

    '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

Private Sub down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles down.Click

    'Move down

    '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

但我也希望列表中的实际索引随着所选项目向上或向下移动而改变。这样,当我导出文件时,我可以保持用户选择的顺序。请指教?

4

2 回答 2

4

不要交换索引;交换值。就像是

Dim tempParam as parameters 

tempParam = myList(I)
myList(I) = myList(I-1)
myList(I-1) = tempParam
于 2013-06-07T18:06:51.070 回答
0

这是使用数据源属性的一种方法。列表框将填充列表中每个项目的 tostring 值(参数)。现在,如果您操作列表并重新加载列表框,它们应该保持同步,您可以将列表保存到文件中。要在列表框中显示更多信息,只需将其添加到 tostring 方法的返回值即可。

    Private Sub up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up.Click
        'Move up
        Dim TempParam As parameters = Params(ListBox1.SelectedIndex)
        'Make sure our item is not the first one on the list.
        If ListBox1.SelectedIndex > 0 Then

            Dim I = ListBox1.SelectedIndex - 1
            Params.Insert(I, TempParam)
            Params.RemoveAt(ListBox1.SelectedIndex + 1)
            ListBox1.DataSource = Nothing
            ListBox1.Items.Clear()
            ListBox1.DataSource = Params
            ListBox1.SelectedIndex = I
        End If

    End Sub

    Private Sub down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles down.Click

        'Move down
        Dim TempParam As parameters = Params(ListBox1.SelectedIndex)
        '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
            Params.Insert(I, TempParam)
            Params.RemoveAt(ListBox1.SelectedIndex)
            ListBox1.DataSource = Nothing
            ListBox1.Items.Clear()
            ListBox1.DataSource = Params
            ListBox1.SelectedIndex = I - 1

        End If
    End Sub

    Dim Params As New List(Of parameters)

    Private Sub save_Click(sender As System.Object, e As System.EventArgs) Handles save.Click

    End Sub
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Params.AddRange({New parameters("Test1", "Test1a", "Test1b", "Test1c", "Test1d", "Test1e", "Test1f"), _
                         New parameters("Test2", "Test2a", "Test2b", "Test2c", "Test2d", "Test2e", "Test2f"), _
                         New parameters("Test3", "Test3a", "Test3b", "Test3c", "Test3d", "Test3e", "Test3f")})
        ListBox1.DataSource = Params
    End Sub
End Class
Public Class parameters
    Public Property test As String
    Public Property test_type As String
    Public Property user_test_name As String
    Public Property meas As String
    Public Property spec_min As String
    Public Property spec_max As String
    Public Property spec_unit As String
    Public Sub New(pTest As String, pTest_Type As String, pUser_Test_Name As String, pMeas As String, pSpec_Min As String, pSpec_Max As String, pSpec_Unit As String)
        test = pTest
        test_type = pTest_Type
        user_test_name = pUser_Test_Name
        meas = pMeas
        spec_min = pSpec_Min
        spec_max = pSpec_Max
        spec_unit = pSpec_Unit
    End Sub
    Public Overrides Function ToString() As String
        Return user_test_name
    End Function
End Class
于 2013-06-07T18:00:34.330 回答