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

如何在VB.Net 2017中通过单选按钮对包含以(3个类别)破折号分隔名称的文件的列表框进行排序?,vb.net,listbox,filenames,delimiter,alphabetical-sort,Vb.net,Listbox,Filenames,Delimiter,Alphabetical Sort,因此,我在相当长的一段时间内构建了我的第一个程序,并成功地完成了大部分程序。现在我遇到了一个问题,需要征求一些建议 描述:我有一个记事本文本(歌词和和弦排列)文件目录,标题格式如下。 Songtitle-Artist-Key.txt 首先,我浏览他们的目录,它填充了一个列表框。然后,在我从上述列表框中选择一首歌曲后,它会填充3个文本框中的1个,这取决于我通过命令按钮将其分配给哪个文本框(有些歌曲最多有3页)。到目前为止,所有这些都可以正常工作。(未来的任务是在单击第一个文本框时,自动将一首有2或

因此,我在相当长的一段时间内构建了我的第一个程序,并成功地完成了大部分程序。现在我遇到了一个问题,需要征求一些建议

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

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

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

编辑:这正是我想要它说的,在阅读了如何提问页面并搜索了一整天之后,我重新打开这个问题

谢谢,
DiggDuggster

如果您使用的是旧版本的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

现在,您应该快速浏览网站,然后学习如何有效地使用网站。使用列表框
数据源
(类)作为列表框。该类需要包含从源文件派生的所有相关元素(标题、艺术家、键、歌曲文本、和弦等)。添加一些用于设置列表框可见列表格式的属性,并根据RadioButton选择将这些属性分配给列表框
DisplayMember
。ListView或TreeView可能会为您提供更多的数据显示选项。考虑到我发布这篇文章时已经是凌晨5点了,在搜索了昨天的大部分时间后,我猜我搜索得相当不错。不过,我会看看“如何提问”这篇文章。我甚至不知道还有一个!谢谢你的提醒。吉米,谢谢,我会进一步调查你说的话!所以我阅读了how to ask页面,广泛地搜索了几天的解决方案,我什么也没找到,甚至在其他网站上,所以我来到了我所知道的最好的社区,询问。我尽我所能清楚地陈述了我的情况,并以我认为需要提问的方式准确地陈述了我的问题。我已经作为客人使用这个网站大约10年了。也许可以正式解释为什么这不是一个好问题,而不是被指责?其他人能够毫无问题地理解我在问什么。我请求重新打开此文件进行答复。我正在使用Visual Studio 2017社区版。让我开始编辑任何需要更改的控件名。非常感谢!好的,Mary,我已经插入了这段代码,根据需要重命名了代码中的控件,并试图消除错误。我仍有1个错误:严重性代码说明项目文件行抑制状态消息“.ctor”不是有效标识符。0当我搜索此字符串时,它告诉我该字符串不存在。我现在会仔细检查我的文件名,以确认它不是文件命名约定错误,并就此与您联系。再次感谢。以下是我的文件命名约定示例:。。。更改为Stevie Ray Vaughan-G#m.txtNow在尽我所能纠正问题后,我可以打开目录,列表正确填充,我可以将歌词/和弦表放在任何窗格中,但是,当我使用单选按钮对其排序时,它会清空列表框,程序锁定,我无法再次加载列表框。它给了我这个错误。。。System.ArgumentException:“设置DataSource属性时无法修改Items集合。”此外,我的表单名称为Textera_Form,项目名称为Textera1a