简体   繁体   English

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

[英]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. 然后我按一个ButtonItem向上移动。 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 . 但是,我确实看到了一个名为HideSelectionListView属性,这似乎是我在寻找的东西,但是我的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.

相关问题 在C#Winforms的列表框中搜索时,如何保持所选项目突出显示? - How to keep selected item highlighted while searching in listbox in c# winforms? Windows 8 C# - 如何保留ListBox项? - Windows 8 C# - how to keep ListBox items? 如何在C#Windows应用程序中将选中的列表框的选定项添加到datagridview中的特定列? - how can i get the selected items of a checked listbox to a particular column in datagridview in c# windows application? 当用户单击 c# windowsform 应用程序中的第一行时,如何获取第一个 ID - how do i get the first ID when user click on the first row in c# windowsform application 如何使用Winforms C#获取用户在列表框中选择项目的顺序 - how do I get the order in which user selected Items in Listbox using Winforms c# 如何在WPF C#列表框中附加选定的项目? - How to append selected items in WPF c# listbox? C#如何保存包含多个对象的设置并将其绑定到列表框中的项目 - C# How do you save settings containing multiple objects and bind them to items in a listbox 如何检查C#中未选择ListBox的选定值? - How do I check if selected value of the ListBox is not selected in C#? C# 检查列表框中的选定项是否在一个序列中 - C# check if selected items in a listbox are in a sequence 在C#中的列表框中获取所选项目的索引 - Getting the indexes of selected items in a listbox in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM