Getting click events for a ListView in the Compact Framework.

We’re going to be adding a full set of custom-drawn and owner-drawn controls in the next version of the SDF.  These are not “new” controls, but wrappers around the existing native control, but we’re adding the custom and owner draw hooks that the OS already provides (and that, honestly, the CF team should have given us by version 3.5). 

In testing them out, I wanted to dogfood the custom-drawn ListView in an application, but I needed to know when a user clicked on an item in the ListView.  Simple.  Or so I thought.  It turns out that the style bits that get set to make a ListView really usable means that the ListView’s *parent* gets it’s click actions, and to turn those into Click events in the managed control is a *lot* of work, and would likely have some performance penalty.

So that got me to wondering how the existing CF ListView deals with it.  Well after a very quick test it seems that the CF team made the same decision – they didn’t support the Click event either

Well my application requires that I know the difference between when the selected index changes (that’s about the only useful event you do get) due a click and and when it changes dues to a keyboard action like an up or down arrow. Being me, I refuse to change the way I want my application will work just becasue of some stupid limitation of a framework.  The OS knows when I click on that damned thing – after all, it changes the selected item – so it *will* tell my application when it happens.

The key here is pretty simple.  We know that the click event does come in, and it comes in as a Windows message.  Our application message pump routes it to the parent of the ListView – so we have two point at which we can try to get it.  Getting it from the parent would entail subclassing the parent Form, and that just does seem like fun, nor is it really extensible if I ever have another app where I want to get ListView item clicks.

So that leaves looking at the application message pump.  Unfortunately the CF team has again not seen fit in any version to provide us the ability to add an IMessageFilter, but this was something we overcame long ago in the SDF.  If you use the Application2.Run method, you can then add IMessageFilters.  So what I did here is created an IMessageFilter that looks for messages whose hWnd matches the ListView I’m interested in, and then looks for the WM_LBUTTONDOWN message:

internal class ClickFilter : IMessageFilter
  private Control m_control;

  public event EventHandler Click;
  public event MouseEventHandler MouseDown;

  public ClickFilter(Control control)
    m_control = control;

  private int LoWord(IntPtr param)
    return (ushort)(param.ToInt32() & ushort.MaxValue);

  private int HiWord(IntPtr param)
    return (ushort)(param.ToInt32() >> 16);

  public bool PreFilterMessage(ref Microsoft.WindowsCE.Forms.Message m)
    if (m_control.IsDisposed) return false;

    if ((m.HWnd == m_control.Handle) && (m.Msg == (int)Microsoft.Controls.WM.WM_LMOUSEDOWN))
      MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 1, LoWord(m.LParam), HiWord(m.LParam), 0);

      ThreadPool.QueueUserWorkItem(ButtonInvoker, args);

    return false;

  void ButtonInvoker(object o)
    // let the system select the clicked listview item

    if (m_control.IsDisposed) return;

    if (m_control.InvokeRequired)
      m_control.Invoke(new WaitCallback(ButtonInvoker), new object[] { o });

    // not truly a click, since it’s not a down/up pair. Fix this later if we feel like it
    if(Click != null) Click(m_control, null);
    if (MouseDown != null) MouseDown(m_control, o as MouseEventArgs);

Take note of the slight kludge in there though with the ThreadPool.  The reason for this is that if you just raise the event immediately on getting the mouse down event, the ListViewItem selection won’t have happened yet, so if your handler looks at the SelectedItem, it would get the item that was selected before the click, not the one that the was clicked. 

Sure, maybe that’s what your app wants, but in my case I wanted to know which item is actually clicked.  The ListView doesn’t support a HitTest method (again, why don’t we have this yet?), so knowing the x,y coordinates of the click is still a long way from giving us the ListViewItem.  This was a kludge to easily get me the info I wanted.

So using this filter in my application was as easy as adding this to the Form that contains the ListView:


ClickFilter filter = new ClickFilter(listView);
filter.Click += new EventHandler(filter_Click);

No, it shouldn’t be this hard.  I wish the CF team would spend more time fixing fundamental stuff like this instead of tilting at the Silverlight windmill, but it is what it is, and as developers we still have to ship solutions.

4 thoughts on “Getting click events for a ListView in the Compact Framework.”

  1. Hi,
    Instead of doing all that, can’t you just use the "this.listView.ItemActivate" event and set "this.listView.Activation = System.Windows.Forms.ItemActivation.OneClick;" ?


  2. @Winny: No. I want to be able to select an item via the keyboard or via the mouse, but I need to be able to tell the difference. An item change due to the keyboard does nothing, ‘Enter’ selects, but a click selects the item you clicked on.

    @gulnor: some days, yes I do.


  3. Well, I am not talking about the ‘SelectedIndexChanged’ event which will be fired if you use the keyboard (up and down).
    The "ItemActivate" event will be fired only for the "Activation" condition: only for a "OneClick", so I guess it is what you need (but I may be doing a mistake, and you cannot get the case when the user is clicking somewhere on the listView, out of any item).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s