gpt4 book ai didi

vb.net - 如何交换对象列表中的索引

转载 作者:行者123 更新时间:2023-12-05 02:24:00 28 4
gpt4 key购买 nike

所以我有一个如下所示的类:

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

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

最佳答案

不要交换索引;交换值。有点像

Dim tempParam as parameters 

tempParam = myList(I)
myList(I) = myList(I-1)
myList(I-1) = tempParam

关于vb.net - 如何交换对象列表中的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16985483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com