2012-11-14 58 views
3

我想在Visual Studio中创建一个Windows应用程序。Windows应用程序C#字符串combobox

public Form1()中,我用SelectComboBox.Items.Insert(0, "Text");向我的ComboBox添加了一些项目,并创建了一个字符串ex。 string NR0 = "__";带一首特别的歌。

当我在ComboBox中选择了一个项目并单击了某个选择后,我想让Windows Media Player播放顶部字符串(例如NR0)中的特定歌曲。

我曾尝试在选择按钮的代码中创建一个字符串。 string ComboNow = "NR" + SelectComboBox.Items.Count.ToString();,然后用Player.URL = @ComboNow;更改了URL。

但是,玩家认为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; 
     } 
    } 
} 
+0

不要找到更多的方式来做到这一点,我选择了我的路。谢谢大家,他们曾用时间来帮助我。我是丹麦的一名青少年,所以我很抱歉,如果我在语法上遇到一些问题。 – akhegr

回答

1

你可以使用一个列表或数组:

private List<string> songs = new List<string>(); 
//... 
SelectComboBox.Items.Insert(0, "First song"); 
songs.Add("URL to song"); 
//... 
Player.URL = songs[SelectComboBox.SelectedIndex]; 
+0

非常好,下面的例子更简单。 – akhegr

0

既然你明确地把这些项目到指定的位置,我会做一些像创建词典:

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]; 

请注意,这只会工作,因为您将项目按特定顺序放入组合框,如果这不是您将来想要的,这不适合您。

+0

@Downvoter,请解释一下。如果没有被告知有什么问题,就不能学习! – Arran

+0

非常感谢,从开始我有一个很长的清单,如果有其他检查号码,请再次检查一个新号码。 – akhegr

相关问题