Reputation: 55
I'm trying to make a Windows application in Visual Studio.
In the public Form1()
, I add some items to my ComboBox with SelectComboBox.Items.Insert(0, "Text");
and create a string ex. string NR0 = "__";
with a special song.
When I have selected an item in the ComboBox, and clicked on a select, I want the Windows Media Player to play the specific song in the string (ex. NR0) in the top.
I had tried to create a string in the code for the select button. string ComboNow = "NR" + SelectComboBox.Items.Count.ToString();
and then changed the URL with Player.URL = @ComboNow;
.
But then the player think the URL is the name of the string (ex. NR0).
Do you have any idea to solve this problem.
Thank you
The code is like below:
namespace Player
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SelectComboBox.Items.Insert(0, "First song");
string NR0 = "URL to song";
SelectComboBox.Items.Insert(1, "Second song");
string NR1 = "URL to song";
}
private void SelectButton_Click(object sender, EventArgs e, string[] value)
{
string ComboNow = "NR" + SelectComboBox.Items.Count.ToString();
Player.URL = @ComboNow;
}
}
}
Upvotes: 3
Views: 1915
Reputation: 9729
You could use a List or an Array:
private List<string> songs = new List<string>();
//...
SelectComboBox.Items.Insert(0, "First song");
songs.Add("URL to song");
//...
Player.URL = songs[SelectComboBox.SelectedIndex];
Upvotes: 1
Reputation: 25056
Since you are explicitly putting these items into certain locations, I would do something like create a dictionary:
private Dictionary<int, string> Songs
{
get
{
return new Dictionary<int, string>()
{
{ 0, "url of first song" },
{ 1, "url of second song" }
};
}
}
You can then just get the URL like so:
string playerURL = Songs[comboBox1.SelectedIndex];
Note this will only work because you are putting the items into the combo box in a particular order, if this is not what you want in the future, this won't be for you.
Upvotes: 0