Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a little problem ,

i have many test to select next item in listview with test_click and nothing works ...


C#
int next = Convert.ToInt32(ctrListviewHalter.FocusedItem.Index) + 1;
                    ctrListviewTest.Items[next].Selected = true;
                    ctrListviewTest.EnsureVisible(next);


what goes wrong
Posted

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;


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
 
Share this answer
 
I have found a other way, for the little problem :-)

C#
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900