You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.Net 2017:如何用单选按钮排序三类短横线分隔文件名的ListBox

嘿,时隔许久重新开发程序还搞定了大部分功能,这已经超棒啦!针对你需要给ListBox里的歌词文件按歌曲名、歌手、调式排序的需求,我整理了具体的实现思路和代码示例,应该能帮到你:

核心思路

因为你的文件名是固定格式Songtitle-Artist-Key.txt,所以我们可以先把每个文件名拆分成歌曲名、歌手、调式三个字段,然后根据单选按钮的选中状态,对对应字段进行排序,最后把排序后的结果重新加载到ListBox中。

1. 编写文件名解析辅助方法

先写一个方法来拆分文件名,提取我们需要的三个字段:

private (string SongTitle, string Artist, string Key) ParseFileName(string fileName)
{
    // 先去掉文件后缀(.txt)
    var nameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
    
    // 按'-'拆分,用Split的重载确保只拆分成3部分(避免歌曲名里含'-'的情况)
    var parts = nameWithoutExtension.Split(new[] {'-'}, 3);
    
    // 确保拆分后有有效字段,没有的话返回空值
    if (parts.Length == 3)
    {
        return (parts[0].Trim(), parts[1].Trim(), parts[2].Trim());
    }
    return ("", "", "");
}

2. 实现排序逻辑并绑定单选按钮

接下来写排序的核心方法,然后把这个方法绑定到三个单选按钮的CheckedChanged事件上:

排序核心方法

private void SortSongList()
{
    // 把ListBox里的所有项转成字符串列表
    var songFiles = listBox1.Items.Cast<string>().ToList();
    
    // 根据选中的单选按钮选择排序规则
    if (radioBtnSongTitle.Checked)
    {
        // 按歌曲名排序,忽略大小写
        songFiles.Sort((fileA, fileB) => 
        {
            var infoA = ParseFileName(fileA);
            var infoB = ParseFileName(fileB);
            return string.Compare(infoA.SongTitle, infoB.SongTitle, StringComparison.OrdinalIgnoreCase);
        });
    }
    else if (radioBtnArtist.Checked)
    {
        // 按歌手排序
        songFiles.Sort((fileA, fileB) => 
        {
            var infoA = ParseFileName(fileA);
            var infoB = ParseFileName(fileB);
            return string.Compare(infoA.Artist, infoB.Artist, StringComparison.OrdinalIgnoreCase);
        });
    }
    else if (radioBtnKey.Checked)
    {
        // 按调式排序
        songFiles.Sort((fileA, fileB) => 
        {
            var infoA = ParseFileName(fileA);
            var infoB = ParseFileName(fileB);
            return string.Compare(infoA.Key, infoB.Key, StringComparison.OrdinalIgnoreCase);
        });
    }
    
    // 清空ListBox,重新加载排序后的内容
    listBox1.Items.Clear();
    listBox1.Items.AddRange(songFiles.ToArray());
}

绑定单选按钮事件

把三个单选按钮的CheckedChanged事件都关联到上面的方法:

private void radioBtnSongTitle_CheckedChanged(object sender, EventArgs e)
{
    // 只有当单选按钮被选中时才触发排序
    if (((RadioButton)sender).Checked)
    {
        SortSongList();
    }
}

// 另外两个单选按钮的事件处理方法和上面完全一样
private void radioBtnArtist_CheckedChanged(object sender, EventArgs e)
{
    if (((RadioButton)sender).Checked)
    {
        SortSongList();
    }
}

private void radioBtnKey_CheckedChanged(object sender, EventArgs e)
{
    if (((RadioButton)sender).Checked)
    {
        SortSongList();
    }
}

3. 额外优化建议

  • 如果你的ListBox里存的是文件的完整路径,ParseFileName方法里的Path.GetFileNameWithoutExtension会自动提取文件名部分,不用额外处理。
  • 用StringComparison.OrdinalIgnoreCase可以让排序忽略大小写,比如"Let It Be"和"let it be"会被视为同一顺序,更符合用户习惯。
  • 如果担心有不符合格式的文件,可以在ParseFileName里加个日志提示,方便排查问题。

内容的提问来源于stack exchange,提问作者DiggDugg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:33:11