Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TStringGrid

129 views
Skip to first unread message

Shakil Amin

unread,
Nov 29, 1999, 3:00:00 AM11/29/99
to
I am using a TStringGrid to paint certain columns within a row. Say, row 4
column 8 to 10 are painted in blue. However, when I scroll across the
columns and back to the start, I lose whatever I have painted.

I need to retain whatever I have painted.

Any help would be appreciated.

thanks

Peter Below (TeamB)

unread,
Nov 30, 1999, 3:00:00 AM11/30/99
to
> I am using a TStringGrid to paint certain columns within a row. Say, row 4
> column 8 to 10 are painted in blue. However, when I scroll across the
> columns and back to the start, I lose whatever I have painted.

That suggests you are painting at the wrong place. All painting on cells
has to be done in the OnDrawCell event of the grid and you draw only one
cell per event, the one identified by the row and col parameters of the
event handler.

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


Melanie

unread,
Dec 1, 1999, 3:00:00 AM12/1/99
to
I'm trying to dock a combobox into a cell in a TStringGrid. Is this
possible?
If so, How? Please help.

Peter Below (TeamB)

unread,
Dec 1, 1999, 3:00:00 AM12/1/99
to
In article <82382p$gk...@forums.borland.com>, Melanie wrote:
> I'm trying to dock a combobox into a cell in a TStringGrid. Is this
> possible?
>

A simple approach that does not require a stringgrid descendent is
this:

Attach a combobox to a stringgrid column

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, Grids;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
ComboBox1: TComboBox;
procedure ComboBox1Exit(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow:
Integer;
var CanSelect: Boolean);
private
{ Private declarations }
Procedure CMDialogKey( Var msg: TCMDialogKey );
message CM_DIALOGKEY;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.CMDialogKey(var msg: TCMDialogKey);
begin
If Activecontrol = Combobox1 Then Begin
If msg.CharCode = VK_TAB Then Begin
// set focus back to the grid and pass the tab key to it
stringgrid1.setfocus;
stringgrid1.perform( WM_KEYDOWN, msg.charcode, msg.keydata );
// swallow this message
msg.result := 1;
Exit;
End;
End;
inherited;
end;

procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
with sender as TCombobox do begin
hide;
if itemindex >= 0 then
with stringgrid1 do
cells[ col, row ] := items[itemindex];
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
combobox1.visible := false;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
R: TRect;
org: TPoint;
begin
With Sender As TStringgrid Do
If (ACol = 2) and (ARow >= FixedRows) Then Begin
// entered the column associated to the combobox
// get grid out of selection mode
perform( WM_CANCELMODE, 0, 0 );
// position the control on top of the cell
R := CellRect( Acol, Arow );
org:= Self.ScreenToClient( ClientToScreen( R.topleft ));
With combobox1 do begin
setbounds( org.X, org.Y, r.right-r.left, height );
itemindex := Items.IndexOf( Cells[ acol, arow ] );
Show;
BringTofront;
// focus the combobox and drop down the list
SetFocus;
DroppedDown := true;
end;
End;
end;

end.

This shows a combobox on top of a cell but it is not a child control
of the grid. Which means it will not scroll with the grid cells and
there is a problem if the user clicks on a partially visible cell
since the grid will scroll it completely into view *after* the
combobox has been fixed to the old position of the cell.

Making the combobox a child of the grid requires a stringgrid
descendent:

unit ControlStringgrid;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
Grids;

type
TControlStringgrid = class(TStringgrid)
private
{ Private declarations }
Procedure WMCommand( var msg: TWMCommand ); message WM_COMMAND;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('PBGoodies', [TControlStringgrid]);
end;

{ TControlStringgrid }

procedure TControlStringgrid.WMCommand(var msg: TWMCommand);
begin
If EditorMode and ( msg.Ctl = InplaceEditor.Handle ) Then
inherited
Else
If msg.Ctl <> 0 Then
msg.result :=
SendMessage( msg.ctl, CN_COMMAND,
TMessage(msg).wparam,
TMessage(msg).lparam );
end;

