首页 > 解决方案 > 使用列表而不是数组的问题

问题描述

在这里,我正在尝试做一个音乐媒体播放器。我做得很好,感谢一个普通的数组。但是,当我尝试使用无法做到的列表时。

stringName.Add(openFileDialog1.SafeFileNames);

第一个问题是在这一行中,我无法将字符串数组转换为字符串。所以我可以在没有它的情况下使用另一种方法,但它只给我列表中的一个声音,不可能得到更多。我需要加载更多歌曲。

这是我的代码:

private void button1_Click(object sender, EventArgs e)
        {
           
            if (openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                //--------------------Thanks to array work perfectly
                //arraylist1 = openFileDialog1.SafeFileNames;
                //a2 = openFileDialog1.FileNames;

                //for (int i = 0; i < arraylist1.Length; i++)
                //{
                //    listBox1.Items.Add(arraylist1[i]);
                //}

                
                //----------------------- with lists it doesn't work-------------------------
                stringName.Add(openFileDialog1.SafeFileName);
                
                MessageBox.Show(stringName.Count.ToString());

                for (int i = 0; i < stringName.Count; i++)
                {
                    listBox1.Items.Add(stringName[i]);
                }
               
            }

        }

这里的目标是把我的名字歌曲列表而不是我的列表,并在我的 listbox1 中显示这个列表。你有什么主意吗 ?我是 C# 新手,我想改进我的代码以便更好地理解和升级。非常感谢您的帮助。或者也许它是唯一的解决方案?

标签: c#listlistboxopenfiledialog

解决方案


推荐阅读