Показать сообщение отдельно
  #19  
Старый 07.04.2009, 00:04
Аватар для Страдалецъ
Страдалецъ Страдалецъ вне форума
Гуру
 
Регистрация: 09.03.2009
Адрес: На курорте, из окна вижу теплое Баренцево море. Бррр.
Сообщения: 4,723
Репутация: 52347
По умолчанию

Насколько я в курсе метод WriteComponent сохранит данные без динамических элементов, что для вас неподходит. Я набросал для вас вариант который сохраняет содержимое ячеек грида.
Код:
unit Unit12;

interface

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

type
  TForm12 = class(TForm)
    StringGrid1: TStringGrid;
    Panel1: TPanel;
    btnGenerate: TButton;
    btnSave: TButton;
    btnLoad: TButton;
    RowsUpDown: TUpDown;
    edRows: TEdit;
    Label1: TLabel;
    edCols: TEdit;
    ColsUpDown: TUpDown;
    Label2: TLabel;
    Panel2: TPanel;
    SaveDialog: TSaveDialog;
    OpenDialog: TOpenDialog;
    btnClear: TButton;
    procedure FormCreate(Sender: TObject);
    procedure edRowsChange(Sender: TObject);
    procedure edColsChange(Sender: TObject);
    procedure btnGenerateClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnLoadClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
  private
   procedure UpdateSheet;
   procedure RandomFillGrid;
  public
    { Public declarations }
  end;

var
  Form12: TForm12;

implementation

{$R *.dfm}

procedure TForm12.UpdateSheet;
Var C,R: Integer;
begin
 for C := 1 to ColsUpDown.Position
 do StringGrid1.Cells[C,0] := Chr(64+C);
 for R := 1 to RowsUpDown.Position
 do StringGrid1.Cells[0,R] := IntToStr(R);
end;

procedure TForm12.RandomFillGrid;
Var C,R: Integer;
begin
 for R := 1 to RowsUpDown.Position
 do for C := 1 to ColsUpDown.Position
    do StringGrid1.Cells[C,R] := IntToStr(Random(100000));
end;

procedure TForm12.btnClearClick(Sender: TObject);
Var C,R: Integer;
begin
 for R := 1 to RowsUpDown.Position
 do for C := 1 to ColsUpDown.Position
    do StringGrid1.Cells[C,R] := '';
end;

procedure TForm12.btnGenerateClick(Sender: TObject);
begin
 RandomFillGrid;
end;

procedure TForm12.btnLoadClick(Sender: TObject);
Var F: TFileStream;
    Col,Row,Cols,Rows: Word;
    Len: Byte;
    Data: ShortString;
begin
 if not OpenDialog.Execute then Exit;
 F := TFileStream.Create(OpenDialog.FileName,fmOpenRead);
 F.ReadBuffer(Cols,SizeOf(Word));
 F.ReadBuffer(Rows,SizeOf(Word));
 ColsUpDown.Position := Cols;
 RowsUpDown.Position := Rows;
 UpdateSheet;
 for Row := 1 to Rows
 do for Col := 1 to Cols
    do begin
       F.ReadBuffer(Len,SizeOf(Byte));
       SetLength(Data,Len);
       F.ReadBuffer(Data[1],Len);
       StringGrid1.Cells[Col,Row] := Data;
       end;
 F.Free;
end;

procedure TForm12.btnSaveClick(Sender: TObject);
Var F: TFileStream;
    Col,Row,Cols,Rows: Word;
    Len: Byte;
    Data: ShortString;
begin
 if not SaveDialog.Execute then Exit;
 F := TFileStream.Create(SaveDialog.FileName,fmCreate);
 Cols := ColsUpDown.Position;
 Rows := RowsUpDown.Position;
 F.WriteBuffer(Cols,SizeOf(Word));
 F.WriteBuffer(Rows,SizeOf(Word));
 for Row := 1 to Rows
 do for Col := 1 to Cols
    do begin
       Data := StringGrid1.Cells[Col,Row];
       F.WriteBuffer(Data,Length(Data)+1);
       end;
 F.Free;
end;

procedure TForm12.edColsChange(Sender: TObject);
begin
 StringGrid1.ColCount := ColsUpDown.Position+1;
 UpdateSheet;
end;

procedure TForm12.edRowsChange(Sender: TObject);
begin
 StringGrid1.RowCount := RowsUpDown.Position+1;
 UpdateSheet;
end;

procedure TForm12.FormCreate(Sender: TObject);
begin
 UpdateSheet;
end;

end.
Код:
object Form12: TForm12
  Left = 0
  Top = 0
  Caption = 'Form12'
  ClientHeight = 302
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 0
    Top = 0
    Width = 547
    Height = 302
    Align = alClient
    TabOrder = 0
    ExplicitWidth = 561
  end
  object Panel1: TPanel
    Left = 547
    Top = 0
    Width = 88
    Height = 302
    Align = alRight
    TabOrder = 1
    ExplicitLeft = 548
    object Label1: TLabel
      Left = 6
      Top = 10
      Width = 30
      Height = 13
      Caption = 'Rows:'
    end
    object Label2: TLabel
      Left = 6
      Top = 33
      Width = 24
      Height = 13
      Caption = 'Cols:'
    end
    object btnGenerate: TButton
      Left = 6
      Top = 56
      Width = 75
      Height = 25
      Caption = 'Generate'
      TabOrder = 0
      OnClick = btnGenerateClick
    end
    object btnSave: TButton
      Left = 6
      Top = 139
      Width = 75
      Height = 25
      Caption = 'Save'
      TabOrder = 1
      OnClick = btnSaveClick
    end
    object btnLoad: TButton
      Left = 6
      Top = 170
      Width = 75
      Height = 25
      Caption = 'Load'
      TabOrder = 2
      OnClick = btnLoadClick
    end
    object RowsUpDown: TUpDown
      Left = 65
      Top = 7
      Width = 16
      Height = 20
      Associate = edRows
      Min = 1
      Position = 2
      TabOrder = 3
    end
    object edRows: TEdit
      Left = 40
      Top = 7
      Width = 25
      Height = 20
      ReadOnly = True
      TabOrder = 4
      Text = '2'
      OnChange = edRowsChange
    end
    object edCols: TEdit
      Left = 40
      Top = 30
      Width = 25
      Height = 20
      ReadOnly = True
      TabOrder = 5
      Text = '2'
      OnChange = edColsChange
    end
    object ColsUpDown: TUpDown
      Left = 65
      Top = 30
      Width = 16
      Height = 20
      Associate = edCols
      Min = 1
      Position = 2
      TabOrder = 6
    end
    object Panel2: TPanel
      Left = 2
      Top = 119
      Width = 83
      Height = 1
      TabOrder = 7
    end
    object btnClear: TButton
      Left = 6
      Top = 88
      Width = 75
      Height = 25
      Caption = 'Clear'
      TabOrder = 8
      OnClick = btnClearClick
    end
  end
  object SaveDialog: TSaveDialog
    DefaultExt = '*.sdf'
    Filter = 'Stream data file (*.sdf)|*.sdf|All files (*.*)|*.*'
    Left = 560
    Top = 208
  end
  object OpenDialog: TOpenDialog
    DefaultExt = '*.sdf'
    Filter = 'Stream data file (*.sdf)|*.sdf|All files (*.*)|*.*'
    Left = 592
    Top = 208
  end
end
__________________
Жизнь такова какова она есть и больше никакова.
Помогаю за спасибо.
Ответить с цитированием