为了更好地解释,这里是一个GUI屏幕截图:
(无法添加小图片链接) Screen GIU <<
这是我的所有代码和评论:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media; // to play sounds
namespace Music_PLayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//List View
listView1.View = View.Details;
listView1.FullRowSelect = true;
// Columns CONSTRUCT
listView1.Columns.Add("ID", 70);
listView1.Columns.Add("Music name", 150);
//combobox items
comboBox1.Items.Add("Music 1");
comboBox1.Items.Add("Music 2");
comboBox1.Items.Add("Music 3");
comboBox1.Items.Add("Music 4");
comboBox1.Items.Add("Music 5");
}
// ADD TO List VIEW
private void add(int ID, string name_music)
{
string[] row = { Convert.ToString(ID), name_music };
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);
}
//button to delete selected items in list view
private void button_delete_selected_music_Click(object sender, EventArgs e)
{
try
{
listView1.SelectedItems[0].Remove();
}
catch { }
}
**//EDITED :**
// buttton to add music to list view
int i = 0;
private void button_add_music_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex==0)
{
dictOfDelegate.Add("Music 1", playmusic1);
add(i, comboBox1.Text);
}
if (comboBox1.SelectedIndex == 1)
{
dictOfDelegate.Add("Music 2", playmusic2);
add(i, comboBox1.Text);
}
if (comboBox1.SelectedIndex == 2)
{
dictOfDelegate.Add("Music 3", playmusic3);
add(i, comboBox1.Text);
}
if (comboBox1.SelectedIndex == 3)
{
dictOfDelegate.Add("Music 4", playmusic4);
add(i, comboBox1.Text);
}
if (comboBox1.SelectedIndex == 4)
{
dictOfDelegate.Add("Music 5", playmusic5);
add(i, comboBox1.Text);
}
}
// button to start music
private void button_start_music(object sender, EventArgs e)
{
}
// Voids with music :
public void playmusic1()
{
SoundPlayer audio = new SoundPlayer(Music_PLayer.Properties.Resources.playsound1);
audio.Play();
}
public void playmusic2()
{
SoundPlayer audio = new SoundPlayer(Music_PLayer.Properties.Resources.playsound2);
audio.Play();
}
public void playmusic3()
{
SoundPlayer audio = new SoundPlayer(Music_PLayer.Properties.Resources.playsound3);
audio.Play();
}
public void playmusic4()
{
SoundPlayer audio = new SoundPlayer(Music_PLayer.Properties.Resources.playsound4);
audio.Play();
}
public void playmusic5()
{
SoundPlayer audio = new SoundPlayer(Music_PLayer.Properties.Resources.playsound5);
audio.Play();
}
public delegate void PlayMusic();
// EDITED
Dictionary<string, PlayMusic> dictOfDelegate = new Dictionary<string, PlayMusic>();
void CreateList()
{
dictOfDelegate.Add("Music 1", playmusic1);
dictOfDelegate.Add("Music 2", playmusic2);
dictOfDelegate.Add("Music 3", playmusic3);
dictOfDelegate.Add("Music 4", playmusic4);
dictOfDelegate.Add("Music 5", playmusic5);
}
void InvokeMethod(string item)
{
PlayMusic method = dictOfDelegate.First(x => x.Key == item).Value;
method.Invoke();
}
}
}
我想从组合框中选择一首歌曲,将其添加到播放列表中,点击&#34;开始播放音乐&#34;播放列表将从上到下播放。按钮..
当您点击&#34;添加音乐到列表&#34;时,我想将组合框中的选定歌曲添加到列表视图中。按钮。当然,使用&#34;删除所选音乐&#34;按钮我可以从列表中删除歌曲。
所以我必须这样做,例如当我从组合框(Music1)中选择单击按钮(添加音乐到列表)时,它将显示ID 1音乐名称音乐1,当点击开始音乐按钮时它将执行void playmusic1();
我知道我只是将组合框中的文字添加到listview,但我不知道如何在那里获得方法...
我真的不知道如何编写“开始音乐”按钮(我考虑队列)以使用我的listView1逐个播放音乐。
我需要的只是收藏品或其他东西或队列。
主要问题是如何创建一个包含方法gorups的列表,然后执行所有这个列表(如您所见,我希望能够删除此列表中的所选元素)
如果我不解释,请问..
修改...
我认为最好的解决方案是这段代码:
public List<Action> musiclist = new List<Action>();
// buttton to add music to list view
int i = 0;
private void button_add_music_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex==0)
{
musiclist.Add(playmusic1);
add(i, comboBox1.Text);
}
if (comboBox1.SelectedIndex == 1)
{
musiclist.Add(playmusic2);
add(i, comboBox1.Text);
}
if (comboBox1.SelectedIndex == 2)
{
musiclist.Add(playmusic3);
add(i, comboBox1.Text);
}
if (comboBox1.SelectedIndex == 3)
{
musiclist.Add(playmusic4);
add(i, comboBox1.Text);
}
if (comboBox1.SelectedIndex == 4)
{
musiclist.Add(playmusic5);
add(i, comboBox1.Text);
}
}
private void button_to_Start_music_Click(object sender, EventArgs e)
{
foreach (Action func in musiclist)
{
func();
}
}
答案 0 :(得分:0)
在我看来,这不是最好的方法,无论如何你可以在字典中使用委托。
public delegate void PlayMusic();
Dictionary<string, PlayMusic> dictOfDelegate = new Dictionary<string, PlayMusic>();
void CreateList()
{
dictOfDelegate.Add("Music 1", playmusic1);
dictOfDelegate.Add("Music 2", playmusic2);
...
}
void InvokeMethod(string item)
{
PlayMusic method = dictOfDelegate.First(x => x.Key == item).Value;
method.Invoke();
}
答案 1 :(得分:0)
您可以创建一个类歌曲和一个可以随时添加或删除内容的列表。
int queueSize = 0;
int queueCurrent = 0;
List<Song> toPlay = new List<Song>();
class Manager
{
public void addSong(Song add)
{
toPlay.add(add);
queueSize++;
}
public void removeSong(int remove)
{
toPlay.RemoveAt(remove)
queueSize--;
}
public void playSong(int index)
{
Song play = toPlay[index]
WMPLib.WindowsMediaPlayer songPlayer = new WMPLib.WindowsMediaPlayer();
songPlayer.URL = play.FilePath;
wplayer.Controls.Play();
}
}
class Song
{
string FilePath;
string Name;
int Index;
public Song(string name, int index)
{
Name = name;
Index = index;
}
}
当然这需要更多的代码,但这应该可以解决您的问题。如果您有任何问题或看到任何修改,请在评论中告诉我。
编辑:OP正在寻找每种歌曲的硬编码方式,所以就在这里。此外,我不建议其他任何人寻找相同的答案。
class Songs
{
public void songOne()
{
string name = "songOne";
string path = "C:/songs/songone/
playSong(path);
}
public void songOne()
{
string name = "songTwo";
string path = "C:/songs/songtwo/
playSong(path);
}
// And all the other songs
public void playSong(string path)
{
WMPLib.WindowsMediaPlayer songPlayer = new WMPLib.WindowsMediaPlayer();
songPlayer.URL = path;
wplayer.Controls.Play();
}
}