如何将ListView控件选中的项向上移动或向下移动

yx_luko 2008-07-08 06:32:44
如何将ListView控件选中的项向上移动或向下移动
...全文
277 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jinglecat 2008-07-09
  • 打赏
  • 举报
回复

// eg.
Rearranger.MoveUp(listView1, listViewItem);

public static class Rearranger
{
public static int MoveDown(IList list, object item)
{
return MoveDown(list, item, 1);
}

public static int MoveDown(IList list, object item, int offset)
{
CheckOffset(offset);
return Move(list, item, offset * -1);
}

public static int MoveUp(IList list, object item, int offset)
{
CheckOffset(offset);
return Move(list, item, offset);
}

public static int MoveUp(IList list, object item)
{
return MoveUp(list, item, 1);
}

public static int MoveDown(IList list, int index)
{
return MoveDown(list, index, 1);
}

public static int MoveDown(IList list, int index, int offset)
{
CheckOffset(offset);
return Move(list, index, offset * -1);
}

public static int MoveUp(IList list, int index, int offset)
{
CheckOffset(offset);
return Move(list, index, offset);
}

public static int MoveUp(IList list, int index)
{
return MoveUp(list, index, 1);
}

public static int Move(IList list, int index, int offset)
{
object item = list[index];
return MoveIternal(list, item, index, offset);
}

public static int Move(IList list, object item, int offset)
{
int index = list.IndexOf(item);
return MoveIternal(list, item, index, offset);
}

public static int MoveIternal(IList list, object item, int index, int offset)
{
int newIndex = index - offset;
if (newIndex < 0)
{
newIndex = 0;
}
else if (newIndex > list.Count - 1)
{
newIndex = list.Count - 1;
}
//
list.RemoveAt(index);
list.Insert(newIndex, item);
return (newIndex - index);
}

private static void CheckOffset(int offset)
{
if (offset < 0)
{
throw new ArgumentOutOfRangeException("offset", offset, "The offset must be larger than -1.");
}
}
}

pp_shy 2008-07-08
  • 打赏
  • 举报
回复

ListViewItem lvSelectItem = this.listView1.SelectedItems[0];//获取选中项
ListViewItem lvDownItem = this.listView1.Items[this.listView1.SelectedItems[0].Index + 1];//获取选中项的下一项
ListViewItem lvUpItem = this.listView1.Items[this.listView1.SelectedItems[0].Index - 1];//获取选中项的上一项
amandag 2008-07-08
  • 打赏
  • 举报
回复
将两项的内容对调即可
cjnet 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pp_shy 的回复:]
获取选中项的索引和值
向上移动操作:和选中索引项的前一项的值调换
向下移动操作:和选中索引项的下一项的值调换。
[/Quote]
pp_shy 2008-07-08
  • 打赏
  • 举报
回复
获取选中项的索引和值
向上移动操作:和选中索引项的前一项的值调换
向下移动操作:和选中索引项的下一项的值调换。
net0003 2008-07-08
  • 打赏
  • 举报
回复
行.selscted=true;

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