所以我有一堂课,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我已将每个对象写入对象列表中,并将它们写入列表框。
我想上下移动列表框中的项目,我已经通过以下代码成功完成了此操作:
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
Run Code Online (Sandbox Code Playgroud)
但我还希望列表中的实际索引随着所选项目向上或向下移动而改变。这样,当我导出文件时,我可以保留用户选择的顺序。请指教?
不要交换索引;交换值。就像是
Dim tempParam as parameters
tempParam = myList(I)
myList(I) = myList(I-1)
myList(I-1) = tempParam
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4678 次 |
最近记录: |