Michael Michael -4 years ago 160
C# Question

C# add method groups 1 by 1 to view list then execution list with contains method groups - only separated method no dynamically

For better explanation here is a GUI screenshoot :

SCREEN GUI ( can't add small image only link)
enter image description here

And here is a all of my code with comments :

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 { }

}

// buttton to add music to list view
int i = 0;
private void button_add_music_Click(object sender, EventArgs e)
{

add(i, comboBox1.Text);
i++;

}

// 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 class dataItem
}
}


I want to to select a song from the combobox to add it to the playlist that will play from top to bottom after clicking on the "Start Music" button..

I would like to add a selected song from the combobox to the listview when you click the "Add Music to list" button. Of course, with the "Delete selected music" button I have the ability to delete a song from the list.

So I have to do so for example when I select from the combobox (Music1) click on the button (Add Music to list), it will display ID 1 Music name Music 1 and when click on the Start Music button it will execute void playmusic1 ();

I know I just only add text from combobox to listview but i don't know how can I get method there...

I really don't know how to code Start Music button ( i thinking about queue) to play music one by one with my listView1.

All I need is Collection or something or Queue.

If I dont explain exacly just ask..

Answer Source

You really, really don't want to have a separate method to play each possible sound - that'll be a maintenance nightmare to say the least.

One thing you could do is dynamically load things from resource files. The linked question has an example specifically for sound player.

From the accepted answer, do the following to dynamically retrieve the resource:

object o = Properties.Resources.ResourceManager.GetObject("whateverName");

You then cast it to a stream and play the audio.

You don't necessarily have to use a resource file, though - if you know where the sounds are, you could easily just retrieve a list of file names in the folder(s) in the questions.

Edit: If you really want separate methods, you could always use a Dictionary to map the sound name to the method to run. Something along the lines of this script I put together in LINQpad:

void Main()
{
    var db = new Dictionary<string, Action>();
    db.Add("Music 1", PlaySound1);

    // Retrieve the method we're looking for and execute it
    db["Music 1"]();
}

private void PlaySound1()
{
    // Play the sound
}

You could also do a switch statement:

switch (soundName) {
   case "Music 1":
       // Play music 1
       break;
   case "Music 2":
       // Play music 2
       break;
   // ...
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download