By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Remember position of scrolling in listview in VB6

Robbie
100+
P: 180
Hi again, I have a listview which gets cleared and has items added again every now and then, and it's very annoying to use because once it's cleared, the scrollbar goes back to the top.
I'd like it to go back to where it was before, as if it's not obvious anything ever happened.

I know there's a similar topic around, but that's in VB.NET which I'm not at all familiar with.

So far I have been able to make it remember what the last selected item was and re-select this same item afterwards, but this does not make the listview scroll back down to that position (if you scroll down, you can see that same item's selected again though). I used this code:
Expand|Select|Wrap|Line Numbers
  1. 'Make a note of selected item
  2.     On Error Resume Next
  3.         OrigListPosition = MainList.SelectedItem.Index
  4.     If Err.Number = 91 Then OrigListPosition = -1
  5.     On Error GoTo 0
  6.     'All done, error-checking's back on, and if no item was selected (error 91 here),
  7.     'OrigListPosition is -1, otherwise it's the index.
  8.  
  9. '<LIST-UPDATING CODE>
  10.  
  11. 'Make the item which was selected before re-making this list, be selected again
  12.     If OrigListPosition > -1 And OrigListPosition < MainList.ListItems.Count Then
  13.     'Checks to make sure it's less than how many items there are, because this time the list
  14.     'may have less items.
  15.         MainList.ListItems(OrigListPosition).Selected = True
  16.     End If
Apr 12 '07 #1
Share this Question
Share on Google+
6 Replies


iburyak
Expert 100+
P: 1,017
You probably can save ListIndex in a variable and then after reload set it back to a ListIndex which is basically you are moving to whatever was previously selected.

I didn't test it thou it is just an idea.
Apr 12 '07 #2

Robbie
100+
P: 180
You probably can save ListIndex in a variable and then after reload set it back to a ListIndex which is basically you are moving to whatever was previously selected.

I didn't test it thou it is just an idea.
Good idea, but a ListView does not have a ListIndex, and even if it did I think it is read-only... =/

It has SelectedItem.Index but unfortunately that is read-only.
Apr 12 '07 #3

Robbie
100+
P: 180
Sorry for a double-post but at least it has some useful info.

I thought of a way to do it:
- Event triggers updating list (e.g. typing in a text box).
- Make note of selected item.
- Update list.
- Select previously-selected item.
- Bring focus to the ListView.
- Use SendKeys() to simulate Up arrow then Down arrow to force VB to realize the selection is at that position, then it would scroll to that position (it does this if you press Up then Down yourself).
- Give focus back to the textbox which you first typed in.

I thought that was all going to work fine, BUT it seems you can't use SetFocus on a ListView! If I do MainList.SetFocus (SetFocus shows up in the little menu of Properties/Methods when you type the dot after MainList), then when I run it I get runtime error 5, invalid procedure call or argument! ><
aaagh...
Apr 12 '07 #4

iburyak
Expert 100+
P: 1,017
OK Sorry I was lazy thought it is simple. But look at my working solution.

Put a ListView and a button on your form and copy code.....

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. For i = 0 To 1000
  3.     ListView1.ListItems.Add , , "Irina " & i
  4. Next
  5.  
  6.  
  7. End Sub
  8.  
  9.  
  10. Private Sub Command1_Click()
  11. Dim a
  12. a = ListView1.GetFirstVisible.Index
  13.  
  14.  
  15. For i = 1000 To 1 Step -1
  16.     ListView1.ListItems.Remove i
  17. Next
  18.  
  19. DoEvents
  20. For i = 0 To 1000
  21.     ListView1.ListItems.Add , , "Irina " & i
  22. Next
  23. DoEvents
  24. ListView1.ListItems.Item(a).EnsureVisible
  25. End Sub

Good Luck
Irina.
Apr 13 '07 #5

P: 58
Hi....

Are you familiar with ARRAYS... i think this can solve your problem....
A dynamic arrays can solve your problem... and since your adding and adding an entry Use Redim for your array... i hope you get my idea...


One more thing i dont regularly visit this site because am usually bz....


GoodLuck....


Sorry for a double-post but at least it has some useful info.

I thought of a way to do it:
- Event triggers updating list (e.g. typing in a text box).
- Make note of selected item.
- Update list.
- Select previously-selected item.
- Bring focus to the ListView.
- Use SendKeys() to simulate Up arrow then Down arrow to force VB to realize the selection is at that position, then it would scroll to that position (it does this if you press Up then Down yourself).
- Give focus back to the textbox which you first typed in.

I thought that was all going to work fine, BUT it seems you can't use SetFocus on a ListView! If I do MainList.SetFocus (SetFocus shows up in the little menu of Properties/Methods when you type the dot after MainList), then when I run it I get runtime error 5, invalid procedure call or argument! ><
aaagh...
Apr 13 '07 #6

Robbie
100+
P: 180
OK Sorry I was lazy thought it is simple. But look at my working solution.

Put a ListView and a button on your form and copy code.....

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. For i = 0 To 1000
  3.     ListView1.ListItems.Add , , "Irina " & i
  4. Next
  5.  
  6.  
  7. End Sub
  8.  
  9.  
  10. Private Sub Command1_Click()
  11. Dim a
  12. a = ListView1.GetFirstVisible.Index
  13.  
  14.  
  15. For i = 1000 To 1 Step -1
  16.     ListView1.ListItems.Remove i
  17. Next
  18.  
  19. DoEvents
  20. For i = 0 To 1000
  21.     ListView1.ListItems.Add , , "Irina " & i
  22. Next
  23. DoEvents
  24. ListView1.ListItems.Item(a).EnsureVisible
  25. End Sub

Good Luck
Irina.
Thanks! =D
But I needed to change GetFirstVisible.Index to SelectedItem.Index - GetFirstVisible always gave back about 5 items past the last item in view in the list, for some reason.
Also, it is much easier and faster just to use ListItems.Clear, since I'm always refreshing the whole list.

Thanks again!

And Esmael, I use arrays every now and then, but I do not know about the ReDim statement...
Apr 13 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.