C# listview 的自动选中某行的问题

studykl 2010-09-27 01:57:04
C# 中如何做到当选中listview某行删除后,自动选中它的上面的一行,如果删除的是第一行,就选中它的下一行,希望能够得到帮助!
...全文
828 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaofei88168 2012-05-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
C# code
//先检查是否选中过
if (ListView1.SelectedIndices.Count==0) return;
//记住之前选择
int last = ListView1.SelectedIndices[0];
//删除选中项
ListView1.Items.RemoveAt(last);
last = Math.Min(last, ListView1.Item……
[/Quote]


不错,有效果,谢谢
studykl 2010-09-27
  • 打赏
  • 举报
回复
谢谢各位的帮助
liuqilin1987 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyazhe 的回复:]
C# code
//先检查是否选中过
if (ListView1.SelectedIndices.Count==0) return;
//记住之前选择
int last = ListView1.SelectedIndices[0];
//删除选中项
ListView1.Items.RemoveAt(last);
last = Math.Min(last, ListView1.Item……
[/Quote]
支持一个!
明哥说编程 2010-09-27
  • 打赏
  • 举报
回复
先设置listview 必须为单选 (MultiSelect 多选属性),

然后在进行选中行删除以后选中其它行操作,

if (ListView1.SelectedIndices.Count==0) return;

int index = ListView1.SelectedIndices[0];
//删除选中项
ListView1.Items.RemoveAt(index);

if(ListView1.Items.Count==index)
{
ListView1.Items[index-1].Selected = true;//选中
ListView1.Items[index-1].Focused = true; //焦点
ListView1.Items[index-1].EnsureVisible();//滚动显示
}
else if(ListView1.Items.Count>index)
{
ListView1.Items[index].Selected = true;//选中
ListView1.Items[index].Focused = true; //焦点
ListView1.Items[index].EnsureVisible();//滚动显示
}




xuyufeng822 2010-09-27
  • 打赏
  • 举报
回复
楼上的方法可以
YellowManDog 2010-09-27
  • 打赏
  • 举报
回复
精辟,楼上的大哥
兔子-顾问 2010-09-27
  • 打赏
  • 举报
回复
//先检查是否选中过
if (ListView1.SelectedIndices.Count==0) return;
//记住之前选择
int last = ListView1.SelectedIndices[0];
//删除选中项
ListView1.Items.RemoveAt(last);
last = Math.Min(last, ListView1.Items.Count - 1);//如果是最后一条,则减少1个
//查看是否在有效范围
if (ListView1.Items.Count > last)//确认last有效
{
ListView1.Items[last].Selected = true;//选中
ListView1.Items[last].Focused = true; //焦点
ListView1.Items[last].EnsureVisible();//滚动显示
}
uuu456927 2010-09-27
  • 打赏
  • 举报
回复
百度上找下。

111,129

社区成员

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

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

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