Oto mój unit:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Unit2;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure GroupBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure nazwaDblClick(Sender: TObject);
procedure atrybutyDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
licznik: integer = 0;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
encja: TGroupBox;
linia: TShape;
nazwa: TLabel;
atrybuty: TLabel;
begin
encja:=TGroupBox.Create(self);
encja.Left := 112+10*licznik;
encja.Top := 48+10*licznik;
encja.Width := 209;
encja.Height := 225;
encja.TabOrder := 1;
encja.Parent := self;
encja.Visible := true;
encja.OnMouseDown := GroupBox1MouseDown;
linia:=TShape.Create(self);
linia.Left := 0;
linia.Top := 49;
linia.Width := 209;
linia.Height := 1;
linia.Parent := encja;
linia.Visible := true;
nazwa:=TLabel.Create(self);
nazwa.Left := 64;
nazwa.Top := 16;
nazwa.Width := 73;
nazwa.Height := 25;
nazwa.Caption := 'Kliknij dwukrotnie';
nazwa.Parent := encja;
nazwa.Visible := true;
nazwa.OnDblClick := nazwaDblClick;
atrybuty:=TLabel.Create(self);
atrybuty.Left := 24;
atrybuty.Top := 64;
atrybuty.Width := 145;
atrybuty.Height := 129;
atrybuty.Caption := 'Kliknij dwukrotnie';
atrybuty.Parent := encja;
atrybuty.Visible := true;
atrybuty.OnDblClick := atrybutyDblClick;
licznik:=licznik+1;
end;
procedure TForm1.nazwaDblClick;
begin
TLabel(Sender).caption:=InputBox('Nazwa encji', 'Wprowadź nazwę encji.','');
if TLabel(Sender).caption='' then TLabel(Sender).caption:='Kliknij dwukrotnie';
end;
procedure TForm1.atrybutyDblClick;
begin
TLabel(Sender).caption:=InputBox('Nazwa atrybutu', 'Wprowadź nazwę atrybutu.','');
if TLabel(Sender).caption='' then TLabel(Sender).caption:='Kliknij dwukrotnie';
end;
procedure TForm1.GroupBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
SendMessage(TWinControl(Sender).Handle, WM_SYSCOMMAND, $F012, 0);
end;
procedure TForm1.Button2Click(Sender: TObject);
var FileStream:TFileStream;
Form2:TForm1;
begin
if OpenDialog1.Execute then begin
FileStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
FileStream.ReadComponent(Form1); // odczytaj ustawienia komponentu TButton
FileStream.Free; // zwolnij zmienna
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var FileStream:TFileStream;
begin
if SaveDialog1.Execute then begin
FileStream := TFileStream.Create(SaveDialog1.FileName, fmOpenWrite);
FileStream.WriteComponent(Form1); // zapisz ustawienia komponentu TButton
FileStream.Free; // zwolnij zmienna
end;
end;
end.
Problem jest chyba głównie w tej procedurze:
procedure TForm1.Button2Click(Sender: TObject);
var FileStream:TFileStream;
Form2:TForm1;
begin
if OpenDialog1.Execute then begin
FileStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
FileStream.ReadComponent(Form1); // odczytaj ustawienia komponentu TButton
FileStream.Free; // zwolnij zmienna
end;
end;
A oto komunikat o błędzie:
Project Project2.exe raised exception class EComponentError with message 'A component named Button1 already exists'. Process stopped. Use Step or Run to continue.
Mam pytanie, co jest nie tak?
Czytałem gdzieś na forum, że ten błąd się pojawia się, gdy dwa razy utworzy się tą samą zmienną.
Ale mnie tu właśnie o to chodzi, żeby te stare ustawienia Form1 zamienić na nowe, wczytane z pliku.