Solved

list view

Posted on 2001-07-24
13
172 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 28

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 28

Expert Comment

by:Ark
ID: 6315500
PS. tested both vb5/vb6 controls - works fine

Cheers
0
 
LVL 28

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 28

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing a value with stream reader AFTER a ";" 3 74
Visual Studio 2005 text editor 10 44
VB6 - Scroll Mouse wheel on Picturebox 13 73
Dinamic report to Crosstab query 9 33
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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.
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…
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…

856 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