簡體   English   中英

如何根據另一個listBox的索引填充listBox

[英]How to Populate a listBox depending on the index of another listBox

好。 因此,我創建了一個字典,將演員(在13,559行txt文件中)分配給他們的電影(在相同的txt文件中)。 例如,演員1被分配給電影“ a”,“ b”和“ c”,演員2被分配給電影“ d”和“ e”。 在我的字典中,鍵是字符串值(演員),電影存儲在列表中並分配給鍵。

我已經附上了我正在使用的GUI,但其要旨是,當用戶在listBox1中選擇Actor A時,我希望Actor A的電影出現在listBox2中。 當然,如果選擇了演員B,我希望他的電影顯示在列表框2中,並且希望刪除演員A的電影。 換句話說,我希望listBox2在滾動listBox1時不斷更新。

如果您有任何建議,請分享! 謝謝!

// dictionary already full of actors and movies
Dictionary<string, List<string>> myDic = new Dictionary<string, List<string>>();

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // as index of listBox1 changes, what should I do in this method?
}

在此處輸入圖片說明

嘗試這樣的事情:

using System.Linq

...

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // filter movies according to selected value in first listbox
    var movies = myDic.Where(x => x.Key == listBox1.SelectedItem.ToString()).SelectMany(x => x.Value).ToList();
    listBox2.Items.Clear();
    foreach (string movie in movies)
    {
        listBox2.Items.Add(movie);
    }
}

根據您填充listBox1 ,您可能需要使用listBox1.SelectedValue而不是listBox1.SelectedItem.ToString()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM