How to ask control to scroll using C#

I was asked how to ask control to scroll using C#, Well here is a very simple way :

Declare some constants:
private const int WM_SCROLL = 276; // Horizontal scroll
private const int WM_VSCROLL = 277; // Vertical scroll
private const int SB_LINEUP = 0; // Scrolls one line up
private const int SB_LINELEFT = 0;// Scrolls one cell left
private const int SB_LINEDOWN = 1; // Scrolls one line down
private const int SB_LINERIGHT = 1;// Scrolls one cell right
private const int SB_PAGEUP = 2; // Scrolls one page up
private const int SB_PAGELEFT = 2;// Scrolls one page left
private const int SB_PAGEDOWN = 3; // Scrolls one page down
private const int SB_PAGERIGTH = 3; // Scrolls one page right
private const int SB_PAGETOP = 6; // Scrolls to the upper left
private const int SB_LEFT = 6; // Scrolls to the left
private const int SB_PAGEBOTTOM = 7; // Scrolls to the upper right
private const int SB_RIGHT = 7; // Scrolls to the right
private const int SB_ENDSCROLL = 8; // Ends scroll

Add a using reference
using System.Runtime.InteropServices;

Declare external SendMessage:
[DllImport("user32.dll",CharSet=CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg,IntPtr wParam, IntPtr lParam);

Usage:
If you have some textbox on the form...

SendMessage( Control Handle , WM Scroll Message, (IntPtr) Scroll Command ,IntPtr.Zero);

Scroll page up
SendMessage(textBox1.Handle,WM_VSCROLL,(IntPtr)SB_PAGEUP,IntPtr.Zero);

Scroll page down
SendMessage(textBox1.Handle,WM_VSCROLL,(IntPtr)SB_PAGEDOWN,IntPtr.Zero);

 

17 Comments

  • Really thanks for this example. i've been searching for this kind of thing for ages!

  • it is what i needed,thanks very much.

  • Exactly what I needed! Super code! THANK YOU!

  • Short but very useful! Thank you a lot!

    Gregor

  • This works fine over a lot of controls, but not over all of them. If you try to use this code over a DataGridView the scroll is not done.
    When you use the scroll bar of any control, two messages are sended to the control's handle (i.e. vertical scroll): WM_VSCROLL with lParam=SB_LINEDOWN (or SB_LINEUP) and WM_SCROLL with lParam=SB_ENDSCROLL. If the control is a DataGridView, the wParam is not IntPtr.Zero, it has a value that I can not determinate what it does mean. But if you send a WM_VSCROLL to a DataGridView with wParam)IntPtr.Zero nothing happends.

    (sorry for bad English)

  • I have found the solution for a DataGridView.

    The DataGridView control has 2 sub-controls, a VScrollBar and a HScrollBar. If you want to do a scroll over a DataGridView you have to send the WM_VSCROLL or WM_HSCROLL message to the DataGridView handle, with wParam as the SB_xxxx value you want, and lParam as the scrollbar handle.

    Example:
    public void ScrollControlDown(DataGridView dataGridView)
    {
    VScrollBar barraVertical = null;

    foreach (Control c in dataGridView.Controls)
    {
    if (c is VScrollBar) barraVertical = (VScrollBar)c;
    }

    if (barraVertical != null)
    {
    SendMessage(dataGridView.Handle, WM_VSCROLL, (IntPtr)SB_LINEDOWN, barraVertical.Handle);
    SendMessage(dataGridView.Handle, WM_VSCROLL, (IntPtr)SB_ENDSCROLL, barraVertical.Handle);
    }
    }

    This method requires the DataGridView's ScrollBars property shows the scrollbar over you are going to do the programatical scroll. If you want to send WM_VSCROLL, ScrollBars property must be set to Both or Vertical. If you want to send WM_HSCROLL, ScrollBars propery must be set to Both or Horizontal.

  • I have multi threaded datagridview. where should i call this sendmessage function?

  • How to capture warning message of dialog box using C#

  • Its is very useful for me. Thank U

  • i got the following error message
    Can't find PInvoke DLL 'user32.dll'.
    when i call
    SendMessage(textBox1.Handle,WM_VSCROLL,(IntPtr)SB_PAGEUP,IntPtr.Zero);
    anyone knwo how to find it. I am using WM5

  • I try to apply it to my datagrib but just wondering want to know where i need to put the "
    SendMessage(dataGridView.Handle, WM_VSCROLL, (IntPtr)SB_LINEDOWN, barraVertical.Handle); "

    private void tbl_contactsDataGrid_CurrentCellChanged(object sender, EventArgs e)
    {
    SendMessage(dataGridView.Handle, WM_VSCROLL, (IntPtr)SB_LINEDOWN, barraVertical.Handle);
    }

    Thanks kevin

  • It wont work for sb_thumbtrack

  • Very useful, thanks.

    Now, I`m trying to make a class with this method to use it on diferent projects. The problem is, How can I pass the correct parameter?. See the code I made:

    public int SubirPagina(IntPtr hWnd) //pageup
    {
    return SendMessage(hWnd, WM_VSCROLL, (IntPtr)SB_PAGEUP, IntPtr.Zero);
    }

    public int BajarPagina(IntPtr hWnd) //PageDown
    {
    return SendMessage(hWnd, WM_SCROLL, (IntPtr)SB_PAGEDOWN, IntPtr.Zero);
    }

    And I call them with:

    botones.SubirPagina(flpFamilias.Handle); //"botones" is the classname

    But nothing happen, What's wrong?

    Thanks.

  • I found the solution to the problem I asked.

    I wrote wrong parameter. I passed wrong scroll constant in the second parameter. It's a vertical scroll, so this is the correct code fragment:

    public int BajarPagina(IntPtr hWnd) //PageDown

    {

    return SendMessage(hWnd, WM_VSCROLL, (IntPtr)SB_PAGEDOWN, IntPtr.Zero);

    }

    Thanks.

    PD: Sorry for bad English

  • Hi.

    I have tried to use this code over a WebBrowser, however the scroll is not done.

    Some idea?

    Thanks in advance.

  • Kevin,
    For .Net Compact Framework, many of the functions that should be in user32.dll are found in "coredll.dll" (not just core.dll).

  • I have a textbox in aspx page and direct output from a database select statement and be able to scroll it. How to use modify this code?

Comments have been disabled for this content.