Chen Zen
Chen Zen

Reputation: 11

Highlight specific string in listbox

I am trying to highlight current playing song in listbox.
I got the currentItem of playlist.
But it does not get selected because index is always -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);
}

Can anyone help me understand what I'm missing?

Upvotes: 0

Views: 3449

Answers (2)

dshun
dshun

Reputation: 223

The above solution definitely works. It is just a matter if you have added your file names correctly

            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

Upvotes: 0

dshun
dshun

Reputation: 223

How about:

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

Upvotes: 1

Related Questions