所以我有一个如下所示的课程:
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
但我也希望列表中的实际索引随着所选项目向上或向下移动而改变。这样,当我导出文件时,我可以保持用户选择的顺序。请指教?