C#数组到listBox ...需要帮助 [英] C# Array into listBox ... help needed

查看:94
本文介绍了C#数组到listBox ...需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在练习编程(我是新手),并且正在制作一个简单的音频播放器.

形式如下:

有2个列表框,其中1个用于将音乐导入到下一个列表框,即音乐列表框.
您可以使用按钮在打开的文件对话框中查找歌曲文件,然后将文件名放入listBox1.从那里可以突出显示它,然后使用另一个按钮将其发送到listBox2.

但是,....打开文件对话框中的文件名类似于C:\ Users \ Joel \ Music \ song.wav,因此我使用代码将所有歌曲缩短为例如song.wav.
但这会引起问题.我希望可见的歌曲名称为"Song.wav",但仍然具有某种背景信息,因此,当您单击播放"按钮时,它将获取信息(在SoundPlayer中播放它的文件位置)而不是名称.

如何为输入到listbox1的每个项目提供一些背景信息,这些信息可以随其携带到listbox2等中.
我已经想了好几个小时,但我无法解决,也许是阵列吗?将所有歌曲存储在列表中?但是我不知道如何工作:s

我真的真的很需要帮助.如果我不清楚任何事情,我会尝试改写
谢谢!

Okay I am practicing programming (I''m new) and I''m making a simple audio player.

The form is like this:

There is 2 ListBoxes, 1 is for Importing Music to the next ListBox, which is the Music listbox.
You use a button to find a song file in an open file dialog which then puts the file name into the listBox1. From there you can highlight it and send it to listBox2 with another button.

However.... The filename from the open file dialog is like C:\Users\Joel\Music\song.wav so I had used code to shorten all the songs down to just song.wav for example.
But this causes problems. I want the visible song name to be "Song.wav" but still have some sort of background information so that when you click the Play button, it takes information (the file location to play it in SoundPlayer) instead of the name.

How can I give each item entered to the listbox1 some background information which it can carry with it around to listbox2 and etc.
I have been thinking for hours but I can''t work it out, Maybe an array? to store all the songs in the list? But I don''t have a clue how to work it :s

I really really really need help. I will try rephrase if I was unclear about anything
Thanks!

推荐答案

这可能是一种可能的解决方案.创建一个名为MusicFile的类,该类具有FileNamePath属性.
This can be one possible solution. Create a class called MusicFile which has FileName and Path properties.
public class Song{
   public string Path {get;set;}
   public string SongName {get;set;}
}


然后通过加载包含分配了Path和SongName(已删除文件路径)的音乐文件的文件夹来创建音乐列表.


Then create the music list by loading the folder which contains the music files assigning the Path and SongName(removed file path)

 // Assuming that you are using Window Form
 protected void Form_Load(object sender, EventArgs e) {
   bindList();
 } 
 private void bindList{
   List<Song> songs = new List<song>();
   // for each files in folder 
      songs.Add(new Song() { Path="C:\\Users\\Joel\\Music\\song.wav", SongName="song" } );
   // Finally
   listBox1.DataSource = songs;
   listBox1.DisplayMember = "SongName";
   listBox1.ValueMember = "Path";
}

private void listBox1_SelectedIndexChanged(Object sender, System.EventArgs e)
{
    // Get the currently selected item in the ListBox.
    string song = listBox1.SelectedValue.ToString();
    // Do play or do something.
} 


这篇关于C#数组到listBox ...需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