所以我有一个课程如下所示:
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
但我还希望列表中的实际索引随着所选项目向上或向下移动而更改。这种方式当我导出文件时,我可以保持用户选择的顺序。请指教?
答案 0 :(得分:3)
不要交换索引;交换价值。像
这样的东西Dim tempParam as parameters
tempParam = myList(I)
myList(I) = myList(I-1)
myList(I-1) = tempParam
答案 1 :(得分:0)
这是使用datasource属性的一种方法。列表框将填充列表中每个项目的tostring值(Params)。现在,如果您操作列表并重新加载列表框,它们应该保持同步,您可以将列表保存到文件中。要在列表框中显示更多信息,只需将其添加到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