如何在button_click上选择Listview中的下一个项目 [英] How do Select next Item in Listview on button_click

查看:66
本文介绍了如何在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

推荐答案

你好Belial_X,

你已经添加了一行额外的代码来获得所需的输出。实际上以编程方式选择项目不会自动将焦点更改为ListView控件。因此,您通常需要在选择项目时调用Focus方法。

Hello Belial_X,
you have add one extra line of code to get the required out put . actually selecting an item programmatically does not automatically change the focus to the ListView control. For this reason, you will typically want to call the Focus method when selecting an item.
int next = Convert.ToInt32(listView1.FocusedItem.Index) + 1;
//add this line            
 this.listView1.Focus();
 this.listView1.Items[next].Selected = true;





查看参考:

在Windows窗体ListView控件中选择一个项目



不要忘记可能有 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


我找到了另一种方法,对于这个小问题:-)



I have found a other way, for the little problem :-)

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中的下一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