Click here to Skip to main content
Licence CPOL
First Posted 30 Sep 2008
Views 21,928
Downloads 199
Bookmarked 25 times

EnsureVisible for ListViewSubItem

By | 30 Sep 2008 | Article
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:

        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:

        /// <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)

About the Author

M Palmér

Software Developer (Senior)

Sweden Sweden

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPRECISELY what I needed!!! Pinmemberleward8:35 4 Dec '08  
QuestionWhere is the "MARGIN" defined ? Pinmembertal_segal20:54 6 Oct '08  
AnswerRe: Where is the "MARGIN" defined ? PinmemberM Palmér12:21 17 Oct '08  
GeneralThanks. PinmemberIlíon3:31 1 Oct '08  
GeneralRe: Thanks. PinmemberM Palmér10:47 2 Oct '08  

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.

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