简体   繁体   English

Windows应用程序C#字符串组合框

[英]Windows Application C# strings combobox

I'm trying to make a Windows application in Visual Studio. 我正在尝试在Visual Studio中创建一个Windows应用程序。

In the public Form1() , I add some items to my ComboBox with SelectComboBox.Items.Insert(0, "Text"); public Form1() ,我使用SelectComboBox.Items.Insert(0, "Text");向我的ComboBox添加一些项目SelectComboBox.Items.Insert(0, "Text"); and create a string ex. 并创建一个字符串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. 当我在ComboBox中选择了一个项目并单击了一个选项时,我希望Windows Media Player能够播放顶部字符串(例如NR0)中的特定歌曲。

I had tried to create a string in the code for the select button. 我曾尝试在select按钮的代码中创建一个字符串。 string ComboNow = "NR" + SelectComboBox.Items.Count.ToString(); and then changed the URL with Player.URL = @ComboNow; 然后使用Player.URL = @ComboNow;更改URL Player.URL = @ComboNow; .

But then the player think the URL is the name of the string (ex. NR0). 但是玩家认为URL是字符串的名称(例如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;
        }
    }
}

You could use a List or an Array: 您可以使用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];

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: 然后,您可以像这样获取URL:

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. 请注意,这只会起作用,因为您按特定顺序将项目放入组合框中,如果这不是您希望的将来,这将不适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM