C#:如何让选定的项目在WindowsForm应用程序的列表框中突出显示?
问题描述:
我有一个ListBox
我在那里选择一些Item
(s)在它被突出显示。然后我按01将Item
向上移动。当我点击'上'Button
,它做它应该的,同样的Item
不再突出显示 - 我仍然希望它是。
怎么回事?C#:如何让选定的项目在WindowsForm应用程序的列表框中突出显示?
我抬头看看ListBox
的属性,并没有看到任何符合这种情况的东西。不过,我确实看到ListView
属性HideSelection
这似乎是我正在寻找,但我的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
}
}
结果;
希望帮助,
然后我按一个按钮来移动该项目达.---怎么样?这不是内置的行为 –
创建了一个按钮,并在'上'按钮上做了一个点击事件 – NBY
把这个代码放在那里,因为这是关键。你可能需要添加'Focus()' –