Solved

stop at the end

Posted on 2007-03-19
9
142 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 50

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 50

Expert Comment

by:Ryan Chong
ID: 18746855
I thought the Index property should start from 1, not 0?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
LVL 50

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 115
Visual Basic Excel Formatting error 4 102
Best way to parse out a json string in VB6? 10 162
vbModal 12 51
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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

813 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

9 Experts available now in Live!

Get 1:1 Help Now