Click here to Skip to main content
15,905,781 members
Home / Discussions / C#
   

C#

 
QuestionCtrl + MouseClick using PostMessage is not working for DataGridview. Pin
aks.29-Dec-13 3:03
aks.29-Dec-13 3:03 
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.

C#
/// <summary>
/// Select rows.
/// </summary>
/// <param name="rows">List of rows to be selected.</param>
/// <param name="selctionType">SelectionType.</param>
private void PerformSelection( List<int> 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));
    }
}

/// <summary>
/// Scroll to invisible rows.
/// </summary>
/// <param name="rowIndex">Index of row to be selected.</param>
private void SetScrollPositionListView( int rowIndex )
{
    listView1.EnsureVisible( rowIndex );
}

/// <summary>
/// Get the location of of row to be selected.
/// </summary>
/// <param name="rowIndex">Index of row to be selected.</param>
/// <returns>Row location.</returns>
private Point GetRowLocationListView( int rowIndex )
{
    // Scroll to row position.
    SetScrollPositionListView( rowIndex );

    // Get position of list view with respect t0 desktop.
    Point listViewlocation = listView1.PointToScreen( Point.Empty );

    Rectangle listViewRect = listView1.GetItemRect( rowIndex );

    // Calculate row co ordinate.
    int left = listViewlocation.X + listViewRect.X + ( listViewRect.Width / 2 );
    int right = listViewlocation.Y + listViewRect.Y + ( listViewRect.Height / 2 );
    return new Point( left, right );
}


For ListView the Ctrl + selection working fine. But for datagridview the only single Selection is working, the Ctrl Keydown is not working for datagridview.
When I give 1 2 3 as items to be selcected, the corresponding items selected in lisview, but for datagrid view only the last given item (3) is selected.
Could you please let me know why the above code is not working fro datagrid view.

Note: DataGridView.MulitiSelect property is True
aks


modified 30-Dec-13 3:01am.

AnswerRe: Ctrl + MouseClick using PostMessage is not working for DataGridview. Pin
Eddy Vluggen29-Dec-13 3:30
professionalEddy Vluggen29-Dec-13 3:30 
GeneralRe: Ctrl + MouseClick using PostMessage is not working for DataGridview. Pin
aks.29-Dec-13 20:36
aks.29-Dec-13 20:36 
QuestionRe: Ctrl + MouseClick using PostMessage is not working for DataGridview. Pin
Eddy Vluggen30-Dec-13 2:40
professionalEddy Vluggen30-Dec-13 2:40 
AnswerRe: Ctrl + MouseClick using PostMessage is not working for DataGridview. Pin
aks.30-Dec-13 3:39
aks.30-Dec-13 3:39 
GeneralRe: Ctrl + MouseClick using PostMessage is not working for DataGridview. Pin
Eddy Vluggen2-Jan-14 8:00
professionalEddy Vluggen2-Jan-14 8:00 
QuestionItemCommand(Repeater) not getting fired with modalpopup. Pin
Kundan Saw28-Dec-13 4:08
Kundan Saw28-Dec-13 4:08 
AnswerRe: ItemCommand(Repeater) not getting fired with modalpopup. Pin
Eddy Vluggen29-Dec-13 2:01
professionalEddy Vluggen29-Dec-13 2:01 
GeneralRe: ItemCommand(Repeater) not getting fired with modalpopup. Pin
Kundan Saw29-Dec-13 6:26
Kundan Saw29-Dec-13 6:26 
QuestionThe problem of axShockwaveFlash controls Pin
toodfu28-Dec-13 2:09
toodfu28-Dec-13 2:09 
AnswerRe: The problem of axShockwaveFlash controls Pin
Eddy Vluggen29-Dec-13 2:04
professionalEddy Vluggen29-Dec-13 2:04 
GeneralRe: The problem of axShockwaveFlash controls Pin
toodfu29-Dec-13 15:27
toodfu29-Dec-13 15:27 
GeneralRe: The problem of axShockwaveFlash controls Pin
Eddy Vluggen30-Dec-13 2:42
professionalEddy Vluggen30-Dec-13 2:42 
QuestionCannot Convert Table<T> to Table<object> Pin
talg1828-Dec-13 0:35
talg1828-Dec-13 0:35 
AnswerRe: Cannot Convert Table<T> to Table<object> Pin
OriginalGriff28-Dec-13 2:19
mveOriginalGriff28-Dec-13 2:19 
GeneralRe: Cannot Convert Table<T> to Table<object> Pin
talg1828-Dec-13 2:35
talg1828-Dec-13 2:35 
GeneralRe: Cannot Convert Table<T> to Table<object> Pin
OriginalGriff28-Dec-13 3:20
mveOriginalGriff28-Dec-13 3:20 
QuestionHow configure a virtual printer port to redirect it to a external program Pin
delphix527-Dec-13 22:47
delphix527-Dec-13 22:47 
AnswerRe: How configure a virtual printer port to redirect it to a external program Pin
Richard MacCutchan28-Dec-13 0:37
mveRichard MacCutchan28-Dec-13 0:37 
GeneralRe: How configure a virtual printer port to redirect it to a external program Pin
delphix528-Dec-13 1:15
delphix528-Dec-13 1:15 
GeneralRe: How configure a virtual printer port to redirect it to a external program Pin
Richard MacCutchan28-Dec-13 1:37
mveRichard MacCutchan28-Dec-13 1:37 
GeneralRe: How configure a virtual printer port to redirect it to a external program Pin
delphix528-Dec-13 1:44
delphix528-Dec-13 1:44 
GeneralRe: How configure a virtual printer port to redirect it to a external program Pin
Richard MacCutchan28-Dec-13 2:20
mveRichard MacCutchan28-Dec-13 2:20 
Questiondisplay image on crytal report Pin
Member 990020327-Dec-13 18:10
Member 990020327-Dec-13 18:10 
AnswerRe: display image on crytal report Pin
Eddy Vluggen29-Dec-13 2:08
professionalEddy Vluggen29-Dec-13 2:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.