Home > OS >  sort items in a listbox which contain integer from highest to lowest for VB.NET
sort items in a listbox which contain integer from highest to lowest for VB.NET

Time:06-16

Design View of the program

(I have about 3 weeks of knowledge so sorry if i cant use the right naming conventions (like string for words)).

I would recommend reading this and then looking at the image I've provided. Also anything red ignore it and blue shows where data travels

To sum it up, I'm entering data, for example, a most football goals challenge and will enter the persons name how many points they achieved (1-100) and the event (series 1,2 or 3) this will then be entered into the 'solo' listbox and repeated until 20 participants have been added into the listbox. Then i will click the 'Add to Rank' button and all the items within solo listbox will be added to the 'RANK', listbox.

Now this is the tricky part, all I'm trying to do is sort the listbox by descending points (most to least, 100-1) which is why i put the points first, but i don't know how to use classes or arrays, hope this is enough information, please respond asap thanks lads

CodePudding user response:

Can you maybe share you code? would be nice too see what you are doing.

Dim List As New List(Of Integer)

    'Add ListBox Items to List
    For Each ListBoxItem In ListBox1.Items
        List.Add(ListBoxItem)
    Next

    'Sort the List and Clear the ListBox
    List.Sort()
    ListBox1.Items.Clear()

    'Add to ListBox Ascending
    For Each ListItem In List
        ListBox1.Items.Add(ListItem)
    Next

    'Add to ListBox Descending
    For i = List.Count - 1 To 0 Step -1
        ListBox1.Items.Add(List(i))
    Next

small example here for you

CodePudding user response:

Don't store the data in the UI. Store it in data structures in code. Here is some code you can use to do that.

First of all, make a class to hold a Solo. This is a fundamental of object oriented programming. It will give you easy access to the properties of the solo, importantly the score which you can use to sort. Then it doesn't matter in what order you display it.

Public Class Solo
    Public Sub New(name As String, score As Integer, series As String)
        Me.Name = name
        Me.Score = score
        Me.Series = series
    End Sub

    Public Property Name As String
    Public Property Score As Integer
    Public Property Series As String

    Public ReadOnly Property Text As String
        Get
            Return $"{Name}, {Series}, {Score} Points"
        End Get
    End Property

End Class

In your form, you can use this code. It will bind the data to the ListBoxes. You still have the data in your code to do other things with.

Private serieses As List(Of String)
Private solos As List(Of Solo)


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    serieses = New List(Of String)() From {"Series 1", "Series 2", "Series 3"}
    solos = New List(Of Solo)()
    Me.SeriesComboBox.DataSource = serieses
End Sub

Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
    Dim score As Integer
    If Not String.IsNullOrEmpty(NameTextBox.Text) AndAlso Integer.TryParse(ScoreTextBox.Text, score) Then
        solos.Add(New Solo(NameTextBox.Text, score, SeriesComboBox.Text))
        bindSolos()
    End If
End Sub

Private Sub ShowRankButton_Click(sender As Object, e As EventArgs) Handles ShowRankButton.Click
    bindRanks()
End Sub

Private Sub bindSolos()
    SoloListBox.DataSource = Nothing
    SoloListBox.DataSource = solos
    SoloListBox.DisplayMember = "Text"
End Sub

Private Sub bindRanks()
    RankListBox.DataSource = Nothing
    RankListBox.DataSource = solos.OrderByDescending(Function(s) s.Score).ToList()
    RankListBox.DisplayMember = "Text"
End Sub

Note the series are populated in Form_Load, not in the designer. In fact there is no data in the designer, as it should be.

enter image description here

You can rethink the rank button. You don't need it at all and can just bind the sorted ranks when you add, like this

Private Sub bindSolos()
    SoloListBox.DataSource = Nothing
    SoloListBox.DataSource = solos
    SoloListBox.DisplayMember = "Text"
    RankListBox.DataSource = solos.OrderByDescending(Function(s) s.Score).ToList()
    RankListBox.DisplayMember = "Text"
End Sub

And really, you can just automatically just sort the solo ListBox and get rid of the second ListBox, so when you Add, they are always displayed in the solo ListBox sorted.

Private Sub bindSolos()
    SoloListBox.DataSource = Nothing
    SoloListBox.DataSource = solos.OrderByDescending(Function(s) s.Score).ToList()
    SoloListBox.DisplayMember = "Text"
End Sub
  • Related