The Stringgrid component is used to create a grid of display strings similar to spreadsheets. It simplifies the manipulation of strings and related objects in tables. The Stringgrid component provides many properties that control the idea of a network, as well as events and methods that respond to user actions using the structure of the table. Stringgrid has the effect of associating objects with each string in the grid, which is the information or behavior that the user encapsulates as a string representation.
First, Delphi Stringgrid control properties and instructions for use
1. Fixed rows and fixed columns: (when there is no fixed column, the width and height of each cell cannot be adjusted at run time)
Stringgrid.fixedcols: = fixed line number;
Stringgrid.fixedrows: = fixed number of columns;
Stringgrid.fixedcolor: = fixed row and column color;
Stringgrid.color: = The color of the invariant row (data area);
2. Width and height of data area column:
Stringgrid.defaultcolwidth: = The width of the internal setting;
Stringgrid.defaultrowheight: = All the height of the internal setting;
Stringgrid.colwidths[index:longint]:= the width of an entire row of a row;
stringgrid.rowheights[index:longint]:= the height of an entire column of a column;
3. Data area (CELL) designation (stops a row in the upper-left corner of the data area)
Stringgrid.leftcol:= a line;
stringgrid.toprow:= a column;
4. Focus moves to a cell:
Stringgrid.row:=?;
Stringgrid.col:=?;
5, set the number of rows and columns of the data area: (including fixed rows, columns are also counted inside)
Stringgrid.rowcount:=?;
Stringgrid.colcount:=?;
6. Assign a value to a cell (starting from 0):
Stringgrid.cells[col value, row value]:= string;
7. Determine which cell the mouse is in
In the mouse event of Stringgrid (Up,down or move):
VAR C, R:longint;
Stringgrid.mousetocell (X,Y,C,R); {x, y passed in by mouse event}
{Retrieve C, R is the current col, row value}
8. Stringgrid's Options properties:
To open a closed-lease in the execution of an options function such as ' gotabs ', you can do the following:
Open: stringgrid.options:= stringgrid.options + [gotabs];
Close: stringgrid.options:= stringgrid.options-[gotabs];
Gofixedhorzline a horizontal split line between fixed columns
Gofixedvertline vertical split lines between fixed rows
Gohorzline horizontal split lines between scrollable columns
Govertline vertical split lines between scrollable rows
Gorangeselect can be a multiple-selection unit, and if Goediting is included, Gorangeselect is ignored.
Godrawfocusselected with the mouse multiple selection, the first selected unit is anti-white
Gorowsizing with mouse to change sizing
Gocolsizing with mouse to change line width
Gorowmoving to move scrollable rows with the mouse
Gocolmoving to move scrollable columns with the mouse
Goediting the contents of an editable unit
The Goalwaysshoweditor table is always in edit mode, and no F2 or enter is required to have cursors waiting for input. If the options do not contain goediting or contain gorowselect, the goalwaysshoweditor is not valid.
Gotabs tab and shift+tab to switch table cells
Gorowselect with a mouse click to select an entire column (also with the mouse can be multiple choice of mutual exclusion)
When the gothumbtracking scrolls, the grid moves, and the grid moves.
First, the Delphi Stringgrid control's common operation code
Initialize the first row and first column of the Stirnggrid
Procedure Trefercontentform.setsgridtitle (Sgrid:tsuistringgrid);
Var
Colindex, Rowindex:integer;
Begin
Draw the first line (title bar)
For colindex: = 1 to Sgrid.colcount do
Begin
Sgrid.cells[colindex, 0]: = ' column name ' + Chr (ord (' A ')-1 + colindex);
End
Draw the first column (number bar)
Sgrid.colwidths[0]: = 30;
Sgrid.cells[0, 0]: = ' sequence ';
For RowIndex: = 1 to Sgrid.rowcount-1 do
Begin
Sgrid.cells[0, RowIndex]: = IntToStr (RowIndex);
End
End
Clear Stirnggrid, specify the number of rows and columns
Procedure Trefercontentform.clearsgrid (Sgrid:tsuistringgrid; arow, Acol:integer);
Var
I:integer;
Begin
Sgrid.rowcount: = Arow;
Sgrid.colcount: = Acol;
For I: = 0 to Sgrid.rowcount-1 do//If the table header is not clear starting from 1
Sgrid.rows[i]. Clear;
Setsgridtitle (Sgrid);
End
Add a row
Procedure Trefercontentform.mmin1click (Sender:tobject);
Var
Sgrid:tsuistringgrid;
Begin
Sgrid: = Tsuistringgrid (suipmsgrid.popupcomponent);
If Sgrid <> Nil Then
Begin
Sgrid.rowcount: = Sgrid.rowcount + 1;
Setsgridtitle (Sgrid);
End
End
Insert a row
Procedure Trefercontentform.mmin2click (Sender:tobject);
Var
I, Currow:integer;
Sgrid:tsuistringgrid;
Begin
Sgrid: = Tsuistringgrid (suipmsgrid.popupcomponent);
If Sgrid <> Nil Then
Begin
Currow: = Sgrid.row; Record the position of the currently selected row
Sgrid.rowcount: = Sgrid.rowcount + 1;
For I: = sgrid.rowcount-1 downto Currow + 1 Do
Sgrid.rows[i]: = Sgrid.rows[i-1];
Sgrid.rows[currow]. Clear;
Setsgridtitle (Sgrid);
End
End
Delete the current row
Procedure Trefercontentform.mmin3click (Sender:tobject);
Var
I:integer;
Sgrid:tsuistringgrid;
Begin
Sgrid: = Tsuistringgrid (suipmsgrid.popupcomponent);
If Sgrid <> Nil Then
Begin
For I: = Sgrid.row to Sgrid.rowcount-1 do
Sgrid.rows[i]: = sgrid.rows[i + 1];
Sgrid.rowcount: = sgrid.rowcount-1; Delete
Setsgridtitle (Sgrid);
End
End
Add a column
Procedure Trefercontentform.mmin5click (Sender:tobject);
Var
Sgrid:tsuistringgrid;
Begin
Sgrid: = Tsuistringgrid (suipmsgrid.popupcomponent);
If Sgrid <> Nil Then
Sgrid.colcount: = Sgrid.colcount + 1;
Setsgridtitle (Sgrid);
End
Insert a column
Procedure Trefercontentform.mmin6click (Sender:tobject);
Var
I, Curcol:integer;
Sgrid:tsuistringgrid;
Begin
Sgrid: = Tsuistringgrid (suipmsgrid.popupcomponent);
If Sgrid <> Nil Then
Begin
Curcol: = Sgrid.col; Record the position of the currently selected row
Sgrid.colcount: = Sgrid.colcount + 1;
For I: = sgrid.colcount-1 downto Curcol + 1 Do
Sgrid.cols[i]: = Sgrid.cols[i-1];
Sgrid.cols[curcol]. Clear;
End
Setsgridtitle (Sgrid);
End
Delete a column
Procedure Trefercontentform.mmin7click (Sender:tobject);
Var
I:integer;
Sgrid:tsuistringgrid;
Begin
Sgrid: = Tsuistringgrid (suipmsgrid.popupcomponent);
If Sgrid <> Nil Then
Begin
For I: = Sgrid.col to Sgrid.colcount-1 do
Sgrid.cols[i]: = sgrid.cols[i + 1];
Sgrid.colcount: = sgrid.colcount-1; Delete
End
Setsgridtitle (Sgrid);
End
Empty
Procedure Trefercontentform.mmin9click (Sender:tobject);
Var
Sgrid:tsuistringgrid;
Begin
Sgrid: = Tsuistringgrid (suipmsgrid.popupcomponent);
If Sgrid <> Nil Then
Begin
Clearsgrid (Sgrid, 5, 5)
End
Setsgridtitle (Sgrid);
End
Right-click to select cells
Procedure Trefercontentform.suistringgridcontentmousedown (Sender:tobject;
Button:tmousebutton; Shift:tshiftstate; X, Y:integer);
Begin
if (Button = mbright) Then
Begin
SendMessage (Sgridcontent.handle, wm_lbuttondown, 0, Makelong (x, y));
SendMessage (Sgridcontent.handle, Wm_lbuttonup, 0, Makelong (x, y));
End
End
Procedure Trefercontentform.formshow (Sender:tobject);
Begin
Setsgridtitle (sgridcontent);
End
The title bar and the number column is the content, center appears
Procedure Trefercontentform.sgridcontentdrawcell (Sender:tobject; Acol,
Arow:integer; Rect:trect; State:tgriddrawstate);
Var
s:string;
R:trect;
Begin
if (Acol = 0) or (arow = 0) Then
Begin
With Tsuistringgrid (Sender) do
Begin
Canvas.Brush.Color: = $00f0ddce;
Canvas.fillrect (Rect);
S: = Cells[acol, Arow];
r: = Rect;
DrawText (Canvas.handle, PChar (s), Length (s), R, Dt_center or Dt_singleline or dt_vcenter);
End
End
End
Delphi Stringgrid Common Properties and common operations