Detect right click in ListView ColumnHeader

Posted on 2006-05-19
Last Modified: 2008-01-09
I would like to detect a right click on a ListView ColumnHeader with the ultimate goal of showing a context menu showing a checklist of the columns and allowing the user to add or remove visible columns from the ListView.

The ListView ColumnClick event only responds to left click.

What can I do? - Searched all over and have found only scant references - seems as though an API call may be necessary.  Could I avoid with some sort of hittest?

Any example code or links would be helpful.


Question by:rmirabelle
    LVL 10

    Accepted Solution

    try the sample code in the following link

    That article have an sample to handle double click event on the header control.
    Change the wndproc method of HeaderControl class to the following
    protected override void WndProc(ref Message message)
          const int WM_LBUTTONDBLCLK = 0x0203;
          const int WM_SETCURSOR = 0x0020;
          const int WM_RBUTTONUP = 0x0205;
          bool callBase = true;

          switch ( message.Msg )
          case WM_LBUTTONDBLCLK:                    
          case WM_SETCURSOR:
                      //Don't change cursor to sizing cursor.  Also ignore
                      //double click, which sizes the column to fit the data.
                      message.Result = (IntPtr)1;      //Return TRUE from message handler
                      callBase = false;            //Don't call the base class.
                } //if
                case WM_RBUTTONUP:
                      MessageBox.Show("Right click");            //Right button Click
          } //switch

                // pass messages on to the base control for processing
                base.WndProc( ref message ) ;
          } //if
    } //WndProc()


    LVL 1

    Author Comment

    I'm surprised that I don't find readily available a ListView with column right click functionality - guess I'll have to write and submit one!
    Thanks, you pointed me in the right direction.
    LVL 10

    Expert Comment

    :). thanks  rmirabelle

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now