ListView control - "Selected" 
Author Message
 ListView control - "Selected"

I'm using a ListView control (3-lvwReport mode) and I have
a command button to "Deselect" everything in the list. The
code for it is:

Private Sub CmdClearLV1_Click()
    For z = 1 To ListViewLV1.ListItems.Count
        ListViewLV1.ListItems(z).Selected = False
    Next z
End Sub

I also have a command button to add selected items from
the ListView control to another list box. Since there is
no reason to "add" items if there is nothing selected, I
do a check at the beginning of the add subroutine. For
some reason, it always seems to show an item selected.

Private Sub CmdAddLV1_Click()
    If ListViewLV1.SelectedItem Is Nothing Then Exit Sub
    If ListViewLV1.ListItems.Count > 1 Then
        ListViewHidden.ListItems.Clear
        For z = 1 To ListViewLV1.ListItems.Count
            ...
            ...
            ...
End Sub

Ths first line of the subroutine,
(If ListViewLV1.SelectedItem Is Nothing Then Exit Sub)
always indicates that something is selected.

How can I actually deselect ALL of the items in the
ListView box?

Thanks,
Craig



Sun, 19 Sep 2004 07:36:54 GMT  
 ListView control - "Selected"
SelectedItem is a misnomer, give the existence of the Selected property.  It
should really be called FocusItem, or something to indicate that it's the
item with the focus rectangle.  This confusion is further compounded by the
ListView's behavior of auto-selecting the focus item when in single-select
mode.  Even in single-select mode, you can induce the list's SelectedItem to
not be Selected--as you've encountered.


Quote:
> I'm using a ListView control (3-lvwReport mode) and I have
> a command button to "Deselect" everything in the list. The
> code for it is:

> Private Sub CmdClearLV1_Click()
>     For z = 1 To ListViewLV1.ListItems.Count
>         ListViewLV1.ListItems(z).Selected = False
>     Next z
> End Sub

> I also have a command button to add selected items from
> the ListView control to another list box. Since there is
> no reason to "add" items if there is nothing selected, I
> do a check at the beginning of the add subroutine. For
> some reason, it always seems to show an item selected.

> Private Sub CmdAddLV1_Click()
>     If ListViewLV1.SelectedItem Is Nothing Then Exit Sub
>     If ListViewLV1.ListItems.Count > 1 Then
>         ListViewHidden.ListItems.Clear
>         For z = 1 To ListViewLV1.ListItems.Count
>             ...
>             ...
>             ...
> End Sub

> Ths first line of the subroutine,
> (If ListViewLV1.SelectedItem Is Nothing Then Exit Sub)
> always indicates that something is selected.

> How can I actually deselect ALL of the items in the
> ListView box?

> Thanks,
> Craig



Sun, 19 Sep 2004 09:56:38 GMT  
 ListView control - "Selected"
Use this:
Private Sub CmdAddLV1_Click()
    For Z = 1 To ListViewLV1.ListItems.Count
        If ListViewLV1.ListItems(Z).Selected Then Exit For
    Next Z
    If Z > ListViewLV1.ListItems.Count Then
        MsgBox ("There are no selected items")
    Else
        MsgBox ("There are selected item(s) in list")
    End If
End Sub

Mikhail Berlyant
Data Integrator, Yahoo!Music   www.yahoo.com
Brainbench MVP for Visual Basic   www.brainbench.com


Quote:
> I'm using a ListView control (3-lvwReport mode) and I have
> a command button to "Deselect" everything in the list. The
> code for it is:

> Private Sub CmdClearLV1_Click()
>     For z = 1 To ListViewLV1.ListItems.Count
>         ListViewLV1.ListItems(z).Selected = False
>     Next z
> End Sub

> I also have a command button to add selected items from
> the ListView control to another list box. Since there is
> no reason to "add" items if there is nothing selected, I
> do a check at the beginning of the add subroutine. For
> some reason, it always seems to show an item selected.

> Private Sub CmdAddLV1_Click()
>     If ListViewLV1.SelectedItem Is Nothing Then Exit Sub
>     If ListViewLV1.ListItems.Count > 1 Then
>         ListViewHidden.ListItems.Clear
>         For z = 1 To ListViewLV1.ListItems.Count
>             ...
>             ...
>             ...
> End Sub

