Solved

stop at the end

Posted on 2007-03-19
9
139 Views
Last Modified: 2010-04-30
this is what im using to select the next record from my listview
but how can I make something so it wont keep trying to
select more when it gets to the end?

Private Sub SelectNextOne()
    With lvwMain
        If Not .SelectedItem Is Nothing Then
            If .SelectedItem.Index < .ListItems.Count And .ListItems.Count > 1 Then
                .ListItems(.SelectedItem.Index + 1).Selected = True
            End If
        End If
    End With
End Sub
0
Comment
Question by:Sabrin
  • 3
  • 3
  • 3
9 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 18746847
Check for Count - 1:

Private Sub SelectNextOne()
    With lvwMain
        If Not .SelectedItem Is Nothing Then
            If .SelectedItem.Index < .ListItems.Count - 1 And .ListItems.Count > 1 Then
                .ListItems(.SelectedItem.Index + 1).Selected = True
            End If
        End If
    End With
End Sub

The last item in the listview is .ListItems.Count -1 so if it is less than that there will be one more to select. If it is the last one then no more will be selected.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 18746850
what is mean by "select more when it gets to the end" ?

above sample should works without any error.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 18746855
I thought the Index property should start from 1, not 0?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 18746859
Good question, I can't remember right now. I though the index property was from 0 to .Count -1 it is with the listbox, can't be certain from memory on the listview though. If it is zero based then need to use .Count - 1 otherwise as you say it should work as is.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Expert Comment

by:Ryan Chong
ID: 18746866
cheers Tim ; )
0
 

Author Comment

by:Sabrin
ID: 18746885
so whats the correct code?
0
 

Author Comment

by:Sabrin
ID: 18746896
like this?

If .SelectedItem.Index < .ListItems.Count - 0 And .ListItems.Count > 1 Then
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 18746907
As I showed you assuming that the .index propery starts at zero and not 1.

            If .SelectedItem.Index < .ListItems.Count - 1 And .ListItems.Count > 1 Then
0
 

Author Comment

by:Sabrin
ID: 18746928
with this line it stops 1 before the end

If .SelectedItem.Index < .ListItems.Count - 1 And .ListItems.Count > 1 Then
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now