本文介绍了如何在button_click上选择Listview中的下一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一点问题,
i有很多测试用list_click来选择listview中的下一个项目并且没有任何效果...
i have a little problem ,
i have many test to select next item in listview with test_click and nothing works ...
int next = Convert.ToInt32(ctrListviewHalter.FocusedItem.Index) + 1;
ctrListviewTest.Items[next].Selected = true;
ctrListviewTest.EnsureVisible(next);
出了什么问题
what goes wrong
推荐答案
int next = Convert.ToInt32(listView1.FocusedItem.Index) + 1;
//add this line
this.listView1.Focus();
this.listView1.Items[next].Selected = true;
查看参考:
不要忘记可能有 ArgumentOutOfRangeException
错误来自直接添加索引1。所以,尝试按照要求修改代码。
谢谢
Check the reference :
Select an Item in the Windows Forms ListView Control
Don't forget that there may be ArgumentOutOfRangeException
error comes as you directly add the index by 1 . so, try to modify the code as per requirement .
thanks
int next_a, next_b;
private void ctrOneForwart_Click(object sender, EventArgs e)
{
try
{
if (ctrListview1.Items.Count > 0)
{
next_a = Convert.ToInt32(ctrListview1.FocusedItem.Index);
if (next_a != -1)
{
next_a = -1;
}
next_a = Convert.ToInt32(ctrListview1.FocusedItem.Index);
next_b = next_a - 1;
ctrListview1.Items[next_b].Focused = true;
ctrListview1.Items[next_b].Selected = true;
}
}
}
catch (Exception) { }
//replace Exception with ArgumentOutOfRangeException or NullReferenzExeption , it's better
这篇关于如何在button_click上选择Listview中的下一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!