兄弟的问题好象是如何把Edit1和StringGrid的格子关连起来,那就需要记录格子的坐标,Edit1中多余的属性只有Tag属性,
但Tag属性是一个整数,而我们需要在Edit中记录格子的两个坐标,好在格子的坐标不会是负数,一般也不会超过Word的最大值,
所以可以用一个整数来保存两个Word类型的数,高位是Col,低位是Row,在Edit改变的时候再根据它的Tag属性解析出相对应的Cells,
并改变它的颜色,请看例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Edit2: TEdit;
procedure Edit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Edit1Change(Sender: TObject);
Var Rect:TRect;
ACol,ARow:Integer;
iTag:Integer;
begin
if not (Sender is TEdit) then
Exit;
iTag:=(Sender as TEdit).Tag;
ACol:=Loword(iTag);
ARow:=Hiword(iTag);
StringGrid1.Cells[ACol,ARow]:=Edit1.Text;
StringGrid1.Canvas.Brush.Color:=clRed;//改成红色.
Rect:=StringGrid1.CellRect(ACol,ARow);
StringGrid1.Canvas.FillRect(Rect);//填充
StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
//输出文字.
Sleep(50);
//等50ms
StringGrid1.Invalidate;
//再画一次以恢复颜色.
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Tag:=MakeLong(3,3);
Edit2.Tag:=MakeLong(2,3);
Edit1.OnChange:=Edit1Change;
Edit2.OnChange:=Edit1Change;
end;
end.