J
jinkxboy
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是一段将窗体控件保存和读取的代码,运行时读取出错
Code
------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ActnList;
type TMyControl = class(TControl);
type
TForm1 = class(TForm)
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
ComboBox1: TComboBox;
ProgressBar1: TProgressBar;
Button1: TButton;
Button4: TButton;
ActionList1: TActionList;
Action1: TAction;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
FileName = 'C:/FormSave.dat';
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
var F: TFileStream;
i: Integer;
begin
if FileExists(FileName) then
begin
F := TFileStream.Create(FileName, fmOpenRead);
for i := ComponentCount - 1 downto 0 do Components.Free;
F.ReadComponent(Self);
F.Free;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
FM: TFileStream;
i: integer;
begin
Fm := TFileStream.Create(FileName, fmCreate);
fm.WriteComponent(self);
fm.free;
end;
end.
form
------------------
object Form1: TForm1
Left = 175
Top = 195
Width = 388
Height = 235
Caption = 'Form1'
Color = clBtnFace
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object Button2: TButton
Left = 104
Top = 144
Width = 97
Height = 25
Caption = '读取'
TabOrder = 0
OnClick = Button2Click
end
object Edit1: TEdit
Left = 24
Top = 16
Width = 121
Height = 20
TabOrder = 1
Text = 'Edit1'
end
object Button3: TButton
Left = 104
Top = 176
Width = 97
Height = 25
Caption = '保存'
TabOrder = 2
OnClick = Button3Click
end
object ComboBox1: TComboBox
Left = 40
Top = 64
Width = 145
Height = 20
ItemHeight = 12
TabOrder = 3
Text = 'ComboBox1'
end
object ProgressBar1: TProgressBar
Left = 32
Top = 112
Width = 150
Height = 16
Min = 0
Max = 100
TabOrder = 4
end
object Button1: TButton
Left = 216
Top = 144
Width = 97
Height = 25
Action = Action1
TabOrder = 5
end
object Button4: TButton
Left = 216
Top = 175
Width = 97
Height = 25
Caption = '保存'
TabOrder = 6
end
object ActionList1: TActionList
Left = 248
Top = 72
object Action1: TAction
Caption = 'Action1'
end
end
end
Code
------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ActnList;
type TMyControl = class(TControl);
type
TForm1 = class(TForm)
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
ComboBox1: TComboBox;
ProgressBar1: TProgressBar;
Button1: TButton;
Button4: TButton;
ActionList1: TActionList;
Action1: TAction;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
FileName = 'C:/FormSave.dat';
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
var F: TFileStream;
i: Integer;
begin
if FileExists(FileName) then
begin
F := TFileStream.Create(FileName, fmOpenRead);
for i := ComponentCount - 1 downto 0 do Components.Free;
F.ReadComponent(Self);
F.Free;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
FM: TFileStream;
i: integer;
begin
Fm := TFileStream.Create(FileName, fmCreate);
fm.WriteComponent(self);
fm.free;
end;
end.
form
------------------
object Form1: TForm1
Left = 175
Top = 195
Width = 388
Height = 235
Caption = 'Form1'
Color = clBtnFace
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object Button2: TButton
Left = 104
Top = 144
Width = 97
Height = 25
Caption = '读取'
TabOrder = 0
OnClick = Button2Click
end
object Edit1: TEdit
Left = 24
Top = 16
Width = 121
Height = 20
TabOrder = 1
Text = 'Edit1'
end
object Button3: TButton
Left = 104
Top = 176
Width = 97
Height = 25
Caption = '保存'
TabOrder = 2
OnClick = Button3Click
end
object ComboBox1: TComboBox
Left = 40
Top = 64
Width = 145
Height = 20
ItemHeight = 12
TabOrder = 3
Text = 'ComboBox1'
end
object ProgressBar1: TProgressBar
Left = 32
Top = 112
Width = 150
Height = 16
Min = 0
Max = 100
TabOrder = 4
end
object Button1: TButton
Left = 216
Top = 144
Width = 97
Height = 25
Action = Action1
TabOrder = 5
end
object Button4: TButton
Left = 216
Top = 175
Width = 97
Height = 25
Caption = '保存'
TabOrder = 6
end
object ActionList1: TActionList
Left = 248
Top = 72
object Action1: TAction
Caption = 'Action1'
end
end
end