Rapid-Q Documentation by William Yu (c)1999-2000 Appendix A: QSTRINGGRID

QSTRINGGRID Component

QStringGrid allows you to present textual data in a tabular format, like cells in a spreadsheet program.

QStringGrid Properties
FieldTypeR/WDefault




AlignINTEGERRWalNone

Align determines how the control aligns within its parent control.
BorderStyleINTEGERRWbsSingle
Cell2D ARRAY of STRINGRW

Cell(X, Y) is an array of strings, one string for each cell in the grid.
X = Column, Y = Row
ColINTEGERRW

Col is the index of the column that contains the selected cell.
ColCountINTEGERRW5
ColorINTEGERRW
ColumnListARRAY of STRINGRW

ColumnList maintains a list of strings deliminated by LF for use in Columned lists.
ColumnStyleARRAY of INTEGERRWgcsNone

ColumnStyle determines the style of each column.
0 = gcsList -- Column with a drop-down list for each item
1 = gcsEllipsis -- Column with an ellipsis button for each item
2 = gcsNone -- Normal column
ColWidthsARRAY of INTEGERRW

ColWidths is an indexed array giving the width in pixels of all the columns in the grid.
CursorINTEGERRWcrDefault
DefaultColWidthINTEGERRW64
DefaultRowHeightINTEGERRW24
EditorModeINTEGERRW
EnabledINTEGERRWTrue
FixedColorINTEGERRW
FixedColsINTEGERRW1

FixedColss is the number of columns on the left of the grid that cannot be scrolled.
FixedRowsINTEGERRW1

FixedRows is the number of rows on the top of the grid that cannot be scrolled.
FontQFONTW
GridHeightINTEGERRW
GridLineWidthINTEGERRW1
GridWidthINTEGERRW
HandleINTEGERR
HeightINTEGERRW
HintSTRINGRW
LeftINTEGERRW0
LeftColINTEGERRW
ParentQFORM/QPANEL/QTABCONTROLW
PopupMenuQPOPUPMENUW
RowINTEGERRW

Row is the index of the row that contains the selected cell.
RowCountINTEGERRW5
RowHeightsARRAY of INTEGERRW

RowHeights is an indexed array giving the height in pixels of all the rows in the grid.
ScrollBarsINTEGERRWssBoth
SeparatorSTRINGRW

Separator determines how the stringgrid should parse (ie. read/write) grid data. For example, you can save the grid data to a file, with each item deliminated by the Separator.
ShowHintINTEGERRWFalse
TabOrderINTEGERRW
TabStopsARRAY of INTEGERRW
TagINTEGERRW
TopINTEGERRW0
TopRowINTEGERRW
VisibleINTEGERRWTrue
VisibleColCountINTEGERR
VisibleRowCountINTEGERR
WidthINTEGERRW


QStringGrid Methods
MethodType Description Params
AddOptions SUBI Add options to string gridINTEGER, Infinite
goFixedVertLine = 0       horizontal lines appear between the rows within nonscrolling regions.
goFixedHorzLine = 1      vertical lines appear between the columns within nonscrolling regions.
goVertLine = 2               vertical lines appear between the columns
goHorzLine = 3              horizontal lines appear between the rows.
goRangeSelect = 4         the user can select a range of cells at one time. When goEditing is True, the user can no longer select a range of cells.
goDrawFocusSelected = 5    the cell with the focus is colored 'the same as other cells in a selected block are colored. When False, the cell with the focus remains the color of all unselected cells, the color specified with the grid Color property.

goRowSizing = 6         rows can be resized individually except for fixed or nonscrolling rows.
goColSizing = 7          columns can be resized individually except for fixed or nonscrolling columns.
goRowMoving = 8     the user can move a row to a new location in the grid using the mouse.
goColMoving = 9       the user can move a column to a new location in the grid using the mouse.
goEditing = 10           the user can edit the text in the grid. When goEditing is True, the user cannot select a range of cells at one time.
goTabs = 11              the user can use the Tab and Shift-Tab keys to move from column to column in the grid.
goRowSelect = 12     the user can only whole rows at a time instead of individual cells.

