Solved

list view

Posted on 2001-07-24
13
169 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 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

19 Experts available now in Live!

Get 1:1 Help Now