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. Ctrl + MouseClick using PostMessage is not working for DataGridview.

Ctrl + MouseClick using PostMessage is not working for DataGridview.

Scheduled Pinned Locked Moved C#
csharpdatabase
6 Posts 2 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.
  • A Offline
    A Offline
    aks
    wrote on last edited by
    #1

    I have a C# application with a ListView and GridView control.
    I would like to simulate the mouse click in both control simultaneously.
    I would like to work this functionality even if the wind is not foreground.
    So I used PostMessage to do the selection.

        /// /// Select rows.
        /// 
        /// List of rows to be selected.
        /// SelectionType.
        private void PerformSelection( List rows, SelectionType selctionType )
        {
           // timer1.Start();
            IntPtr wParam = MK\_CONTROL;
    
            switch ( selctionType )
            {
                case SelectionType.Ctrl:
                    wParam = MK\_CONTROL;
                    dataGridView1.VirtualKey = Keys.Control;
                    break;
                case SelectionType.Shift:
                    wParam = MK\_SHIFT;
                    dataGridView1.VirtualKey = Keys.Shift;
                    break;
                case SelectionType.CtrlShift:
                    wParam = MK\_CONTROLSHIFT;
                    break;
            }
    
            dataGridView1.Focus();
       
            foreach (int rowIndex in rows)
            {
                SetScrollPositionListView( rowIndex );
                NativeMethods.PostMessage( listView1.Handle, WM\_LBUTTONDOWN, wParam, 
                                          (IntPtr) ( ( listView1.Items\[rowIndex\].Position.Y << 16 ) | listView1.Items\[rowIndex\].Position.X ) );
                NativeMethods.PostMessage( listView1.Handle, WM\_LBUTTONUP, wParam, 
                                           (IntPtr) ( ( listView1.Items\[rowIndex\].Position.Y << 16 ) | listView1.Items\[rowIndex\].Position.X ) );
    
                SetScrollPosition(rowIndex);
    
                dataGridView1.Focus();
                // Get position of dgv row in dgv.
                Rectangle dgvRect = dataGridView1.GetRowDisplayRectangle(rowIndex, true);
    
               
                NativeMethods.PostMessage(dataGridView1.Handle, WM\_LBUTTONDOWN, wParam, (IntPtr)((dgvRect.Top + 1 << 16) | dgvRect.Left + 1));
                NativeMethods.PostMessage(dataGridView1.Handle, WM\_LBUTTONUP, wParam, (IntPtr)((dgvRect.Top + 1 << 16) | dgvRect.Left + 1));
            }
        }
    
        /// /// Scroll to invisible rows.
        /// 
        /// Index of row to be selected.
    
    L 1 Reply Last reply
    0
    • A aks

      I have a C# application with a ListView and GridView control.
      I would like to simulate the mouse click in both control simultaneously.
      I would like to work this functionality even if the wind is not foreground.
      So I used PostMessage to do the selection.

          /// /// Select rows.
          /// 
          /// List of rows to be selected.
          /// SelectionType.
          private void PerformSelection( List rows, SelectionType selctionType )
          {
             // timer1.Start();
              IntPtr wParam = MK\_CONTROL;
      
              switch ( selctionType )
              {
                  case SelectionType.Ctrl:
                      wParam = MK\_CONTROL;
                      dataGridView1.VirtualKey = Keys.Control;
                      break;
                  case SelectionType.Shift:
                      wParam = MK\_SHIFT;
                      dataGridView1.VirtualKey = Keys.Shift;
                      break;
                  case SelectionType.CtrlShift:
                      wParam = MK\_CONTROLSHIFT;
                      break;
              }
      
              dataGridView1.Focus();
         
              foreach (int rowIndex in rows)
              {
                  SetScrollPositionListView( rowIndex );
                  NativeMethods.PostMessage( listView1.Handle, WM\_LBUTTONDOWN, wParam, 
                                            (IntPtr) ( ( listView1.Items\[rowIndex\].Position.Y << 16 ) | listView1.Items\[rowIndex\].Position.X ) );
                  NativeMethods.PostMessage( listView1.Handle, WM\_LBUTTONUP, wParam, 
                                             (IntPtr) ( ( listView1.Items\[rowIndex\].Position.Y << 16 ) | listView1.Items\[rowIndex\].Position.X ) );
      
                  SetScrollPosition(rowIndex);
      
                  dataGridView1.Focus();
                  // Get position of dgv row in dgv.
                  Rectangle dgvRect = dataGridView1.GetRowDisplayRectangle(rowIndex, true);
      
                 
                  NativeMethods.PostMessage(dataGridView1.Handle, WM\_LBUTTONDOWN, wParam, (IntPtr)((dgvRect.Top + 1 << 16) | dgvRect.Left + 1));
                  NativeMethods.PostMessage(dataGridView1.Handle, WM\_LBUTTONUP, wParam, (IntPtr)((dgvRect.Top + 1 << 16) | dgvRect.Left + 1));
              }
          }
      
          /// /// Scroll to invisible rows.
          /// 
          /// Index of row to be selected.
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      From MSDN: "If the function succeeds, the return value is nonzero." What's your return value? And which of the two currently has input-focus? (Receiving keyboard events)?

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      A 1 Reply Last reply
      0
      • L Lost User

        From MSDN: "If the function succeeds, the return value is nonzero." What's your return value? And which of the two currently has input-focus? (Receiving keyboard events)?

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        A Offline
        A Offline
        aks
        wrote on last edited by
        #3

        Yes, the PostMessage is returning true as always. The focus is in datagidview. I have updated my query with an explanatory code snippet. Could you please have a look at that also.

        aks

        L 1 Reply Last reply
        0
        • A aks

          Yes, the PostMessage is returning true as always. The focus is in datagidview. I have updated my query with an explanatory code snippet. Could you please have a look at that also.

          aks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Don't take it the wrong way, but can you verify the setting of the DataGridViews MultiSelect[^] property? Verify that it's set to true? Also, if you have a pointer to the datagridview-object, then why not simply set the collected items using code?

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          A 1 Reply Last reply
          0
          • L Lost User

            Don't take it the wrong way, but can you verify the setting of the DataGridViews MultiSelect[^] property? Verify that it's set to true? Also, if you have a pointer to the datagridview-object, then why not simply set the collected items using code?

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            A Offline
            A Offline
            aks
            wrote on last edited by
            #5

            Yes, I reassured the DataGridView's MultiSelect property is true. From GIO i am able to multi select the rows by using Ctrl and Shift. Thank you Eddy for the suggestion But my intention id an automation interface which works for both ListView and DataGridView. I surely need to simulate the actual user selection by PostMessage.

            aks

            L 1 Reply Last reply
            0
            • A aks

              Yes, I reassured the DataGridView's MultiSelect property is true. From GIO i am able to multi select the rows by using Ctrl and Shift. Thank you Eddy for the suggestion But my intention id an automation interface which works for both ListView and DataGridView. I surely need to simulate the actual user selection by PostMessage.

              aks

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              aks. wrote:

              I reassured the DataGridView's MultiSelect property is true.

              Can you (debug).print the amount of rows selected? Kinda hoping that the selection is there, but not correctly shown (think "HideSelection" properties and the likes).

              aks. wrote:

              I surely need to simulate the actual user selection by PostMessage.

              Not really; one does not need to test whether the DataGridView still handles mouse-messages correctly - that part of the code did not change. And it does not "have" to be using windows-messages, you could use the PerformClick method from a button to simulate a mouseclick. Is there anyone looking at the grid and view when the test-suite makes it selection? Or does the test run without supervision?

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              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