如何在 VB.Net 2017 中通过单选按钮对包含(3 类)破折号分隔名称的文件列表框进行排序?

How to sort a listbox full of files with (3 category) dash delimited names via radio buttons in VB.Net 2017?

所以我在很长一段时间内构建了我的第一个程序,并且成功地完成了大部分程序。现在我 运行 遇到了问题,需要寻求一些建议。

描述:我有一个记事本文本(歌词和和弦编排)文件目录,标题格式如下。 Songtitle-Artist-Key.txt 首先,我浏览他们的目录,其中包含一个列表框。然后,在我 select 所述列表框中的一首歌曲之后,它会填充 3 个文本框中的 1 个,具体取决于我通过命令按钮将其分配给哪个文本框(有些歌曲最多有 3 页。)到目前为止,所有这些工作正常。 (未来的任务是在单击第一个文本框时自动将 2 或 3 页的歌曲放入每个相应的文本框。)

目标:我在一个组控件中有 3 个单选按钮,分别标记为“标题”、“艺术家”和“键”。这些用于按各自的名称、标题、艺术家和键对列表框进行排序。

问题:我能否根据分隔的类别对该列表框进行排序,或者我是否需要有 3 个单独的列表框(以及如何使它们保持同步),或者我是否需要涉及某种数据元素?欢迎任何建议、想法、示例或想法!

编辑:这正是我想说的,阅读了如何提问页面并搜索了一整天后,我重新提出这个问题。

谢谢, DiggDugster

如果您使用的是 vb 的旧版本,您将无法使用内插字符串 ($)。更改为 String.Format。其他解释在代码注释中。

Public Class frmSortedList
    Private lstSongs As New List(Of Song)
    Public SortOrder As Integer = 0
    Private Sub frmSortedList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim lstFileName As New List(Of String)
        lstFileName.Add("Star Spangled Banner-Francis Scott Keyes-G.txt")
        lstFileName.Add("Jailhouse Rock-Elvis Presley-F.txt")
        lstFileName.Add("Macho Insecurity-Dead Kennedys-G.txt")
        lstFileName.Add("(We Are) The Road Crew-Motorhead-F.txt")
        lstFileName.Add("Moonlight Drive-The Doors-B.txt")
        lstFileName.Add("While My Guitar Gently Weeps-The Beatles-B.txt")
        lstFileName.Add("Waffle-Sevendust-G.txt")
        lstFileName.Add("Don't Take Your Guns To Town-Johnny Cash-G.txt")
        lstFileName.Add("Where Eagles Dare-Iron Maiden-B.txt")
        lstFileName.Add("London Calling-The Clash-F.txt")
        'your list will come from your Directory
        'loop through you list of file names (or an array of file names
        'call the constructor of Song passing the file name
        'add the Song object to the list
        For Each file As String In lstFileName
            Dim s As New Song(file)
            lstSongs.Add(s)
        Next
        'The ListBox will call .ToString on the objects you add
        'to determine what to display
        ListBox1.DataSource = lstSongs
        ListBox1.ValueMember = "FileName"
        'To access any of the properties of the items in the ListBox
        'Cast the object stored in the ListBox back to song
        Dim item As Song = CType(ListBox1.SelectedItem, Song)
        Debug.Print($"Title: {item.Title}, Artist: {item.Artist}, Key: {item.MusicKey}")
        'You can set the value property to any property of Song
        Dim thefile As String = CStr(ListBox1.SelectedValue)
        Debug.Print(thefile)
    End Sub
    Private Sub btnArtist_Click(sender As Object, e As EventArgs) Handles btnArtist.Click
        'A little link code to sort the list
        SortOrder = 1
        Dim lstArtists As List(Of Song) = lstSongs.OrderBy(Function(s) s.Artist).ToList
        ListBox1.DataSource = Nothing
        ListBox1.DataSource = lstArtists
        ListBox1.ValueMember = "FileName"
    End Sub

    Private Sub btnTitle_Click(sender As Object, e As EventArgs) Handles btnTitle.Click
        SortOrder = 0
        Dim lstTitle As List(Of Song) = lstSongs.OrderBy(Function(s) s.Title).ToList
        ListBox1.DataSource = Nothing
        ListBox1.DataSource = lstTitle
        ListBox1.ValueMember = "FileName"
    End Sub

    Private Sub btnKey_Click(sender As Object, e As EventArgs) Handles btnKey.Click
        SortOrder = 2
        Dim lstKey As List(Of Song) = lstSongs.OrderBy(Function(s) s.MusicKey).ToList
        ListBox1.DataSource = Nothing
        ListBox1.DataSource = lstKey
        ListBox1.ValueMember = "FileName"
    End Sub
End Class
Public Class Song
    Public ReadOnly Property Title As String
    Public ReadOnly Property Artist As String
    Public ReadOnly Property MusicKey As String
    Public ReadOnly Property ShortFileName As String
    Public Property FileName As String
    Public Sub New(file As String)
        FileName = file 'the full file name that was passed to the constructor
        Dim index As Integer = file.IndexOf(".") 'Find where the extension starts
        ShortFileName = file.Remove(index) 'Get rid of .txt
        Dim parts As String() = ShortFileName.Split("-"c) 'Split returns and array
        Title = parts(0)
        Artist = parts(1)
        MusicKey = parts(2)
    End Sub
    Public Overrides Function ToString() As String
        'The ListBox calls .ToString on the objects you added
        'You can return any property of combination of properties
        'you want to display
        'if you don't override you will get the fully qualified name of the object Argh!
        Select Case frmSortedList.SortOrder
            Case 0
                Return ShortFileName
            Case 1 'Artist
                Return $"{Artist} - {Title} -  {MusicKey}"
            Case 2 'key
                Return $"{MusicKey} - {Title} - {Artist}"
            Case Else
                Return ShortFileName
        End Select
    End Function
End Class