[英]C#: How do you keep selected Items highlighted in a ListBox on a WindowsForm application?
I have a ListBox
where I select some Item
(s) in it which get highlighted. 我有一个
ListBox
,我选择了一些Item
在它(们)得到强调。 Then I press a Button
to move the Item
up. 然后我按一个
Button
将Item
向上移动。 After I click that 'up' Button
and it does what it's supposed to, that same Item
isn't highlighted anymore - which I still want it to be. 当我单击“向上”
Button
并按预期执行操作后,不再突出显示同一Item
-我仍然希望如此。
How come? 怎么会?
I looked up the properties for ListBox
and didn't see anything that would match this situation. 我查找了
ListBox
的属性,但没有找到任何适合此情况的内容。 However, I did see a ListView
property called HideSelection
that seems to be what I'm looking for, but my Control
isn't a ListView
, it's a ListBox
. 但是,我确实看到了一个名为
HideSelection
的ListView
属性,这似乎是我在寻找的东西,但是我的Control
不是ListView
,而是ListBox
。
Basically the question is: 基本上,问题是:
How do I keep those Item
s highlighted after I click the Button
? 单击“
Button
后如何突出显示那些Item
?
I'm a little lost. 我有点迷路了。 Any help would be appreciated.
任何帮助,将不胜感激。
If I didn't understand wrong you are looking for SetSelected()
method. 如果我听错了,您正在寻找
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
}
}
Result; 结果;
Hope helps, 希望有帮助,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.