Page 1 of 1

Enter key in a grid

Posted: Tue Nov 16, 2021 7:39 pm
by ROBROS
Hi friends,

I have created a grid, the data (names) are stored in an array with just one column. Navigating up and down the rows can be simply done. To find out the CellRowIndex I use nRow:=This.CellRowIndex. Works fine with OnDblClick.
If I use the Enter key I always get a CellRowIndex of 0 (zero) and of course an array access error .In clipper the first index of an array seems to be 1 and not 0 like in "C". Set navigation extended is on. In grid control onenter does not work. Is there any workaround for this?
Maybe I can try with Listbox.

TIA
Robert

Re: Enter key in a grid

Posted: Tue Nov 16, 2021 9:06 pm
by AUGE_OHR
hi,

GRID have "itself"

Code: Select all

ONKEY
---

about DblClick
The following data are available at ‘OnDblClick’ procedure:

This ...

Note: These properties are not available when OnDblClick procedure is fired by <Enter> key pressing.

Re: Enter key in a grid

Posted: Tue Nov 16, 2021 9:25 pm
by ROBROS
Hi Jimmy, thanks for your hint, had not found onkey-event.

Robert

Re: Enter key in a grid

Posted: Wed Nov 17, 2021 6:25 am
by nekbmm
Maybe this can help you:

Code: Select all

  ON KEY RETURN OF Form_1 ACTION (...)
Nex.

Re: Enter key in a grid

Posted: Wed Nov 17, 2021 8:14 am
by Claudio Ricardo
Hi... i use:

Code: Select all

LOCAL nPos := GetProperty ("Form_Name" , "Grid_Name" , "Value")
LOCAL aArray := GetProperty ("Form_Name" , "Grid_Name" , "Item" , nPos)
LOCAL xCol_1, xCol_2

xCol_1 := aArray [1]
xCol_2 := aArray [2]   // etc....
aArray is unidimentional and contain all row selected values...
Work with doubleclick or enter over highlight row.

Re: Enter key in a grid

Posted: Wed Nov 17, 2021 8:48 am
by AUGE_OHR
hi,

forgot to say that it work with "virtual-Key"

Code: Select all

        GRID ...
        ON KEY DoGridKey1( cForm, cObj )

Code: Select all

PROCEDURE DoGridKey1( cForm, cObj )
LOCAL xKey   := HMG_GetLastVirtualKeyDown()

      DO CASE
         CASE xKey == VK_F1 .AND. .NOT. IsKeyVK()
            HelpFile()
         CASE xKey == VK_T .AND. HMG_VirtualKeyIsPressed( VK_CONTROL )
            NewTab()
         CASE xKey == VK_U .AND. HMG_VirtualKeyIsPressed( VK_CONTROL )
            ChangeLeftRight()
...
         CASE xKey == VK_RETURN
            // it IS include as DblClick
            // DoDblClick()
also have a look at
X:\hmg.3.4.4\SAMPLES\Controls\Grid\GridIncrementalSearch

Re: Enter key in a grid

Posted: Wed Nov 17, 2021 8:58 am
by edk
ROBROS wrote: Tue Nov 16, 2021 7:39 pm Hi friends,

I have created a grid, the data (names) are stored in an array with just one column. Navigating up and down the rows can be simply done. To find out the CellRowIndex I use nRow:=This.CellRowIndex. Works fine with OnDblClick.
If I use the Enter key I always get a CellRowIndex of 0 (zero) and of course an array access error .In clipper the first index of an array seems to be 1 and not 0 like in "C". Set navigation extended is on. In grid control onenter does not work. Is there any workaround for this?
Maybe I can try with Listbox.

TIA
Robert
Hi Robert.
Try to use

Code: Select all

nRow := This.CellRowFocused
instead of

Code: Select all

nRow := This.CellRowIndex
Look at this sample:

Code: Select all

#include "hmg.ch"

Function Main
Local i, j, xSel
Local aMultiArray:={1 , 2 , 'string' , Nil , { 5, 55, 555 }, .T. , { 7 ,  { '7a' , '7b' ,  { '7c' ,  {  77.77 , 777.777 } } } , 777 } , 8 }
Local aSingleArray:={1,2,3,4,5,6,7,8,9}
Local xNOTArray:= 1
Local aEmptyArray:={}
Local aTwoDimArray:=Array(10,10)

FOR i = 1 TO LEN( aTwoDimArray )
	FOR j = 1 TO LEN( aTwoDimArray [i] )
		aTwoDimArray [i][j] := ((i - 1) * 10) + j - 1
	NEXT j
NEXT i

