How do I modify ListView's scroll position?

  • Thread starter Thread starter trant
  • Start date Start date
T

trant

I would like to get and set a ListView's scroll position using .NET 3.5

Is there an easy way to do this with managed code?

while searching on the web I seemed to have found one solution but it
requires Interop with some getscorllinfo/setuserinfo functions. Is that the
only way ??
 
I would like to get and set a ListView's scroll position using .NET 3.5

Is there an easy way to do this with managed code?
Hi,

you can use listBox.TopIndex

Uwe
 
I would like to get and set a ListView's scroll position using .NET 3.5

Is there an easy way to do this with managed code?
Hi,

if you like to show the last item you can use this code:

listBox1.SetSelected(listBoxMeldungen.Items.Count-1,true);
listBox1.SetSelected(listBoxMeldungen.Items.Count-1,false);

Uwe
 
Hi,

if you like to show the last item you can use this code:

listBox1.SetSelected(listBoxMeldungen.Items.Count-1,true);
listBox1.SetSelected(listBoxMeldungen.Items.Count-1,false);

He said list VIEW, not list BOX. Your other suggestion about TopItem works
fine for both, but SetSelected() is not a method of the ListView class.
 
I can get and save the TopItem however when I next relaod the ListView and
set the TopItem nothing happens.

Basically I have a separate dialog form which has a ListView.

I populate the ListView like this:

listViewEx1.BeginUpdate();
listViewEx1.ListViewItemSorter = sorter;
foreach ( // objects I fill list with ) {

ListViewItem item = new ListViewItem();
// fill subitems
}

listViewEx1.Sort();
listViewEx1.EndUpdate();

if (lastTopItem != null)
{
listViewEx1.TopItem = lastTopItem;
}


lastTopItem is defined as:
static ListViewItem lastTopItem;

It is set on closing the form and when I step through the code I verify that
lastTopItem is set properly and it does execute the line setting TopItem to
the right object when the form is reloaded however it has absolutely no
effect. The list created is sorted and shows exactly the same items as when I
last set TopItem which is when I scrolled down about halfway down my list yet
when I reload the list and set TopItem it shows the ListView with scroll
position at 0 (the top).

Something I missed?
 
I can get and save the TopItem however when I next relaod the ListView and
set the TopItem nothing happens.

Basically I have a separate dialog form which has a ListView.

I populate the ListView like this:

listViewEx1.BeginUpdate();
listViewEx1.ListViewItemSorter = sorter;
foreach ( // objects I fill list with ) {

ListViewItem item = new ListViewItem();
// fill subitems
}

listViewEx1.Sort();
listViewEx1.EndUpdate();

if (lastTopItem != null)
{
listViewEx1.TopItem = lastTopItem;
}


lastTopItem is defined as:
static ListViewItem lastTopItem;

It is set on closing the form and when I step through the code I verify that
lastTopItem is set properly and it does execute the line setting TopItem to
the right object when the form is reloaded however it has absolutely no
effect. The list created is sorted and shows exactly the same items as when I
last set TopItem which is when I scrolled down about halfway down my list yet
when I reload the list and set TopItem it shows the ListView with scroll
position at 0 (the top).

Something I missed?

Hi,

when you fill the ListView you create each item new. Therefore the
item is not the same which you have stored in lastTopItem. Store the
index of the TopItem.

static int lastTopIndex = listViewEx1.TopItem.Index;

After you have filled the ListView set the TopMostItem with

listViewEx1.TopMostItem = listViewEx1.Items[lastTopIndex];

Uwe
 
Back
Top