goAlwaysShowEditor = 13   the grid is in automatic edit mode 'if goEditing is also True. When the grid is in automatic edit mode, the user does not have to press Enter or F2 before editing the contents of a cell. When goAlwaysShowEditor is False and goEditing is True, the user must press Enter or F2 before editing the contents of a cell. If goEditing is False, setting goAlwaysShowEditor to True has no effect.

goThumbTracking = 14  the contents of the grid scrolls while the user is moving the thumb tab of the grid scroll bar. When False, the contents of the grid doesn't scroll until the user releases the thumb tab in its new position.


'-- ColumnStyles
gcsList = 0
gcsEllipsis = 1
gcsNone = 2 
DeleteCol SUB (Column%) Deletes/Remove a column 1
DeleteRow SUB (Row%) Deletes/Remove a row 1
DelOptions SUBI Delete options from string grid INTEGER, Infinite
Circle SUB (x1%, y1%, x2%, y2%, c%, fill%) Draw & Fill Circle 6
CopyRect SUB (D, Image, S) D and S are QRECTs, Image can be a QImage, QCanvas, or QBitmap 3
Draw SUB (x%, y%, BMP) Draw Bitmap at (X,Y) 3
FillRect SUB (x1%, y1%, x2%, y2%, c%) Draws & Fills a rectangle 5
InsertCol SUB (Column%) Inserts a blank column 1
InsertRow SUB (Row%) Inserts a blank row 1
Line SUB (x1%, y1%, x2%, y2%, c%) Draws a line 5
LoadFromFile SUB (File$, RowOffset%, ColOffset%, MaxRows%) Loads table from file w/Separator
4
 
You can find out how many rows by loading the StringGrid Data into
 in a QStringList First and using QStringList.ItemCount to get StringGrid.Rowcount:

Dim slTmp As QStringList
   slTmp.LoadFromFile ("YourFile")
   MyStringGrid.LoadFromFile ("YourFile", 0, 0, slTmp.ItemCount)
   slTmp.Clear               ' to free memory, if not in a procedure
LoadFromStream SUB (S AS QFileStream/QMemoryStream, RowOffset%, ColOffset%, MaxRows%) 4

Loads table from stream w/Separator
Paint SUB (x%, y%, c%, borderc%) Fill Region 4
Pset SUB (x%, y%, c%) Pixel plot 3
Rectangle SUB (x1%, y1%, x2%, y2%, c%) Draws a rectangle 5
Repaint SUB Force repainting of stringgrid 0
RoundRect SUB (x1%, y1%, x2%, y2%, x3%, y3%, c%) Draws & Fills a rounded rectangle 7
SaveToFile SUB (File$, RowOffset%, ColOffset%, MaxRows%) Saves table to file w/Separator
4

This only works if the path and the FILE exists. First create a file (and path if needed) either with a utility like NOTEPAD, or use Qfilestream. Then use FILEEXISTS to make sure the file exists.
SaveToStream SUB (S AS QFileStream/QMemoryStream, RowOffset%, ColOffset%, MaxRows%) Saves table to stream w/Separator 4
SwapCols SUB (Col1%, Col2%) Swap two columns2
SwapRows SUB (Row1%, Row2%) Swap two rows2
StretchDraw SUB (Rect AS QRECT, BMP) Draw BMP and stretch to fit inside Rect 2
TextHeight FUNCTION (Text$) AS WORD Returns the height, in pixels, of Text$ string 1
TextWidth FUNCTION (Text$) AS WORD Returns the width, in pixels, of Text$ string 1
TextRect SUB (Rect AS QRECT, x%, y%, S$, fc%, bc%) 6

Write text, and clip within region Rect
TextOut SUB (x%, y%, S$, fc%, bc%) Writes text to cell 5

QStringGrid Events
EventTypeOccurs when...Params
OnDrawCellSUB (Col%, Row%, State%, R AS QRect)Cells need to be redrawn4
 
How to draw a cell in a custom color:
MyStringGrid.OnDrawCell = DrawCell