DEFINE WINDOW Form_1 ;
	AT 0,0 ;
	WIDTH 450 ;
	HEIGHT 400 ;
	TITLE 'Hello' ;
	MAIN 

	DEFINE MAIN MENU
		DEFINE POPUP 'Matrix Browse'
			MENUITEM 'Single' ACTION ( xSel := MatrixBrowse( aSingleArray ), MsgBox ('Selected value ' + hb_valToStr( xSel )) )
			MENUITEM 'Two-dimensional array' ACTION ( xSel := MatrixBrowse( aTwoDimArray ), MsgBox ('Selected value ' + hb_valToStr( xSel )) )
			MENUITEM 'Multi-dimensional array' ACTION ( xSel := MatrixBrowse( aMultiArray ), MsgBox ('Selected value ' + hb_valToStr( xSel )) )
			MENUITEM 'Empty array' ACTION ( xSel := MatrixBrowse( aEmptyArray ), MsgBox ('Selected value ' + hb_valToStr( xSel )) )
			MENUITEM 'Not an Array' ACTION ( xSel := MatrixBrowse( xNOTArray ), MsgBox ('Selected value ' + hb_valToStr( xSel )) )
		END POPUP
	END MENU

END WINDOW

CENTER WINDOW Form_1

ACTIVATE WINDOW Form_1

Return

*******************************************************************************
Function MatrixBrowse ( aAnyArray )
Local nItems, aColumns, cFormName
Local nFormNo := 1
Public _xMatrixValue

IF .NOT. HB_isArray( aAnyArray )
	MsgStop ("The variable is not an array.")
	RETURN 
ENDIF

nItems := 1
FOR EACH _xMatrixValue IN aAnyArray
	IF hb_isArray( _xMatrixValue )
		nItems := MAX ( nItems, LEN( _xMatrixValue ) )  
	ENDIF
NEXT

_xMatrixValue := Nil

aColumns := Array ( nItems )
AEval( aColumns, { | x, y | aColumns [ y ] := "Column " + Alltrim(Str( y )) })

DO WHILE .T.
	cFormName := "_MatrixForm_" + Alltrim( Str ( nFormNo ) )
	IF !_IsWindowDefined ( cFormName )
		EXIT
	ENDIF
	nFormNo ++
ENDDO

DEFINE WINDOW &cFormName ;
	AT 0,0 ;
	WIDTH nItems * 100 + 50 ;
	HEIGHT MIN ( LEN( aAnyArray ) * 25 + 100, GetDesktopHeight() ) ;
	TITLE 'Matrix browse #' + Alltrim( Str ( nFormNo ) ) ;
	MODAL

	@ 5,5 GRID Grid_1 ;
	WIDTH nItems * 100 + 20 ;
	HEIGHT MIN ( LEN( aAnyArray ) * 25 + 20, GetDesktopHeight() - 80) ;
	HEADERS aColumns ;
	WIDTHS AFill ( Array (nItems), 100 );
	DYNAMICFORECOLOR AFill ( Array (nItems), { || if ( hb_isString(This.CellValue) .AND. This.CellValue = "{ SubArray }" , RED, BLACK )  } ) ;
	VIRTUAL ;
	ITEMCOUNT LEN( aAnyArray ) ;
	ON QUERYDATA QueryArray ( aAnyArray ) ;
	CELLNAVIGATION ;
	VALUE { 1, 1 } ;
	ON DBLCLICK OnKeyMatrixBrowse( aAnyArray, cFormName ) ;
	ON KEY OnKeyMatrixBrowse( aAnyArray, cFormName ) 
    
END WINDOW

CENTER WINDOW &cFormName

ACTIVATE WINDOW &cFormName

Return _xMatrixValue

*******************************************
Function OnKeyMatrixBrowse ( aArray, cFormName )
Local nRow := GetProperty(cFormName, 'Grid_1', 'CellRowFocused' )
Local nCol := GetProperty(cFormName, 'Grid_1', 'CellColFocused' )
Local lEnter := HMG_GetLastVirtualKeyDown() == VK_RETURN .OR. HMG_GetLastMouseMessage() == WM_LBUTTONDBLCLK
HMG_CleanLastVirtualKeyDown()
HMG_CleanLastMouseMessage()

IF lEnter
	IF GetProperty( cFormName, 'Grid_1', 'CellEx', nRow, nCol ) == '{ SubArray }'
		//show sub-Array
		MatrixBrowse( aArray [nRow] [nCol] )
	ELSE
		//Quit
		IF LEN( aArray ) > 0
			IF hb_isArray( aArray [nRow] )
				_xMatrixValue := aArray [nRow] [nCol]
			ELSEIF nCol == 1
				_xMatrixValue := aArray [nRow]
			ENDIF
		ELSE
			_xMatrixValue := Nil
		ENDIF
		DoMethod (cFormName, 'Release')
	ENDIF
ENDIF
   
Return NIL
******************************************
Procedure QueryArray( aArray )
	
	IF !hb_isArray ( aArray [This.QueryRowIndex] )
	
		IF This.QueryColIndex == 1
			This.QueryData := hb_ValToStr( aArray [This.QueryRowIndex] )
		ELSE
			This.QueryData := ""
		ENDIF
		
	ELSEIF hb_isArray ( aArray [This.QueryRowIndex] [This.QueryColIndex] )
	
		This.QueryData := "{ SubArray }"
		
	ELSE	
	
		This.QueryData := hb_ValToStr( aArray [This.QueryRowIndex] [This.QueryColIndex] )
		
	ENDIF

Return

Re: Enter key in a grid

Posted: Wed Nov 17, 2021 10:12 am
by ROBROS
Thanks for all your suggestions.

I will try.
Robert