end.

Using this you can set the comboboxes Parent property to the grid and
set its bounds to the cellrect of the cell it should appear on. It
will now scroll with the cells

Melanie

unread,
Dec 6, 1999, 3:00:00 AM12/6/99
to
Thanks Peter

Peter Below (TeamB) <10011...@compuXXserve.com> wrote in message
news:VA.00004296.011aa49a@noname...

Leonardo Mozachi Neto

unread,
Dec 6, 1999, 3:00:00 AM12/6/99
to
Hello to everyone out there!

First of all, I've read the last 1922 messages and haven't found answers

for these problems.

Well, I have aStringGrind in a Form, with 4 columns, without fixed
columns
and many rows.The StringGrid is filled dinamically. The user can select
any cell of the grid and there is no limit of cells he can select.

These are the topics:

1- Multiline
The text that have to be shown is larger than cell's width.
How do I make Multiline in a StringGrid.

2- Colloring Individual cells.
After a user click, would be nice if the color of the cell chages, and
if the user clicks again, the collor returns to default.
How to paint cells individually?

3- If we could do the doubt #2, would be nice to know WICH cells
are in diferent colours.Is it possible?


Thanks for your time guys!


Leonardo.

Peter Below (TeamB)

unread,
Dec 7, 1999, 3:00:00 AM12/7/99
to
> Well, I have aStringGrind in a Form, with 4 columns, without fixed
> columns
> and many rows.The StringGrid is filled dinamically. The user can select
> any cell of the grid and there is no limit of cells he can select.
>
> These are the topics:
>
> 1- Multiline
> The text that have to be shown is larger than cell's width.
> How do I make Multiline in a StringGrid.

You have to draw it yourself, in an OnDrawCell event handler, or a
overriden DrawCell method, if you derive a new stringgrid class to handle
your needs better.


procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
Rect: TRect; State: TGridDrawState);
var
S: String;
drawrect :trect;
begin
S:= (Sender As TStringgrid).Cells[ Col, Row ];
If Length(S) > 0 Then Begin
drawrect := rect;
DrawText((Sender As TStringgrid).canvas.handle,
Pchar(S), Length(S), drawrect,
dt_calcrect or dt_wordbreak or dt_left );
If (drawrect.bottom - drawrect.top) >
(Sender As TStringgrid).RowHeights[row]
Then
(Sender As TStringgrid).RowHeights[row] :=
(drawrect.bottom - drawrect.top)
Else Begin
drawrect.Right := rect.right;
(Sender As TStringgrid).canvas.fillrect( drawrect );
DrawText((Sender As TStringgrid).canvas.handle,
Pchar(S), Length(S), drawrect,
dt_wordbreak or dt_left);
End;
End;
end;

It will automatically adjust the row height to larger values if needed. It
will not *reduce* cell height automatically, though. This is something you
may want to handle in the code that sets the cell contents. I also noted
that the standard TStringgrid will not edit such multiline cells
correctly. The default inplace editor needs to be replaced for that to
work properly, and that requires a new grid class derived from
TStringgrid.

> 2- Colloring Individual cells.
> After a user click, would be nice if the color of the cell chages, and
> if the user clicks again, the collor returns to default.
> How to paint cells individually?

Same as above: a OnDrawCell event. You need a place to store the cell
colors. If you do not use the grids Objects property for something else
you can store the colors there. That requires some typecasting, since the
Objects property is typed as Tobject. Instead of storing the color value
directly you may want to store a flag that indicates the cells
selected/non-selected state.



> 3- If we could do the doubt #2, would be nice to know WICH cells
> are in diferent colours.Is it possible?

If you store the cell state into Objects than you can of course examine it
by looking at the grids Objects property.

0 new messages