Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Strange order of events in ListView with MultiSelect set to false

Strange order of events in ListView with MultiSelect set to false

Scheduled Pinned Locked Moved C#
questionannouncement
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    crypto_rsa
    wrote on last edited by
    #1

    I noticed a weird behaviour in ListView. When its MultiSelect property is set to true and I press a mouse button while the cursor is inside the control, I get a MouseDown event. Then I release the button and get a MouseUp event, just as I would expect. However, when I set the MultiSelect property to false and press a mouse button, I suddenly get the MouseUp event immediately after the MouseDown one. When I then actually release the button, no MouseUp fires. Is this intentional? How can I then properly determine when the mouse button was actually released?

    RaviBeeR D 3 Replies Last reply
    0
    • C crypto_rsa

      I noticed a weird behaviour in ListView. When its MultiSelect property is set to true and I press a mouse button while the cursor is inside the control, I get a MouseDown event. Then I release the button and get a MouseUp event, just as I would expect. However, when I set the MultiSelect property to false and press a mouse button, I suddenly get the MouseUp event immediately after the MouseDown one. When I then actually release the button, no MouseUp fires. Is this intentional? How can I then properly determine when the mouse button was actually released?

      RaviBeeR Offline
      RaviBeeR Offline
      RaviBee
      wrote on last edited by
      #2

      Can you check the control's MultiSelect property in your mouse event handlers? /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • C crypto_rsa

        I noticed a weird behaviour in ListView. When its MultiSelect property is set to true and I press a mouse button while the cursor is inside the control, I get a MouseDown event. Then I release the button and get a MouseUp event, just as I would expect. However, when I set the MultiSelect property to false and press a mouse button, I suddenly get the MouseUp event immediately after the MouseDown one. When I then actually release the button, no MouseUp fires. Is this intentional? How can I then properly determine when the mouse button was actually released?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        I can confirm what you are experiencing - this only occurs when the control itself is clicked. When a ListViewItem is clicked (MouseDown) the behaviour is normal (even if the mouse is moved away from the clicked item). Very odd. :confused:

        Dave

        If this helped, please vote & accept answer!

        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        C 1 Reply Last reply
        0
        • C crypto_rsa

          I noticed a weird behaviour in ListView. When its MultiSelect property is set to true and I press a mouse button while the cursor is inside the control, I get a MouseDown event. Then I release the button and get a MouseUp event, just as I would expect. However, when I set the MultiSelect property to false and press a mouse button, I suddenly get the MouseUp event immediately after the MouseDown one. When I then actually release the button, no MouseUp fires. Is this intentional? How can I then properly determine when the mouse button was actually released?

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          This is a semi-solution. Subclassing the ListView and overriding WndProc, listen for the left mouse button up message and raise the MouseUp event passing the correct parameters. With a bit more work you may be able to use a flag to allow/disallow MouseUp messages to supress the erroneous one that you are getting.

          public class FixedListView : ListView
          {
              private const int WM\_LBUTTONUP = 0x0202;
          
              protected override void WndProc(ref Message m)
              {
                  if (m.Msg == WM\_LBUTTONUP)
                  {
                      // adjusting as X and Y are 16 bits each of LParam
                      Point point = new Point(((int)m.LParam) & 0x0000FFFF, (int)m.LParam >> 16);
                      OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, point.X, point.Y, 0));
                      return;
                  }
                  base.WndProc(ref m);
              }
          }
          

          WM_LBUTTONUP Message[^]

          Dave

          If this helped, please vote & accept answer!

          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          C 1 Reply Last reply
          0
          • D DaveyM69

            I can confirm what you are experiencing - this only occurs when the control itself is clicked. When a ListViewItem is clicked (MouseDown) the behaviour is normal (even if the mouse is moved away from the clicked item). Very odd. :confused:

            Dave

            If this helped, please vote & accept answer!

            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            C Offline
            C Offline
            crypto_rsa
            wrote on last edited by
            #5

            Yes, that's true, unfortunately I need to properly handle all clicks in the control, not just those on a ListViewItem. It even doesn't work on a ListViewSubItem.

            J 1 Reply Last reply
            0
            • C crypto_rsa

              Yes, that's true, unfortunately I need to properly handle all clicks in the control, not just those on a ListViewItem. It even doesn't work on a ListViewSubItem.

              J Offline
              J Offline
              johannesnestler
              wrote on last edited by
              #6

              It works on sub items if you set FullRowSelect to true... wired bahavior...

              1 Reply Last reply
              0
              • D DaveyM69

                This is a semi-solution. Subclassing the ListView and overriding WndProc, listen for the left mouse button up message and raise the MouseUp event passing the correct parameters. With a bit more work you may be able to use a flag to allow/disallow MouseUp messages to supress the erroneous one that you are getting.

                public class FixedListView : ListView
                {
                    private const int WM\_LBUTTONUP = 0x0202;
                
                    protected override void WndProc(ref Message m)
                    {
                        if (m.Msg == WM\_LBUTTONUP)
                        {
                            // adjusting as X and Y are 16 bits each of LParam
                            Point point = new Point(((int)m.LParam) & 0x0000FFFF, (int)m.LParam >> 16);
                            OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, point.X, point.Y, 0));
                            return;
                        }
                        base.WndProc(ref m);
                    }
                }
                

                WM_LBUTTONUP Message[^]

                Dave

                If this helped, please vote & accept answer!

                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                C Offline
                C Offline
                crypto_rsa
                wrote on last edited by
                #7

                Thanks for the proposed semi-solution. I put together different pieces and came up with this solution which seems to do what I want :) First, I leave the MultiSelect property set to false and set FullRowSelect to true. Then I use this code:

                public class CustomListView : ListView
                {
                public CustomListView()
                {
                InitializeComponent();
                }

                bool fakeMouseUp = false;
                bool mouseDownOnItem = false;
                
                protected override void OnMouseDown( MouseEventArgs e )
                {
                	// if we're not on an item or subitem, we will get OnMouseUp immediately
                	mouseDownOnItem = HitTest( e.Location ).Item != null;
                
                	Debug.WriteLine( "OnMouseDown" );
                
                	base.OnMouseDown( e );
                }
                
                protected override void OnMouseUp( MouseEventArgs e )
                {
                	if( mouseDownOnItem || fakeMouseUp )
                	{
                		Debug.WriteLine( "OnMouseUp" );
                
                		mouseDownOnItem = false;
                		fakeMouseUp = false;
                
                		base.OnMouseUp( e );
                	}
                }
                
                protected override void WndProc( ref Message m )
                {
                	if( !mouseDownOnItem && m.Msg == 0x0202 /\*WM\_LBUTTONUP\*/ )
                	{
                		// send a fake MouseUp event
                		fakeMouseUp = true;
                		Point point = new Point( ((int) m.LParam) & 0x0000FFFF, (int) m.LParam >> 16 );
                		OnMouseUp( new MouseEventArgs( MouseButtons.Left, 1, point.X, point.Y, 0 ) );
                	}
                
                	base.WndProc( ref m );
                }
                

                }

                But I always have a feeling of wasted time when I need to do such workarounds for weird .NET behaviour. Why on Earth do I get a MouseUp event when the mouse button was not physically released? :confused:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups