Jump to content

Removing Items In A ListView.

- - - - -

  • Please log in to reply
2 replies to this topic

#1
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Hi everyone! So I have a listView on my Form named "officeView" I already have the code to add and update info into it, but Im having troubles deleting items out of it. :/

Now I know how to delete an Item from the listView, but I want the item before the deleted item to become automatically selected.
Eg:
item1
item2 
item3 <-- delete this one, and "item2" item becomes selected, ect..

Here's what I have so far:
if (officeView.Items.Count != 0  && officeView.Items.Count > 1)
            {
                int index = officeView.FocusedItem.Index;
                officeView.Items.Remove(officeView.Items[index]);
                officeView.Items[index-1].Selected = true;              
            }
            else if (officeView.Items.Count == 1) 
            {
                int index = officeView.FocusedItem.Index;
                officeView.Items.Remove(officeView.Items[index]);
                officeView.Items[0].Selected = true; 
            }

When i run it, and try to delete things it works fine the first time but then goes bad.
Eg I have this:
item1
item2
item3
item4
item5
I select "item3" press delete, and it deletes it. Now item2 is automatically selected. I then press delete again, and item4(which is now in item3s position) is deleted. See the problem? Im really not sure why its doing that, maybe Im missing something simple.

Any help is appreciated ~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#2
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
I think instead of making the previous item as selected you have to move the focus to it. If you set the ListView to be able to "multiselect", marking an item as selected is only "highlight" it, not immediately giving it focus.

#3
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Just added this line before selecting the new item.
officeView.Items[index-1].Focused = true;
And it works perfect. +rep :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users