Solved

stop at the end

Posted on 2007-03-19
9
137 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
Comment Utility
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
Comment Utility
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
Comment Utility
I thought the Index property should start from 1, not 0?
0
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
cheers Tim ; )
0
 

Author Comment

by:Sabrin
Comment Utility
so whats the correct code?
0
 

Author Comment

by:Sabrin
Comment Utility
like this?

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

Accepted Solution

by:
TimCottee earned 500 total points
Comment Utility
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
Comment Utility
with this line it stops 1 before the end

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

14 Experts available now in Live!

Get 1:1 Help Now