• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

easy points...

How do i check in a listview to see if there is an item selected?

i tried with this "lstView.SelectedItem.index" but it return the index of the 1st item if none are selected...
0
OeilNoir
Asked:
OeilNoir
  • 8
  • 7
1 Solution
 
alex1234Commented:
Use ListIndex method.

0
 
OeilNoirAuthor Commented:
i said a LISTVIEW control....
There is no listindex methode...
0
 
caraf_gCommented:
'For example:

If YourListView.SelectedItem Is Nothing Then
    'No item selected
    Exit Sub
End If

If YourListView.SelectedItem.Selected = False Then
    'This can happen in a multi-select list view
    'No item selected
    Exit Sub
End If

'Now you can be sure an item is selected, and you can access it by:
YourListView.SelectedItem

'For multi-select list views
Dim lngCounter As Long
Dim objItem As ListItem
For lngCounter = 1 To YourListView.ListItems.Count
    Set objItem = YourListView.ListItems(lngCounter)
    If objItem.Selected Then
        'Process this item
    End If
Next
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
OeilNoirAuthor Commented:
it doesn't work...
the problem is that when no item are selected, the "lstView.SelectedItem"
still return the first item... i don't know why... so, it will never be "Is Nothing"
0
 
caraf_gCommented:
Exactly, but don't forget you've got a second check as well:

If YourListView.SelectedItem.Selected = False Then
    'Even though SelectedItem returned a list item, it is _
     not actually selected, and hence we must conclude that _
     no item is selected

End If

(See the SECOND if statement in my answer)
0
 
OeilNoirAuthor Commented:
i'll check it out tomm...
0
 
OeilNoirAuthor Commented:
Sorry for the delay...
This doesn't work.. any idea why?
0
 
OeilNoirAuthor Commented:
it still return the item 1 as the default item, even if none are selected....
0
 
caraf_gCommented:
Ok, I tried this with VB5.

I put a list view on the form, a button, and pasted in the following code:

Private Sub Command1_Click()

With Me.ListView1
    If .SelectedItem Is Nothing Then
        MsgBox "Nothing is selected"
        Exit Sub
    End If
    If .SelectedItem.Selected = False Then
        MsgBox "nothing is selected"
        Exit Sub
    End If
End With

End Sub

Private Sub Form_Load()

With Me.ListView1
    .View = lvwReport
    .ColumnHeaders.Add , , "Test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    .ListItems.Add , , "test"
    '.SelectedItem.Selected = False
    'Set .SelectedItem = Nothing
End With

End Sub

It works perfectly.

In VB6, you have to uncomment the two commented lines, because by default a list view with multiselect set to false will always have one item selected.

Again, it works perfectly.

AHA!!!!!

If you set multiselect to TRUE you get exactly what you describe.

So the solution is, before the list view is displayed for the first time, execute:
YourListView.SelectedItem.Selected = False
Set YourListView.SelectedItem = Nothing

Now the code will work.

I guess therefore that
1 you are using VB6
2 your listview has a multiselect property = true
0
 
OeilNoirAuthor Commented:
well.. yes i'm using VB6, but multiselect is not enabled... i can't allow the user to select more than one item at once...

i figured out something...

the code doesn't work, but it behave differentrly with "Microsoft Windows Common Control 6.0" and with "Microsoft Windows Common Control 5.0 (SP2)"

with "Microsoft Windows Common Control 6.0"
it return the last item that was selected before.. and if none was selected,  it return the 1st one by default...

and with Microsoft Windows Common Control 5.0 (SP2) then
it also return the last item that was selected, but, if none was selected before, then your code will work...

what version of common control are you using?
0
 
caraf_gCommented:
Well spotted!.... I'm using SP3
:-)
0
 
caraf_gCommented:
Sorry - I'm using SP3 for MWCC 6.0, and SP2 for MWCC 5.0

It's exactly like you said, and the only way around it for 6.0 is if you execute the lines of code yourself.

0
 
OeilNoirAuthor Commented:
Where can i get SP3 for MWCC 6.0?
and what do you mean by "you execute the lines of code yourself. " ?
0
 
caraf_gCommented:
I would guess you can get SP3 from the Microsoft site. I don't know where the PC I'm currently working on got it from, I only started working for this company today ;-)

With "execute...yourself" I meant the following.

I am assuming that you are going to populate the list view with data before displaying it to the user.

The problem with the VB6 (SP3?) list view is that it then exhibits the behaviour you are complaining about. You can avoid this by executing the following two lines after populating the list view:

YourListView.SelectedItem.Selected = False
Set YourListView.SelectedItem = Nothing

After executing these lines, the method explained in my answer will work correctly.

Good luck!

Pino
0
 
OeilNoirAuthor Commented:
Arghhhhhhhhh.. i just found the answer to my problem..

i had the hide selection option checked.. which caused it to don't display which row was selected.. so, when smeone was clicking on an other control (or that is, when the list view was losing the focus) there was no more selection visible (and i tought it was losing it's selection) so, when a user was clicking later, on the delete button, (no selection was displayed) it was deleted the selection anyway ... hehe

damn... all that time for such a small problem hhehe
as long as the user can see which item is selected, i don't mind and need to unselect it =)

Well.. you deserve the points for all the help =))))
Thanks a lot!
0
 
caraf_gCommented:
:-)
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now