Solved

list view

Posted on 2001-07-24
13
167 Views
Last Modified: 2010-05-02
Ok this is kind of a stupid question but i have a ListView with a lot of entries in it that it is about 4 or 5 pages long.  At certains i want to highlight a certain entry and show it to the user, so i use
lvw.listitems(xx).selected=true
lvw.ensurevisible.
The problem i am having is if the item i want is lower on the list than the currently selected item it will scroll down to the page and highlight it.  But if the item is higher it doesn't scroll up to it, but when i manually scroll it is highlight.  Anyone have any idea what i am doing wrong?
0
Comment
Question by:MChu
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
I think this is a glitch with the LV control

Here is what I have done in the past, up to the top then down to the desired element... Not pretty but it works

With lvw
   .ListItems.Item(1).EnsureVisible
   .ListItems(RealIndexYouWant).selected = true
   .ListItems.Item(RealIndexYouWant).EnsureVisible
End With
0
 

Author Comment

by:MChu
Comment Utility
It doesn't work.  Does it work for you?
0
 

Author Comment

by:MChu
Comment Utility
In listbox there is a property for topindex, is there a similar property for listview?
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
Give this a shot... been a little while

lvw.ListItems(1).EnsureVisible = True
lvw.ListItems(RealIndexYouWant).selected = true
lvw.ListItems(RealIndexYouWant).EnsureVisible = True
0
 

Author Comment

by:MChu
Comment Utility
I did, i cut and paste but didn't work.  Any other ideas?
0
 
LVL 8

Expert Comment

by:Dave_Greene
Comment Utility
Try switching them

lvw.ListItems(RealIndexYouWant).EnsureVisible = True
lvw.ListItems(RealIndexYouWant).selected = true
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
I think you cannot get that withoutr using API.
It is just a thought.
0
 
LVL 27

Accepted Solution

by:
Ark earned 50 total points
Comment Utility
Just add SetFocus:

Private Sub Command1_Click()
   ListView1.ListItems(100).EnsureVisible
   ListView1.ListItems(100).Selected = True
   ListView1.SetFocus
End Sub

Private Sub Form_Load()
  ListView1.View = lvwReport
  ListView1.ColumnHeaders.Add , , "Items"
  For i = 1 To 200
     ListView1.ListItems.Add , , "Item No. " & CStr(i)
  Next i
End Sub
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
PS. tested both vb5/vb6 controls - works fine

Cheers
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
Hi
Alternate Function:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LVM_FIRST = &H1000
Const LVM_SCROLL = (LVM_FIRST + 20)

Private Sub ScrollToIndex(ByVal nIndex As Long)
   Dim nTopIndex As Long, yScroll As Long
   nTopIndex = ListView1.GetFirstVisible.Index
   yScroll = ListView1.ListItems(1).Height \ Screen.TwipsPerPixelY
   yScroll = yScroll * (nIndex - nTopIndex)
   Call SendMessage(ListView1.hWnd, LVM_SCROLL, 0, ByVal yScroll)
   ListView1.ListItems(nIndex).Selected = True
   ListView1.SetFocus
End Sub


Cheers
0
 

Author Comment

by:MChu
Comment Utility
Hmm, When i try your code on a brand new project it worked.  But on my current one it doesn't, which probably means that the problem is coming from somewhere else.  I am going to give the points to ark since he made me realize this.  Thanks everyone for your help.
0
 

Author Comment

by:MChu
Comment Utility
Nice code, even clear to understand even though it didn't solve my problem.
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
Thanks for points, glad I could help you.
BTW, look at ScrollToIndex Sub - it works as fast as EnsureVisible, but always put Selected Item on top of the list, even if it's already visible.

Cheers
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now