簡體   English   中英

C#:如何在WindowsForm應用程序的列表框中保持所選項目的突出顯示?

[英]C#: How do you keep selected Items highlighted in a ListBox on a WindowsForm application?

我有一個ListBox ,我選擇了一些Item在它(們)得到強調。 然后我按一個ButtonItem向上移動。 當我單擊“向上” Button並按預期執行操作后,不再突出顯示同一Item -我仍然希望如此。
怎么會?

我查找了ListBox的屬性,但沒有找到任何適合此情況的內容。 但是,我確實看到了一個名為HideSelectionListView屬性,這似乎是我在尋找的東西,但是我的Control不是ListView ,而是ListBox

基本上,問題是:
單擊“ Button后如何突出顯示那些Item
我有點迷路了。 任何幫助,將不勝感激。

如果我聽錯了,您正在尋找SetSelected()方法。

private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > 0)
            {
                int selectedIndex = listBox1.SelectedIndex;
                object selectedItem = listBox1.SelectedItem;
                listBox1.Items.Remove(selectedItem);
                listBox1.Items.Insert(selectedIndex - 1, selectedItem);
                listBox1.SetSelected(selectedIndex -1, true); // here we go
            }
        }

結果;

在此處輸入圖片說明

希望有幫助,

暫無
暫無

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

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