Multi-row select for Grid control
From DataFlex Wiki
(Redirected from Muli-row select for Grid control)
Functionality
This code demonstrates how to select multiple rows in a Grid. The code will work no matter how many columns. The object of this code is to allow the user to use the SHIFT+RCLICK or CTRL+RCLICK key to select multiple rows just like a spreadsheet program.
Example
Paste this code into a test view to test it and see how it works. Once compiled, you will see a narrow column on the left that acts as a margin. Clicking this gray cell will highlight the row. Using SHIFT or CTRL and clicking another gray cell will select multiple rows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | Object oGrid1 is a Grid Set Location to 7 26 Set Size to 116 207 Set Line_Width to 4 0 Set Form_Width 0 to 8 Set Header_Label 0 to "" Set Form_Width 1 to 60 Set Header_Label 1 to "Column 1" Set Form_Width 2 to 60 Set Header_Label 2 to "Column 2" Set Form_Width 3 to 60 Set Header_Label 3 to "Column 3" // BEGIN MULTI-SELECT CODE // DO NOT MODIFY Set Select_Mode to Multi_Select Property Integer [] paiSelected Property Integer piLastSelected 0 Procedure Select_Toggling Integer iItem Integer iState Integer iCurrent iCol iLast iCount iFrom iTo iRet iUpdate Integer [] aiSelected aiEmpty // Get Dynamic_Update_State to iUpdate Set Dynamic_Update_State to False // Get base item Move (Current_Item( Self )) to iCurrent Move (Mod(iCurrent , Line_Size( Self ))) to iCol Subtract iCol from iCurrent // get list of selected items Move (paiSelected( Self )) to aiSelected Move (SizeOfArray(aiSelected)) to iCount // Move (GetAsyncKeyState(VK_CONTROL)) to iRet If (iRet<> 0 ) Begin If (iState) Begin // add selected item Move iCurrent to aiSelected[iCount] // Update last selected with this item Set piLastSelected to iCurrent // Select Current row For iCol from 0 to (Line_Size( Self )- 1 ) Forward Send Select_Toggling (iCurrent+iCol) iState Loop End Else Begin // remove selected item For iItem from 0 to (iCount- 1 ) Move aiSelected[iItem] to iLast If (iLast=iCurrent) Move - 1 to aiSelected[iItem] Loop // Update last selected with this item Set piLastSelected to - 1 End End Else Begin // If shift is used, we will ignore // the state of the clicked row // unselect everything For iItem from 0 to (iCount- 1 ) Move aiSelected[iItem] to iLast For iCol from 0 to (Line_Size( Self )- 1 ) Forward Send Select_Toggling (iLast+iCol) False Loop Loop Move aiEmpty to aiSelected // Move (GetAsyncKeyState(VK_SHIFT)) to iRet If ((iRet<> 0 ) and (piLastSelected( Self )<>- 1 )) Begin // select everything in between // do not mark this as the last current row, // the first item of the shift remains If (iCurrent > piLastSelected( Self )) Begin Move (piLastSelected( Self )) to iFrom Move iCurrent to iTo End Else Begin Move iCurrent to iFrom Move (piLastSelected( Self )) to iTo End Move 0 to iCount For iItem from iFrom to (iTo+ 3 ) // Add to array Move iItem to aiSelected[iCount] Increment iCount // select For iCol from 0 to (Line_Size( Self )- 1 ) Forward Send Select_Toggling (iItem+iCol) True Loop // next row Add 3 to iItem Loop End Else Begin // no shift or control key was pressed // Update last selected with this item Move iCurrent to aiSelected[ 0 ] Set piLastSelected to iCurrent // Select Current item For iCol from 0 to (Line_Size( Self )- 1 ) Forward Send Select_Toggling (iCurrent+iCol) iState Loop End End Set Dynamic_Update_State to iUpdate Set paiSelected to aiSelected End_Procedure // Select_Toggling Procedure Mouse_Up Integer iWindowNumber Integer iPosition Integer iCol Move (Mod(iWindowNumber , Line_Size( Self ))) to iCol Forward Send Mouse_Up iWindowNumber iPosition If (iCol<> 1 ) Send Select_Toggling 0 False End_Procedure // Mouse_Up // END MULTI-SELECT CODE //Sample method of how to fill a grid Procedure DoFillGrid Integer iRow iMaxRows Move 20 to iMaxRows For iRow from 0 to iMaxRows Send Add_Item Msg_None "" Send Add_Item Msg_None ( "Col 1, row " + String (iRow)) Send Add_Item Msg_None ( "Col 2, row " + String (iRow)) Send Add_Item Msg_None ( "Col 3, row " + String (iRow)) Set Entry_State item (iRow* 4 ) to False Set ItemColor item (iRow* 4 ) to clBtnFace Loop End_Procedure // DoFillgrid Send DoFillgrid End_Object // oGrid1 Object oButton1 is a Button Set Size to 14 70 Set Location to 151 44 Set Label to "Show Selection" // Example on how to get selected rows Procedure OnClick Integer [] aiSelected Integer iItem iCount Get paiSelected of oGrid1 to aiSelected Move (SizeOfArray(aiSelected)) to iCount Showln "Selected Row base_item(s) are" For iItem from 0 to (iCount- 1 ) Showln (Character( 9 )) aiSelected[iItem] Loop End_Procedure // OnClick End_Object // oButton1 |