Solved

list view

Posted on 2001-07-24
13
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

710 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