Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在对项目进行排序时,是否有办法保持列表框的索引?_Vb.net_Dictionary_Listbox - Fatal编程技术网

Vb.net 在对项目进行排序时,是否有办法保持列表框的索引?

Vb.net 在对项目进行排序时,是否有办法保持列表框的索引?,vb.net,dictionary,listbox,Vb.net,Dictionary,Listbox,我使用listbox从xml文件中获取项目,每个项目都有一个id、名称和内容, 但我只在列表框中输入名称,并将其他数据存储在变量中,如dictionary(整数:index,字符串:names),并保持列表框中的项与dictionary中的项之间的关系,现在一切都很好,但如果我在列表框中对项进行排序(修改名称后),它将丢失索引顺序:( 在分类之前 列表框-----------------字典--------------- 索引|值-----------------键|值 0 |管理员------

我使用listbox从xml文件中获取项目,每个项目都有一个id、名称和内容, 但我只在列表框中输入名称,并将其他数据存储在变量中,如dictionary(整数:index,字符串:names),并保持列表框中的项与dictionary中的项之间的关系,现在一切都很好,但如果我在列表框中对项进行排序(修改名称后),它将丢失索引顺序:(

在分类之前

列表框-----------------字典---------------

索引|值-----------------键|值

0 |管理员-----------------0 |管理员
1|adam.ley------------------1|adam.ley
2 | jean.clift------------------2 | jean.clift

将adam.ley更改为零,并对列表进行排序

0 |管理员-----------------0 |管理员
1 | jean.clift------------------1 |零
2 |零-------------------2 | jean.clift


谢谢大家:)

我想你们必须对这个项目进行分类

有不止一个细节的。 像

类将被称为“MyItem”,其中包含成员 (整数id, 字符串名, 字符串内容)

这将是类结构,并将此项(类)添加到列表框中 在c#中是可能的,在vb.net中一定有类似的内容

如需进一步参考,请查看此链接并获得提示。

因此,在我创建一个类之后,如下所示:

Public Class ListItem

    Private _Id As Integer
    Private _Name As String

    ' Initialize the object.
    Public Sub New(ByVal Id As Integer, ByVal Name As String)
        _Id = Id
        _Name = Name
    End Sub

    ' Return the object's Name.
    Public Overrides Function ToString() As String
        Return _Name
    End Function

    ' Return the object's Id.
    Public Function Id() As Integer
        Return _Id
    End Function

End Class
要将新项目添加到列表框,请执行以下操作:

ListBox1.Items.Add(New ListItem(Id, Name))
而且它工作得很好:)

但是我需要的是得到一个Id=3的项目的索引,例如


谢谢大家!祝你今天愉快;)

谢谢,我试试这个;)