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

datasource プロパティを使用する 1 つの方法を次に示します。リストボックスには、リスト (Params) 内の各項目の 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 に答える