简体   繁体   English

在现有列表框和数组中添加新项目

[英]add new item in existing listbox and array

I have this code for listbox that acts as a playlist. 我具有用作播放列表的列表框的代码。 I would like to give the option to add a new song after the first ones are loaded via a new button. 我想提供在通过新按钮加载第一首歌曲后添加新歌曲的选项。 How can I do it? 我该怎么做?

 OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() ==  System.Windows.Forms.DialogResult.OK)
            {
                files = openFileDialog1.SafeFileNames; //save only the names
                paths = openFileDialog1.FileNames; // save the paths
                for (int i = 0; i < files.Length; i++)
                {
                    listBox1.Items.Add(files[i]); // add songs to listbox


                }
             }

If you want to use audio files after close and open the app, you have to handle the filepaths too. 如果要在关闭并打开应用程序后使用音频文件,则也必须处理文件路径。 Create a filelist file and at Load event open it. 创建一个文件列表文件,并在Load事件中将其打开。 The code below will add the files to the playlist. 以下代码会将文件添加到播放列表中。

        OpenFileDialog OpenFile = new OpenFileDialog();
        OpenFile.Filter = "Audio files (*.mp3)|*.mp3";
        OpenFile.Multiselect = true;

        System.IO.StreamWriter settingsFile = new System.IO.StreamWriter((Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) + "playlist.ini");

        if (OpenFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            foreach (string filename in OpenFile.FileNames)
            {                 
                listBoxList.Items.Add(filename);
                settingsFile.WriteLine(filename);
            }
        }

        settingsFile.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM