unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button2: TButton;
    Label3: TLabel;
    procedure Button2Click(Sender: TObject);
    procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  zakres: TGridRect;
begin
  zakres.Left := 2;
  zakres.Top := 1;
  zakres.Right := 3;
  zakres.Bottom := 4;
  StringGrid1.Selection := zakres;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I, J : Integer;
begin
  with StringGrid1 do
    for I := 0 to ColCount - 1 do
      for J:= 0 to RowCount - 1 do
          Cells[I,J] := IntToStr(Random(141));
end;

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var i,j,s:integer;
begin
  s:=0;
  with Stringgrid1 do
  begin
    for i:=Selection.Left to Selection.Right do  //
      for j:=Selection.Top to Selection.Bottom do
       if Cells[i,j]<>''  then  s:=s+Strtoint(Cells[i,j]);
//wyświetlenie współrzędnych i sumy liczb z zaznaczonego myszą //zakresu komórek ĺ
label1.Caption:=Inttostr(Selection.Left)+','+Inttostr(Selection.Top)+'   '+ Inttostr(Selection.Right)+','+Inttostr(Selection.Bottom)+' zawartość: '+InttoStr(s);
  end;
end;

end.
