| xHarbour Reference Documentation > Class Reference (textmode) |
![]() |
![]() |
![]() |
Creates a new TBrowse object.
TBrowse():new( [<nTop>] , ;
[<nLeft>] , ;
[<nBottom>], ;
[<nRight>] ) --> oTBrowse
Function TBrowse() returns a new TBrowse object and method :new() initializes the object.
TBrowse objects are used in text mode applications to display tabular data organized in rows and columns in a rectangular area on the screen. Common data sources for TBrowse objects are database files or arrays. In order to display such data, a TBrowse object must receive objects of the TBColumn() class which contain all information required for displaying a single column in a browse view. In addition, a TBrowse object must be assigned code blocks required for navigating the row pointer of the data source as a response to user input. The example below demonstrates all steps required to create a fully functional browse view with TBrowse and TBColumn objects.
| See also: | DbSkipper(), SetColor(), TBColumn(), TBrowseDb(), TBrowseNew(), TBMouse() |
| Category: | Object functions |
| Header: | tbrowse.ch |
| Source: | rtl\tbrowse.prg |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// The example demonstrates the steps required for creating a
// browse view for a database file.
#include "TBrowse.ch"
PROCEDURE Main
LOCAL oTBrowse, oTBColumn
LOCAL bFieldBlock, cFieldName, i, nKey
USE Customer ALIAS Cust
// create TBrowse object
oTBrowse := TBrowse():new( 2,2, MaxRow()-2, MaxCol()-2 )
oTBrowse:headSep := "-"
oTBrowse:colorSpec := "N/BG,W+/R"
// add code blocks for navigating the record pointer
oTBrowse:goTopBlock := {|| DbGoTop() }
oTBrowse:goBottomBlock := {|| DbGoBottom() }
oTBrowse:skipBlock := {|nSkip| DbSkipper(nSkip) }
// create TBColumn objects and add them to TBrowse object
FOR i:=1 TO FCount()
cFieldName := FieldName( i )
bFieldBlock := FieldBlock( cFieldName )
oTBColumn := TBColumn():new( cFieldName, bFieldBlock )
oTBrowse:addColumn( oTBColumn )
NEXT
// display browser and process user input
DO WHILE .T.
oTBrowse:forceStable()
nKey := Inkey(0)
IF oTBrowse:applyKey( nKey ) == TBR_EXIT
EXIT
ENDIF
ENDDO
USE
RETURN
// The example demonstrates the steps required for creating a
// browse view for a two dimensional array. Note that the data
// source and row pointer of the data source are stored in
// oTBrowse:cargo. The pseudo instance variables :data and :recno
// are translated by the preprocessor.
#include "TBrowse.ch"
#xtrans :data => :cargo\[1]
#xtrans :recno => :cargo\[2]
PROCEDURE Main
LOCAL i, nKey, bBlock, oTBrowse, oTBColumn
LOCAL aHeading := { "File Name", ;
"File Size", ;
"File Date", ;
"File Time", ;
"File Attr" }
LOCAL aWidth := { 20, 10, 9, 9, 9 }
// Create TBrowse object
// data source is the Directory() array
oTBrowse := TBrowse():new( 2, 2, MaxRow()-2, MaxCol()-2 )
oTBrowse:cargo := { Directory( "*.*" ), 1 }
oTBrowse:headSep := "-"
oTBrowse:colorSpec := "N/BG,W+/R"
// Navigation code blocks for array
oTBrowse:goTopBlock := {|| oTBrowse:recno := 1 }
oTBrowse:goBottomBlock := {|| oTBrowse:recno := Len( oTBrowse:data ) }
oTBrowse:skipBlock := {|nSkip| ArraySkipper( nSkip, oTBrowse ) }
// create TBColumn objects and add them to TBrowse object
FOR i:=1 TO Len( aHeading )
// code block for individual columns of the array
bBlock := ArrayBlock( oTBrowse, i )
oTBColumn := TBColumn():new( aHeading[i], bBlock )
oTBColumn:width := aWidth[i]
oTBrowse:addColumn( oTBColumn )
NEXT
// display browser and process user input
DO WHILE .T.
oTBrowse:forceStable()
nKey := Inkey(0)
IF oTBrowse:applyKey( nKey ) == TBR_EXIT
EXIT
ENDIF
ENDDO
RETURN
// This code block uses detached LOCAL variables to
// access single elements of a two-dimensional array.
FUNCTION ArrayBlock( oTBrowse, nSubScript )
RETURN {|| oTBrowse:data[ oTBrowse:recno, nSubScript ] }
// This function navigates the row pointer of the
// the data source (array)
FUNCTION ArraySkipper( nSkipRequest, oTBrowse )
LOCAL nSkipped
LOCAL nLastRec := Len( oTBrowse:data ) // Length of array
IF oTBrowse:recno + nSkipRequest < 1
// skip requested that navigates past first array element
nSkipped := 1 - oTBrowse:recno
ELSEIF oTBrowse:recno + nSkipRequest > nLastRec
// skip requested that navigates past last array element
nSkipped := nLastRec - oTBrowse:recno
ELSE
// skip requested that navigates within array
nSkipped := nSkipRequest
ENDIF
// adjust row pointer
oTBrowse:recno += nSkipped
// tell TBrowse how many rows are actually skipped.
RETURN nSkipped
http://www.xHarbour.com