Page 1 of 2

ItemSelectionChanged Event

Posted: 05 Jun 2010, 10:38
by Mex
Hi

ItemSelectionChanged is rised twice, is this correct?
1) Previous item
2) New item


BR;
Meelis

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 14:49
by TiKu
Yes, that's correct, because the selection state of both items changes.
Maybe you are looking for the CaretChanged event?

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 20:10
by Mex
Ok, the soluton is in ItemSelectionChanged event
If listItem.Selected Then
'Do what is needed
End If

I can't use CaretChangd event, because when i change list item selection in code and the list has no focus then CaretChanged is not rised.

Danke;
Meelis

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 20:35
by TiKu
How do you change the selection in code? Setting the CaretItem property should fire the CaretChanged event.

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 20:43
by Mex
Im using your control on my own user control, its like a wraper for yor control with additonal events, properties and methods.
My control has a ListIndex property what i can change in code.

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 20:56
by TiKu
Well, I assume this ListIndex property is supposed to get/set the caret item, so you should use the CaretItem property. The caret item is the item that has the keyboard focus (if the control has the focus) and usually is selected. Only one item can be the caret item. The ListViewItem.Selected property and the ItemSelectionChanged event have little to do with the caret item. They refer to the item's selection state, i. e. whether it is selected or not. An item is selected if it has a blue background (okay, that's not necessarily correct, but you get the idea). An item that is selected is not necessarily the caret item. In multi-selection mode many items can be selected, but still there's only one caret item.

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 21:03
by Mex
Well, I assume this ListIndex property is supposed to get/set the caret item, so you should use the CaretItem property.
Yes and no. When ListView has no focus then i can't use CaretItem

Let' say you have texbox, button and listview on the form. Listview has 10 items and textbox has a focus.
When youy click button you must change listviews "listindex" but the listview has no focus then ItemSelectionChanged is rised but CaretChanged not.

I'll try to post a sample with my control :)

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 21:11
by TiKu
I just did a test (WIndows 7 x64 with themes):

Code: Select all

Set ExLvw.CaretItem = ExLvw.ListItems(3)
This code causes the CaretChanged event to be raised, even if the list view doesn't have the focus. The only situation in which the event is not raised, is when item 3 already was the caret item.

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 21:18
by Mex
Ok now i understant, we have just different approach :)

In my control, when i set the ListIndex property i use;
Public Property Let ListIndex(ByVal value As Long)
If theList.ListItems.Count > 0 Then
If value > theList.ListItems.Count - 1 Then
value = 0
End If
theList.ListItems(value).Selected = True
Let MyListIndex = value
End If
End Property

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 21:24
by TiKu
What is this useful for? You have a property that can be set to exactly 1 list item. It will select this item. What's with the other selected items? Or are you in single selection mode? If you are in single selection mode, why would you want to select an item without making it the caret item?
Of course the CaretChanged event is not raised, if you change an item's selection state, because changing an item's selection state doesn't change the item's caret state.

After trying your sample, I'm convinced more than before that what you really want to use are the CaretChanged event and the CaretItem property.

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 21:27
by Mex
What is this useful for? You have a property that can be set to exactly 1 list item. It will select this item. What's with the other selected items? Or are you in single selection mode? If you are in single selection mode, why would you want to select an item without making it the caret item?
Of course the CaretChanged event is not raised, if you change an item's selection state, because changing an item's selection state doesn't change the item's caret state.
Yup, im not using MultiSelect.
So better is to use CaretItem?

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 21:29
by TiKu
Yes, see my edit in the previous post.

Re: ItemSelectionChanged Event

Posted: 05 Jun 2010, 21:36
by Mex
ok, thank you for your response and for being so patient with me :)


Meelis

Re: ItemSelectionChanged Event

Posted: 25 Feb 2013, 21:31
by engee30
Hello

Is there a quick way of getting the number of all selected items? I've got over 300.000 items in my Listview and when I want to get the number (in this case in the MouseUp event), it lags a bit before I get it displayed in a Label.

Pete

Re: ItemSelectionChanged Event

Posted: 26 Feb 2013, 00:31
by TiKu
Hi,

try this:

Code: Select all

  Dim col As ListViewItems

  Set col = ExLVwU.ListItems
  col.FilterType(FilteredPropertyConstants.fpSelected) = FilterTypeConstants.ftIncluding
  col.Filter(FilteredPropertyConstants.fpSelected) = Array(True)
  Debug.Print col.Count
Regards
TiKu