C#:如何让选定的项目在WindowsForm应用程序的列表框中突出显示?

问题描述:

我有一个ListBox我在那里选择一些Item(s)在它被突出显示。然后我按01​​将Item向上移动。当我点击'上'Button,它做它应该的,同样的Item不再突出显示 - 我仍然希望它是。
怎么回事?C#:如何让选定的项目在WindowsForm应用程序的列表框中突出显示?

我抬头看看ListBox的属性,并没有看到任何符合这种情况的东西。不过,我确实看到ListView属性HideSelection这似乎是我正在寻找,但我的Control不是ListView,这是一个ListBox

基本上问题是:
我点击Button后,如何保持那些Item为高亮显示?
我有点迷路。任何帮助,将不胜感激。

+0

然后我按一个按钮来移动该项目达.---怎么样?这不是内置的行为 –

+0

创建了一个按钮,并在'上'按钮上做了一个点击事件 – NBY

+0

把这个代码放在那里,因为这是关键。你可能需要添加'Focus()' –

如果我不明白你错了,你正在寻找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 
      } 
     } 

结果;

enter image description here

希望帮助,

+0

这看起来像我想要做的。谢谢! – NBY

+0

很高兴听到@Nick – Berkay

+0

如果有效,请不要忘记注册并标记解决方案@Nick – Berkay