> Ths first line of the subroutine,
> (If ListViewLV1.SelectedItem Is Nothing Then Exit Sub)
> always indicates that something is selected.

> How can I actually deselect ALL of the items in the
> ListView box?

> Thanks,
> Craig



Sun, 19 Sep 2004 10:07:41 GMT  
 ListView control - "Selected"
I'm a little suprised that after having my questions on
the newsgroup for two days, no one suggested the solution
I came up with.

I mearly add the line :
     ListViewLV1.SelectedItem = Nothing
after the loop in the CmdClearLV1_Click subroutine.

Is there any reason I shouldn't be doing it this way? Am I
missing something, or is it that VB just allow so many
different ways to accomplish the same thing that everyone
gets confused?

Anyway I appreciate the other replys and help.

Thanx,
Craig

Quote:
>-----Original Message-----
>I'm using a ListView control (3-lvwReport mode) and I
have
>a command button to "Deselect" everything in the list.
The
>code for it is:

>Private Sub CmdClearLV1_Click()
>    For z = 1 To ListViewLV1.ListItems.Count
>        ListViewLV1.ListItems(z).Selected = False
>    Next z
>End Sub

>I also have a command button to add selected items from
>the ListView control to another list box. Since there is
>no reason to "add" items if there is nothing selected, I
>do a check at the beginning of the add subroutine. For
>some reason, it always seems to show an item selected.

>Private Sub CmdAddLV1_Click()
>    If ListViewLV1.SelectedItem Is Nothing Then Exit Sub
>    If ListViewLV1.ListItems.Count > 1 Then
>        ListViewHidden.ListItems.Clear
>        For z = 1 To ListViewLV1.ListItems.Count
>            ...
>            ...
>            ...
>End Sub

>Ths first line of the subroutine,
>(If ListViewLV1.SelectedItem Is Nothing Then Exit Sub)
>always indicates that something is selected.

>How can I actually deselect ALL of the items in the
>ListView box?

>Thanks,
>Craig

>.



Tue, 21 Sep 2004 02:28:45 GMT  
 ListView control - "Selected"


Quote:
> I'm a little suprised that after having my questions on
> the newsgroup for two days, no one suggested the solution
> I came up with.

> I mearly add the line :
>      ListViewLV1.SelectedItem = Nothing
> after the loop in the CmdClearLV1_Click subroutine.

It's great if this gives you want you want.  This statement is a little
fickle, though, as the control has a tendancy to set this property itself.
If, for instance, you try this in Form_Load, you'll find that it's
countermanded by the control's defaulting mechanism.

Quote:
> Is there any reason I shouldn't be doing it this way?

It's your call, but it seems to be two wrongs to get a right.

Quote:
> Am I
> missing something, or is it that VB just allow so many
> different ways to accomplish the same thing that everyone
> gets confused?

It is confusing the way it's named.  The "SelectedItem" is not directly
correlated to the items that are selected--it's just the item with focus.

Simply do not base your selection count upon that property and it won't be a
problem.

Mikhail's suggestion is up the right alley.  I hope it will be of no offense
to him, though, that I prefer a routine that does not rely upon side
effects.

Public Function lv_any_item_selected(ByVal oLV as ListView) As Boolean
  Dim oItem as ListItem

  For Each oItem in oLV.ListItems
    If oItem.Selected Then
      lv_any_item_selected = True
      Exit Function
    End If
  Next oItem

  lv_any_item_selected = False
End Function



Tue, 21 Sep 2004 04:24:14 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. ListView "Selected" property not working right

2. "OwnerData" and ListView Control

3. Listview control "Key" property

4. *"*-.,._,.-*"* I"LL TRADE VISUAL C++ FOR VBASIC *"*-.,_,.-*"*

5. "Slide sorter","light box","image gallery" control, looking for

6. Select every "n"th record looped from user input

7. Referencing the "Selected" mail item

8. How to customize the dialog box "select names"/addressbook

9. "Selecting" picture programmatically

10. "select...case", multiples cases not possible ?

11. "Select All" code misbehaving

12. "Select All" in FileListBox

 

 
Powered by phpBB® Forum Software