Solved

list view

Posted on 2001-07-24
13
171 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
ID: 6314302
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
ID: 6314349
It doesn't work.  Does it work for you?
0
 

Author Comment

by:MChu
ID: 6314352
In listbox there is a property for topindex, is there a similar property for listview?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6314386
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
ID: 6314431
I did, i cut and paste but didn't work.  Any other ideas?
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6314493
Try switching them

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

Expert Comment

by:Richie_Simonetti
ID: 6314979
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
ID: 6315496
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
ID: 6315500
PS. tested both vb5/vb6 controls - works fine

Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 6315622
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
ID: 6315757
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
ID: 6315758
Nice code, even clear to understand even though it didn't solve my problem.
0
 
LVL 27

Expert Comment

by:Ark
ID: 6315774
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

778 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