Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

easy points...

Posted on 1999-07-08
16
Medium Priority
?
178 Views
Last Modified: 2010-05-02
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
Comment
Question by:OeilNoir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 1

Expert Comment

by:alex1234
ID: 1522900
Use ListIndex method.

0
 
LVL 1

Author Comment

by:OeilNoir
ID: 1522901
i said a LISTVIEW control....
There is no listindex methode...
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 400 total points
ID: 1522902
'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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:OeilNoir
ID: 1522903
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522904
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
 
LVL 1

Author Comment

by:OeilNoir
ID: 1522905
i'll check it out tomm...
0
 
LVL 1

Author Comment

by:OeilNoir
ID: 1522906
Sorry for the delay...
This doesn't work.. any idea why?
0
 
LVL 1

Author Comment

by:OeilNoir
ID: 1522907
it still return the item 1 as the default item, even if none are selected....
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522908
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
 
LVL 1

Author Comment

by:OeilNoir
ID: 1522909
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522910
Well spotted!.... I'm using SP3
:-)
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522911
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
 
LVL 1

Author Comment

by:OeilNoir
ID: 1522912
Where can i get SP3 for MWCC 6.0?
and what do you mean by "you execute the lines of code yourself. " ?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522913
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
 
LVL 1

Author Comment

by:OeilNoir
ID: 1522914
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522915
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question