SUB Drawcell(Col%, Row%, State%, Rect AS QRect)
  IF (Row% > 0 AND Col% > 0) THEN   ' row 0 is fixed row
      MyStringGrid.FillRect(Rect.Left, Rect.Top, Rect.Left + MyStringGrid.ColWidths(Col%), _
            Rect.Top + MyStringGrid.RowHeights(Row%), SelectClr)
     MyStringGrid.TextOut(Rect.Left+2, Rect.Top+2, MyStringGrid.Cell(Col%, Row%), ClrTxt,-1)
  END IF
END SUB
OnEllipsisClickSUB (Col%, Row%)Pull down list is clicked2
OnKeyDownSUB (Key AS Word, Shift AS INTEGER)Key held down2
OnKeyPressSUB (Key AS BYTE)User presses a key1
OnKeyUpSUB (Key AS Word, Shift AS INTEGER)User releases a key2
OnListDropDownSUB (Col%, Row%, ByRef S AS STRING)Pull down list is clicked. S is a string with items separated by a new line character.3
OnSelectCellSUB (Col%, Row%, CanSelect%)User has selected a cell3
OnSetEditTextSUB (Col%, Row%, Value$)User has changed contents of cell3


QStringGrid Examples
CREATE Form AS QForm
CREATE Grid AS QStringGrid
Cell(1,1) = "hello"
END CREATE
Center
ShowModal
END CREATE

'------------------------------------------------------------------
' Pulldown list example

$INCLUDE "RAPIDQ.INC"
$ESCAPECHARS ON

CONST AgeList = "10\n11\n12\n13\n14\n15\n16\n17\n18\n19"

SUB EllipsisClick (Col AS LONG, Row AS LONG, Sender AS QSTRINGGRID)
Sender.Cell(Col, Row) = "Comments"+STR$(Row)
END SUB

CREATE Form AS QFORM
CREATE Grid AS QSTRINGGRID
Align = alClient
AddOptions(goEditing)
FixedRows = 1
FixedCols = 0
ColCount = 3
RowCount = 5
DefaultRowHeight = 20
ColumnStyle(0) = gcsList
ColumnStyle(1) = gcsEllipsis
ColumnList(0) = AgeList
Cell(0,0) = " Age"
Cell(1,0) = " Comments"
OnEllipsisClick = EllipsisClick
END CREATE
Caption = "Dropdown list example"
Center
ShowModal
END CREATE

QStringGrid Fast Clear Example
FOR A = 0 TO GridResults.RowCount-1
FOR B = 0 TO GridResults.ColCount-1
GridResults.cell(B,A)=""
NEXT
NEXT
GridResults.rowCount=1


QStringGrid Example 2

$TYPECHECK ON
$INCLUDE "RapidQ2.inc" ' must use latest version
DECLARE Function MyWndProc(Hnd AS LONG, Msg AS LONG, Wparm AS LONG, Lparm AS LONG) AS LONG

CREATE Form AS QFORMex
Caption = "QStringGrid Click test"
AddMessage WM_PARENTNOTIFY
OnMessage = MyWndProc
CREATE Grid AS QStringGrid
Align=alClient
AddOptions(goEditing,goColSizing)
FixedRows=1
FixedCols=1
ColCount=3
RowCount=5
visible= false 'for testing
END CREATE
END CREATE
Form.ShowModal


Function MyWndProc(Hnd AS LONG, Msg AS LONG, Wparm AS LONG, Lparm AS LONG) AS LONG
DIM x AS INTEGER
DIM y AS INTEGER
DIM theMouse AS STRING

If Grid.Visible THEN 'we get this even if not visible
'check to see if x, y are inside QStringGrid..
y = (lParm AND &HFFFF0000)SHR 16 'hiword
x = (lParm AND &HFFFF) 'loword
'add row/column calcuation here..
SELECT CASE wParm
CASE WM_LBUTTONDOWN
theMouse = "Left"
CASE WM_RBUTTONDOWN
theMouse = "Right"
END SELECT
ShowMessage "You pressed the " + theMouse + " button: " + STR$(x) + ", " + STR$(y)
End IF
RESULT = True 'need to set this
End Function

Prev Component Contents Next Component