Click here to Skip to main content
15,886,644 members
Articles / Programming Languages / C#
Article

EnsureVisible for ListViewSubItem

Rate me:
Please Sign up or sign in to vote.
4.87/5 (8 votes)
30 Sep 2008CPOL 49.2K   502   26   6
Scroll horizontally in a ListView to ensure visibility of a subitem

Introduction 

It extends the ListView control and adds an EnsureVisible method to scroll horizontally to a subitem.

Background   

I often use The Code Project to find good samples, but I didn't find one for this. So I just want to give something back.    

Using the Code 

The native Windows message to scroll the listview is:

C#
const Int32 LVM_FIRST = 0x1000;
const Int32 LVM_SCROLL = LVM_FIRST + 20;

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

private void ScrollHorizontal(int pixelsToScroll)
{
    SendMessage(this.Handle, LVM_SCROLL, (IntPtr)pixelsToScroll,
    IntPtr.Zero);
}

The public method to call with item to scroll to is as follows:

C#
    /// <summary>
    /// Ensure visible of a ListViewItem and SubItem Index.
    /// </summary>
    /// <param name="item"></param>
    /// <param name="subItemIndex"></param>
    public void EnsureVisible(ListViewItem item, int subItemIndex)
    {
        if (item == null || subItemIndex > item.SubItems.Count - 1)
        {
            throw new ArgumentException();
        }

        // scroll to the item row.
        item.EnsureVisible();
        Rectangle bounds = item.SubItems[subItemIndex].Bounds;

        // need to set width from columnheader, first subitem includes
        // all subitems.
        bounds.Width = this.Columns[subItemIndex].Width;

        ScrollToRectangle(bounds);
    }

    /// <summary>
    /// Scrolls the listview.
    /// </summary>
    /// <param name="bounds"></param>
    private void ScrollToRectangle(Rectangle bounds)
    {
        int scrollToLeft = bounds.X + bounds.Width + MARGIN;
        if (scrollToLeft > this.Bounds.Width)
        {
            this.ScrollHorizontal(scrollToLeft - this.Bounds.Width);
        }
        else
        {
            int scrollToRight = bounds.X - MARGIN;
            if (scrollToRight < 0)
            {
                this.ScrollHorizontal(scrollToRight);
            }
        }
    }
}

History

  • 30th September, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks Pin
jonbakari26-Apr-14 18:41
jonbakari26-Apr-14 18:41 
GeneralPRECISELY what I needed!!! Pin
leward4-Dec-08 8:35
leward4-Dec-08 8:35 
QuestionWhere is the "MARGIN" defined ? Pin
tal_segal6-Oct-08 20:54
tal_segal6-Oct-08 20:54 
AnswerRe: Where is the "MARGIN" defined ? Pin
M Palmér17-Oct-08 12:21
M Palmér17-Oct-08 12:21 
GeneralThanks. Pin
Ilíon1-Oct-08 3:31
Ilíon1-Oct-08 3:31 
GeneralRe: Thanks. Pin
M Palmér2-Oct-08 10:47
M Palmér2-Oct-08 10:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.