Scrolling A ListView

G

Gary Brown

Hi,

How do you programmatically scroll a ListView control horizontally?
(The same effect as if the user used the horizontal scroll bar,)
I've done it in C++/MFC, but can't find the means in C#. Autoscroll
is enabled.

Also, I need to know if the user scrolls the ListView horizontally.

Thanks,
Gary
 
K

Ken Kolda

To scroll the listview horizontally, I believe you'll have to use the
SendMessage API, e.g.

[DllImport("user32")]
static extern IntPtr SendMessage(IntPtr Handle, Int32 msg, IntPtr wParam,
IntPtr lParam);

protected void ScrollH(int pixelsToScroll)
{
const Int32 LVM_FIRST = 0x1000;
const Int32 LVM_SCROLL = LVM_FIRST + 20;
SendMessage(lvwList.Handle, LVM_SCROLL, (IntPtr) pixelsToScroll,
IntPtr.Zero);
}

To know when the user scrolls horizonatally, you'll need to capture the
WM_HSCROLL message in the WndProc() method, e.g.

protected override void WndProc(ref Message m)
{
const Int32 WM_HSCROLL = 0x114;

if (m.Msg == WM_HSCROLL)
HandleHorizontalScroll();

base.WndProc(ref m);
}

I'd recommend you subclass the ListView control and then add these methods
to it to achieve what you're looking for.

Ken
 
N

Nicholas Paldino [.NET/C# MVP]

Gary,

How did you do it in C++/MFC? If you are sending a windows message,
then you can easily do this by declaring the SendMessage function and call
it through the P/Invoke layer, passing the appropriate arguments.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top