Click here to Skip to main content
Click here to Skip to main content

EnsureVisible for ListViewSubItem

, 30 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Scroll horizontally in a ListView to ensure visibility of a subitem


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


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:

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

        static extern IntPtr SendMessage(IntPtr Handle, Int32 msg, IntPtr wParam,
        IntPtr lParam);

        private void ScrollHorizontal(int pixelsToScroll)
            SendMessage(this.Handle, LVM_SCROLL, (IntPtr)pixelsToScroll,

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

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

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

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


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


  • 30th September, 2008: Initial post


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


About the Author

M Palmér
Software Developer (Senior)
Sweden Sweden
No Biography provided

Comments and Discussions

QuestionThanks Pinmemberjonbakari26-Apr-14 19:41 
GeneralPRECISELY what I needed!!! Pinmemberleward4-Dec-08 9:35 
QuestionWhere is the "MARGIN" defined ? Pinmembertal_segal6-Oct-08 21:54 
AnswerRe: Where is the "MARGIN" defined ? PinmemberM Palmér17-Oct-08 13:21 
GeneralThanks. PinmemberIlíon1-Oct-08 4:31 
GeneralRe: Thanks. PinmemberM Palmér2-Oct-08 11:47 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150331.1 | Last Updated 30 Sep 2008
Article Copyright 2008 by M Palmér
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid