突出显示列表框中的特定字符串

时间:2015-06-21 20:01:36

标签: c# winforms

我想在列表框中突出显示当前播放的歌曲 我得到了播放列表的currentItem 但它没有被选中,因为索引总是-1。

public void _open_Click_1(object sender, EventArgs e)
{
    _playList.Items.Clear();
    string[] filenames = { };
    _openFile.Multiselect = true;
    _openFile.ShowDialog();
    var l1 = _playa.playlistCollection.newPlaylist("PlayList");
    foreach (var name in _openFile.FileNames)
    {
        _playList.Items.AddRange(_openFile.FileNames.ToArray());

    }

    _playListJob();
    string curItem = _playa.Ctlcontrols.currentItem.getItemInfo("Name");
    int index1 = _playList.FindString(curItem);
    if (index1 != -1)
        _playList.SetSelected(index1, true);
}

任何人都可以帮我理解我错过的东西吗?

2 个答案:

答案 0 :(得分:1)

怎么样:

    index1=_playList.Items.IndexOf(curItem);
    if(index1 >= 0)
    {
    _playList.SetSelected(index1, true);
    }

答案 1 :(得分:0)

上述解决方案绝对有效。如果您正确添加了文件名,那就是一个问题

            public Form1()
    {
        InitializeComponent();
        AddItems();
    }

    public void AddItems()
    {
        listBox1.Items.Clear();
        string[] filenames = new[] { "music a", "music b" };

        listBox1.Items.AddRange(filenames);
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string curItem = "music a";
        int index1 = listBox1.Items.IndexOf(curItem);
        if (index1 != -1)
        {
            MessageBox.Show(curItem);
        }

    }

enter image description here