Menu

Single Cell Selection / Excel Style Selection / MouseUp Event

2015-11-28
2015-11-30
  • LazyCodeMonkey

    LazyCodeMonkey - 2015-11-28

    Hi, I've been using the OLV control for many years now and think it's a great control.

    What I need for my next project is the ability to select a range of individual cells in the "details" view. You may be familiar with cell selection in Excel: that's exactly what I'm after, though my cell selection is slightly simpler as it will be a continuous range only.

    To implement this I have started by creating a new control that inherets ObjectListView. This control has properties for keeping track of the selection: ColIndex start and end, RowIndex start and end, CurrentCell etc...

    I am tracking movement with OnCellOver and want to use OnMouseDown and OnMouseUp to keep track of when the dragging selection begins and ends. When the selection is changed I call BuildList(true) and have code in the CellFormat event to set the background colours of the selected cells. It's not super efficient, but I won't have any more than 500 rows, and a handful of columns, it works well enough.

    However, I have run into problems with the OnMouseUp event since this is fired when the mouse button goes down! I did some research on this forum and found Phil's comments about reporting all mouse clicks as right button. That's fine, I defer to Phil completely for the design decisions he made with this control: ListView sucks, and as we all know, it's hard work to polish a turd.

    So, can anyone point me in a better direction for achieving what I'm after? Are there other events or methods I can override that will allow me to reliably capture the mouse down and up events? I was thinking of hooking the "Hot Cell" tracking, but don't think that will give me mouse down information, not sure; comments?

    Thank you in advance, any assistance is always much appreciated.

    PS: Am working with the latest version 2.9.

     

    Last edit: LazyCodeMonkey 2015-11-28
  • Phillip Piper

    Phillip Piper - 2015-11-29

    As much as I love OLV, I think you may have an up hill struggle to get the behaviour you are after. Too much of ListView's builtin behaviour assumes row selection rather than cell selection.

    However, I still wish you luck :)

    In regards to the mouse up/down events, I can't reproduce the behaviour you are describing. To try, I took the simple tab on the demo and added these lines:

    this.ListView.MouseDown += (sender, args) => Debug.WriteLine("mouse DOWN");
    this.ListView.MouseUp += (sender, args) => Debug.WriteLine("mouse UP");
    

    The DOWN message was generated when the mouse button was down, and the UP message was generated when the mouse was released. I tried with both the left and right button -- the behaviour was consistent. I also tried on a FastObjectListView and it too behaved correctly.

    Are you sure you are using v2.9? v2.8 and v2.8.1 did some "interesting" hacks with mouse events, but v2.9 removed all that.

     
  • LazyCodeMonkey

    LazyCodeMonkey - 2015-11-29

    Phil, mate, thanks for getting back to me. I really thought I was using v2.9, but I'm doubting myself and perhaps now I wasn't! To sanity check myself I created a new project with v2.9 and hooked some event handlers to the MouseUp and MouseDown events. It tested correctly, as you point out above that it should. I then took a second look at my control that inherits ObjectListView where I am using the override keyword to implement my own hooks. For example:

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            StartSelection();
        }
    
        base.OnMouseDown(e);
    }
    

    I can confirm now that MouseUp and MouseDown are working as expected with v2.9.

    Regarding my single cell selection, it's working very well now, with one outstanding item. My motiviation from the start was always to utilise the ObjectListView in a slothful way (yes, I've read all your documentation, and agree it's the way to go) by using the methods and events it exposes. This will also make maintenance easier as I'll be able to drop in the next version without having to worry about breaking changes and putting back in my new code.

    Anyway, what I'm doing is using RowFormat and then calling BuildList(true) to redraw the whole list. This works, and for small numbers of rows (a few hundred) it works fine. However, when I am scrolled down on the list, the scroll bar jumps around like crazy. Note that the table rows do not jump, they are fine, but it's still really annoying.

    What I'd like to know is whether you can suggest a more efficient way to trigger the row formatting events? Will UpdateObject() do the same thing? My idea was that I can keep track of objects as they enter and leave the selected rows and update them accordingly. I suspect that this will be much more efficient, but my preliminary tests show that the row format events aren't called.

    Thanks again for your help, I'll post my class here once I've ironed out these bugs, as it may be of use to others.

     
    • Phillip Piper

      Phillip Piper - 2015-11-30

      Owing to the idiosyncrasies of a virtual list, if you used a
      FastObjectListView, you can just called "Invalidate()" rather than
      "BuildList()" -- which will be much faster and less jumpy


      Phillip Piper phillip.piper@gmail.com
      A man's life does not consist in the abundance of his possessions

      On 30 November 2015 at 04:15, LazyCodeMonkey canorris@users.sf.net wrote:

      Phil, mate, thanks for getting back to me. I really thought I was using
      v2.9, but I'm doubting myself and perhaps now I wasn't! To sanity check
      myself I created a new project with v2.9 and hooked some event handlers to
      the MouseUp and MouseDown events. It tested correctly, as you point out
      above that it should. I then took a second look at my control that inherits
      ObjectListView where I am using the override keyword to implement my own
      hooks. For example:

      protected override void OnMouseDown(MouseEventArgs e)
      {
      if (e.Button == System.Windows.Forms.MouseButtons.Left)
      {
      StartSelection();
      }

      base.OnMouseDown(e);
      

      }

      I can confirm now that MouseUp and MouseDown are working as expected with
      v2.9.

      Regarding my single cell selection, it's working very well now, with one
      outstanding item. My motiviation from the start was always to utilise the
      ObjectListView in a slothful way (yes, I've read all your documentation,
      and agree it's the way to go) by using the methods and events it exposes.
      This will also make maintenance easier as I'll be able to drop in the next
      version without having to worry about breaking changes and putting back in
      my new code.

      Anyway, what I'm doing is using RowFormat and then calling BuildList(true)
      to redraw the whole list. This works, and for small numbers of rows (a few
      hundred) it works fine. However, when I am scrolled down on the list, the
      scroll bar jumps around like crazy. Note that the table rows do not jump,
      they are fine, but it's still really annoying.

      What I'd like to know is whether you can suggest a more efficient way to
      trigger the row formatting events? Will UpdateObject() do the same thing?
      My idea was that I can keep track of objects as they enter and leave the
      selected rows and update them accordingly. I suspect that this will be much
      more efficient, but my preliminary tests show that the row format events
      aren't called.

      Thanks again for your help, I'll post my class here once I've ironed out
      these bugs, as it may be of use to others.


      Single Cell Selection / Excel Style Selection / MouseUp Event
      https://sourceforge.net/p/objectlistview/discussion/812922/thread/75c97974/?limit=25#8112


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/objectlistview/discussion/812922/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       

Log in to post a comment.