I have a ListBox
where I select some Item
(s) in it which get highlighted. Then I press a Button
to move the Item
up. 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.
How come?
I looked up the properties for ListBox
and didn't see anything that would match this situation. 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
.
Basically the question is:
How do I keep those Item
s highlighted after I click the Button
?
I'm a little lost. Any help would be appreciated.
If I didn't understand wrong you are looking for SetSelected()
method.
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,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.