EnsureVisible problems

pachjo

Well-known member
Joined
Dec 12, 2006
Messages
370
Programming Experience
10+
Hi,

I am trying to get my listview to automatically scroll to the last record in the list of data displayed to ensure the user sees the latest entry rather than having to scroll down each time.

I found references to the EnsureVisible() method but am unable to get it to make any difference to my application....namely it does not move to the last record!

This is what I am trying

VB.NET:
[SIZE=2]lsvListView.EnsureVisible(lsvListView.Items.Count - 1)

Any help please?

Thanks
[/SIZE]
 
sorry my mistake, I actually meant listview instead of listbox...I was having one of those moments :p
Urgh, old code I think, serves me right for quickly searching on google :D

I just found a link that says if you don't use listview.select() it will never select what item you choose, so maybe try:

VB.NET:
[SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListView1[/SIZE]
[SIZE=2].Items(.Items.Count - 1).Selected = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2].Select()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
 
lol :D

Nope all that code does is selected the last item in the listview but does not scroill the list up to make it visible to the user.

I'm confused as all the help I find on the listview control states that

ListView.EnsureVisible(index)

should work

So...why does this not work?

VB.NET:
lsvListView.EnsureVisible(lsvListView.Items.Count - 1)

:confused: :( :confused:
 
because you're not selecting it first. (I think)

Have you tried combining all of the code?

VB.NET:
[COLOR=#0000ff]With[SIZE=2]Me[/SIZE][/COLOR][SIZE=2].ListView1[/SIZE]
[SIZE=2]dim selItem = .Items(.Items.Count - 1).Selected = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2].Select()[/SIZE]
.ensureVisible(selItem)
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
[COLOR=#0000ff]
[/COLOR]
??
 
Nope :confused:

same result the last item is selected but the list does not scroll up to display the item.

Oh and yes I have got scroll set to true.


Ahahahhf this is doing my nut in!!!!
 
hmm I haven't really done much with listviews yet in 2005.

I'm sure in 2003 it was as simple as

.SelectedItem.EnsureVisible

:confused:

Sorry haven't been much help, I'm sure one of the experts here will point it out immediately!! (I'm just one of those wannabes!! No use to a man or his dog... :D )
 
Thanks for spending time trying to help;)

To other gurus out they a simple plea..........

HELP ME PLEASE afore I go whacko :eek:
 
You could give that a try I suppose. It's written in vb6 by the the looks of it, and uses some WIN32 calls. It's worth a shot if you can't get it to work the other way.
 
Nah, it is all gobbledygook to me, and I want to stick it .NET way.

Just cannot understand. There are 35 items in the list, the first 27 are in view. I try to get index 34 to scroll up and nothing, no error or anything?
 
I found something like this and modified to my code and still not auto scroll and no error?

VB.NET:
[SIZE=2]lviListViewItem = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListViewItem(dtTranDate.ToShortDateString)
lsvListView.Items.Add(lviListViewItem).EnsureVisible()

Curses...................

Curses...................

Every page I find says how to do it and I understand, so why does it not?

:confused: :confused:

[/SIZE]
 
I've done a search for listview problems in VS2005 and this seems to come up a lot as well as not being able to scroll to the selected row in a treeview as well.

I just found this that fixed someone's;
VB.NET:
Dim last As Short
        last = ListView1.Items.Count - 1
        ListView1.Items(last).Selected = True
        ListView1.HideSelection = False
        ListView1.EnsureVisible(last)
        TextBox1.Text = ListView1.Items(last).Text

although that's pretty much the same as what's been suggested.

Stupid suggestion - delete the listview and current code, then add a new one, rebind and use code?! maybe it's something accidently set in properties or code somewhere...
 
Back
Top