[英]Windows Application C# strings combobox
我正在嘗試在Visual Studio中創建一個Windows應用程序。
在public Form1()
,我使用SelectComboBox.Items.Insert(0, "Text");
向我的ComboBox添加一些項目SelectComboBox.Items.Insert(0, "Text");
並創建一個字符串ex。 string NR0 = "__";
用一首特別的歌。
當我在ComboBox中選擇了一個項目並單擊了一個選項時,我希望Windows Media Player能夠播放頂部字符串(例如NR0)中的特定歌曲。
我曾嘗試在select按鈕的代碼中創建一個字符串。 string ComboNow = "NR" + SelectComboBox.Items.Count.ToString();
然后使用Player.URL = @ComboNow;
更改URL Player.URL = @ComboNow;
。
但是玩家認為URL是字符串的名稱(例如NR0)。
你有任何想法來解決這個問題嗎?
謝謝
代碼如下:
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;
}
}
}
您可以使用List或Array:
private List<string> songs = new List<string>();
//...
SelectComboBox.Items.Insert(0, "First song");
songs.Add("URL to song");
//...
Player.URL = songs[SelectComboBox.SelectedIndex];
由於您明確地將這些項目放入某些位置,我會做類似創建字典的操作:
private Dictionary<int, string> Songs
{
get
{
return new Dictionary<int, string>()
{
{ 0, "url of first song" },
{ 1, "url of second song" }
};
}
}
然后,您可以像這樣獲取URL:
string playerURL = Songs[comboBox1.SelectedIndex];
請注意,這只會起作用,因為您按特定順序將項目放入組合框中,如果這不是您希望的將來,這將不適合您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.