这是我将代码在列表中的一个位置下移的代码:
if (this.folderImages.SelectedIndex > -1 && this.folderImages.SelectedIndex < this.folderImages.Items.Count - 1)
{
string imageName = this.folderImages.SelectedItem as string;
int index = this.folderImages.SelectedIndex;
this.folderImages.Items.RemoveAt(index);
this.folderImages.Items.Insert(index + 1, imageName);
this.folderImages.SelectedIndex = index + 1;
}
并将其向上移动一个位置:
if (this.folderImages.SelectedIndex > 0)
{
string imageName = this.folderImages.SelectedItem as string;
int index = this.folderImages.SelectedIndex;
this.folderImages.Items.RemoveAt(index);
this.folderImages.Items.Insert(index - 1, imageName);
this.folderImages.SelectedIndex = index - 1;
}
folderImages
ListBox
当然是a ,所以列表是a ListBox.ObjectCollection
,而不是a List<T>
,但它确实是从继承的,IList
因此其行为应相同。这有帮助吗?
当然,前者仅在所选项目不是列表中的最后一项时才起作用,而后者在所选项目不是第一项时才起作用。