English 中文(简体)
我如何在列表框中排序项目, 同时又保持其他列表框的数据一致?
原标题:How do I sort items in a listbox while keeping the other listboxes data aligned?

我有这个XML文件 我从中抓取所有数据:

<?xml version="1.0" encoding="utf-8"?>
<Tabel>
  <Member>
    <Naam>Cruciatum</Naam>
    <Kills>10</Kills>
    <Deaths>2</Deaths>
    <Score>2222</Score>
  </Member>
  <Member>
    <Naam>test</Naam>
    <Kills>123</Kills>
    <Deaths>12</Deaths>
    <Score>12222</Score>
  </Member>
  <Member>
    <Naam>test2</Naam>
    <Kills>159</Kills>
    <Deaths>12</Deaths>
    <Score>2222</Score>
  </Member>
  <Member>
    <Naam>test3</Naam>
    <Kills>159</Kills>
    <Deaths>122</Deaths>
    <Score>222284</Score>
  </Member>
  <Member>
    <Naam>test4</Naam>
    <Kills>15</Kills>
    <Deaths>1229</Deaths>
    <Score>129453</Score>
  </Member>
</Tabel>

我用5个不同的列表框(每个儿童节点1个在“成员”栏下)来显示。

我有这个代码 这样做的代号。

Public Class Rank
    Dim memberNodes As XmlNodeList
    Dim memberNode As XmlNode
    Dim x As Short
    Dim dataNodes As XmlNodeList
    Dim firstinrow As Boolean
    Dim datalist(5) As String
    Dim y As Short

    Private Sub Rank_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        lstView.Items.Clear()
        x = 0
        Dim doc As New XmlDocument()
        doc.Load("C:Members.xml")
        memberNodes = doc.GetElementsByTagName("Member")
        For Each memberNode In memberNodes
            dataNodes = memberNode.ChildNodes
            y = 1
            For Each dataNode As XmlNode In dataNodes
                datalist(y) = dataNode.InnerText
                datalist(0) = (x + 1).ToString
                y += 1
            Next
            datalist(5) = datalist(4)
            datalist(4) = FormatNumber((datalist(2) / datalist(3)), 3)
            Dim lvi As New ListViewItem(datalist)
            lstView.Items.Add(lvi)
            x += 1
        Next

    End Sub
End Class

That code is working fine for now, displaying the full list. But now I need all the data sorted by the values in the Score column of the listview, so for in the following example: Result so far

我需要第一行说:"1 测试3 159 122 122 1.33 222284"

第二行,分数第二名,等等。

Update: Instead of seperate listboxes I m now using 1 listview, as recommended by @SteveDog

最佳回答

使用“ ListView” 控件来取代单独的列表框, 使用“ View” 控件来设置“ View ” 属性, 或者使用“ DataGridView” 控件。 如果您必须使用这样的单独列表框, 您将需要在每类上安装可比较的界面, 以取代排序 。

在 ListView 控件中排序显然有点疼。 保持灵活性是件好事, 但只要您想要做的只是一件简单的事情, 它就是一件麻烦。 首先, 您需要创建一个执行 IComparer 界面的排序对象。 例如 :

Public Class ScoreSorter
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Try
            Dim xItem As ListViewItem = CType(x, ListViewItem)
            Dim yItem As ListViewItem = CType(y, ListViewItem)
            Dim xInt As Integer = Integer.Parse(xItem.SubItems(5).Text)
            Dim yInt As Integer = Integer.Parse(yItem.SubItems(5).Text)
            Return yInt - xInt
        Catch
            Return 0
        End Try
    End Function
End Class

然后,您需要将 ListView 控制 S ListViewItsSorter 属性设置为排序对象的新实例,然后告诉它进行排序,例如:

ListView1.ListViewItemSorter = New ScoreSorter()
ListView1.Sort()
问题回答

尝试此( 未测试) :

Dim sortedNodes = memberNodes.OrderBy(Function(n) n.ChildNodes(3))
For Each memberNode In sortedNodes
   ...
Next

您需要有一个 < code> imports System.Linq 。





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签