2015-06-21 89 views
0

我想突出顯示當前在列表框中播放的歌曲。
我收到播放列表的當前項目。
但它沒有被選中,因爲索引總是-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); 
} 

任何人都可以幫助我瞭解我錯過了什麼嗎?

+0

確定'curItem'具有正確的價值? – venerik

+0

歡迎來到Stack Overflow。請在句子開頭使用大寫字母,並在句尾加上句號。發佈前請檢查預覽。如果在換行符之前輸入兩個空格或在其之後輸入空行,編輯器中的換行符只會導致顯示文本中出現換行符。 –

回答

0

如何:

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

我以前試過,索引總是-1因爲某種原因,任何想法爲什麼? –

+0

然後您需要檢查播放列表中的項目,確保項目已正確添加。 curItem的價值是什麼。設置一個斷點,檢查它現在突出顯示的值 – dshun

+0

,但是我必須在indexOf部分之後添加index1 = 0。一個問題,雖然當mediaplayer改變歌曲它不標記當前名稱 –

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

+0

以及不工作,基本上與第一個答案相同我的問題是,我試圖讓它如此當一首歌曲發揮並行名稱顯示在列表框中將突出顯示 –