Link to home
Create AccountLog in
Avatar of rafaelrgl
rafaelrgl

asked on

Creating Component

constructor TPosition.Create(aOwner : TComponent);
begin
  inherited create(aOwner);
  tbLoadFormStatus(Self, Self.Name);
end;


I don´t know what´s wrong with this code above, I ´m trying to get values from the form by component code. but does not work. HELP ME.
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

what error You recive and what does  tbLoadFormStatus(Self, Self.Name); do?
ziolko.
Avatar of Mainiacfreakus
Mainiacfreakus

if the form owns the component then:

var parent : Tcomponent;
...
inherited create(aOwner);
parent := aowner;
while not(parent is TForm) and not(parent = parent.owner) do
  parent := parent.owner;
if parent is tform then with (parent as tform) begin
  *comp1*.text := caption; (takes form caption)
  *comp1*.top := top; (sets control to top of form)
end;

Hope this helps u.

Mainiacfreakus
First of all, you have to implement or include the procedure tbLoadFormStatus

procedure tbLoadFormStatus(Form: TForm; const Section: string);
 var
  Ini: TIniFile;
  Maximized: boolean;
 begin
  Maximized := false; { Evita msg do compilador }
  Ini := TIniFile.Create(ChangeFileExt(
  ExtractFileName(ParamStr(0)),'.INI'));
  try
   Maximized := Ini.ReadBool(Section, 'Maximized', Maximized);
   Form.Left := Ini.ReadInteger(Section, 'Left', Form.Left);
   Form.Top := Ini.ReadInteger(Section, 'Top', Form.Top);
   Form.Width := Ini.ReadInteger(Section, 'Width', Form.Width);
   Form.Height := Ini.ReadInteger(Section, 'Height', Form.Height);
   if Maximized then
    Form.Perform(WM_SIZE, SIZE_MAXIMIZED, 0);
  finally
   Ini.Free;
  end;
 end;

After that, change the code of the constructor

 constructor TPosition.Create(aOwner : TComponent);
 begin
   inherited create(aOwner);
   tbLoadFormStatus(aOwner, aOwner.Name);
 end;

You should use this component as a runtime component and instantiate it in OnShow form event, having the Parent property the name of the form to resize.

Sorry my poor english
Avatar of rafaelrgl

ASKER

When I put the code:

 constructor TPosition.Create(aOwner : TComponent);
 begin
   inherited create(aOwner);
   tbLoadFormStatus(aOwner, aOwner.Name);
 end;

Look what happening:

[Error] Position.pas(44): Incompatible types: 'TForm' and 'TComponent'
[Error] Position.pas(49): Undeclared identifier: 'aOwner'
[Error] Position.pas(49): ')' expected but identifier 'Name' found
[Error] Position.pas(50): ';' expected but 'INHERITED' found
[Error] Position.pas(51): '.' expected but ';' found
[Fatal Error] Nucleo.dpk(34): Could not compile used unit 'Position.pas'
Do you Speak Portuguese.

I´m From Brasil.
try this:

   inherited create(aOwner);
   tbLoadFormStatus(aOwner as TForm, (aOwner as TForm).Name);

Mainiacfreakus
Works Good When I used On  constructor TPosition.Create(aOwner : TComponent);

But I try the Procedure below and does not work. Can you see that for me:

destructor TPosition.destroy;
begin
  tbSaveFormStatus(aOwner as TForm, (aOwner as TForm).Name);
  inherited destroy;
end;

procedure TPosition.tbSaveFormStatus(Form: TForm; const Section: string);
var
  Ini: TIniFile;
  Maximized: boolean;
begin
  Ini := TIniFile.Create(ChangeFileExt(
  ExtractFileName(ParamStr(0)),'.INI'));
  try
  Maximized := Form.WindowState = wsMaximized;
  Ini.WriteBool(Section, 'Maximized', Maximized);
  if not Maximized then begin
  Ini.WriteInteger(Section, 'Left', Form.Left);
  Ini.WriteInteger(Section, 'Top', Form.Top);
  Ini.WriteInteger(Section, 'Width', Form.Width);
  Ini.WriteInteger(Section, 'Height', Form.Height);
  end;
  finally
  Ini.Free;
  end;
end;
This is the ERROR:


  [Error] Position.pas(49): Undeclared identifier: 'aOwner'
  [Error] Position.pas(50): Operator not applicable to this operand type
  [Error] Position.pas(50): Operator not applicable to this operand type
  [Error] Position.pas(51): ';' expected but 'INHERITED' found
  [Error] Position.pas(52): '.' expected but ';' found
  [Fatal Error] Nucleo.dpk(34): Could not compile used unit 'Position.pas'
Hi

destructor TPosition.destroy;
**var aOwner : TComponent;
begin
  aOwner := Self.Owner;
  tbSaveFormStatus(aOwner as TForm, (aOwner as TForm).Name);
  inherited destroy;
end;

Hope this helps.

Mainiacfreakus
I don´t know what's wrong, but when I Close the form with the component. SomeThing goes wrong.
There´s nothing more in the project. Just The Form and the Component.

Here is the ERROR:

Exception EAccessViolation in module Project1.exe at 0004DBEF.
Access violation at address 0004DBEF in module 'Project1.exe'. Read of address 00000008

Hes is The CODE:

destructor TPosition.destroy;
var aOwner : TComponent;
begin
   aOwner := Self.Owner;
   tbSaveFormStatus(aOwner as TForm, (aOwner as TForm).Name);
   inherited destroy;
end;

procedure TPosition.tbSaveFormStatus(Form: TForm; const Section: string);
var
  Ini: TIniFile;
  Maximized: boolean;
begin
  Ini := TIniFile.Create(ChangeFileExt(
  ExtractFileName(ParamStr(0)),'.INI'));
  try
  Maximized := Form.WindowState = wsMaximized;
  Ini.WriteBool(Section, 'Maximized', Maximized);
  if not Maximized then begin
  Ini.WriteInteger(Section, 'Left', Form.Left);
  Ini.WriteInteger(Section, 'Top', Form.Top);
  Ini.WriteInteger(Section, 'Width', Form.Width);
  Ini.WriteInteger(Section, 'Height', Form.Height);
  end;
  finally
  Ini.Free;
  end;
end;
I Plus more 100 Points.
ASKER CERTIFIED SOLUTION
Avatar of Mainiacfreakus
Mainiacfreakus

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Well, If I put position1.free; on the FORM is work well.

But I would like to do this on component code. and doesn´t work. Can you help me Mainiacfreakus.
how are you creating the component in your maincode?

Mainiacfreakus
unit Position;

interface

uses
  Forms, IniFiles, SysUtils, Messages, Windows,

  Classes;

type
  TPosition = class(TComponent)
  private
    PActive : boolean;

    procedure SetActive(Value : boolean);
    { Private declarations }
  protected
    { Protected declarations }
  public
    constructor create(aOwner : TComponent); override;
    destructor destroy; override;
    { Public declarations }
    Procedure tbLoadFormStatus(Form: TForm; const Section: string);
    Procedure tbSaveFormStatus(Form: TForm; const Section: string);
  published
    property Active : boolean read PActive write SetActive default True;
    { Published declarations }
  end;

procedure Register;

implementation

procedure TPosition.SetActive(Value : boolean);
begin
 if value <> Pactive then begin
     PActive := Value;
  end;
end;

constructor TPosition.Create(aOwner : TComponent);
begin
  inherited create(aOwner);
  tbLoadFormStatus(aOwner as TForm, (aOwner as TForm).Name);
end;

destructor TPosition.destroy;
var aOwner : TComponent;
begin
  aOwner := Self.Owner;
  tbSaveFormStatus(aOwner as TForm, (aOwner as TForm).Name);
 Inherited destroy;
end;

procedure TPosition.tbSaveFormStatus(Form: TForm; const Section: string);
var
  Ini: TIniFile;
  Maximized: boolean;
begin
  Ini := TIniFile.Create(ChangeFileExt(
  ExtractFileName(ParamStr(0)),'.INI'));
  try
  Maximized := Form.WindowState = wsMaximized;
  Ini.WriteBool(Section, 'Maximized', Maximized);
  if not Maximized then begin
  Ini.WriteInteger(Section, 'Left', Form.Left);
  Ini.WriteInteger(Section, 'Top', Form.Top);
  Ini.WriteInteger(Section, 'Width', Form.Width);
  Ini.WriteInteger(Section, 'Height', Form.Height);
  end;
  finally
  Ini.Free;
  end;
end;

procedure TPosition.tbLoadFormStatus(Form: TForm; const Section: string);
 var
  Ini: TIniFile;
  Maximized: boolean;
 begin
  Maximized := false; { Evita msg do compilador }
  Ini := TIniFile.Create(ChangeFileExt(
  ExtractFileName(ParamStr(0)),'.INI'));
  try
   Maximized := Ini.ReadBool(Section, 'Maximized', Maximized);
   Form.Left := Ini.ReadInteger(Section, 'Left', Form.Left);
   Form.Top := Ini.ReadInteger(Section, 'Top', Form.Top);
   Form.Width := Ini.ReadInteger(Section, 'Width', Form.Width);
   Form.Height := Ini.ReadInteger(Section, 'Height', Form.Height);
   if Maximized then
    Form.Perform(WM_SIZE, SIZE_MAXIMIZED, 0);
  finally
   Ini.Free;
  end;
 end;

procedure Register;
begin
  RegisterComponents('Nucleo', [TPosition]);
end;

end.
Will look into it as soon as I can...

Mainiacfreakus
I can't find a decent way to do it...

but here is a option...

TPosition = class.....
private
  FSavedCloseQuery : TCloseQueryEvent;
  ....
end;  

procedure TPosition.SaveFormPos(Sender: TObject; var CanClose: Boolean);
var aOwner : TComponent;
begin
  aOwner := Self.Owner;
  tbSaveFormStatus(aOwner as TForm, (aOwner as TForm).Name);
  If Assigned(FSavedCloseQuery) then FSavedCloseQuery(Sender, CanClose);
end;

constructor TPosition.Create(aOwner : TComponent);
begin
  inherited create(aOwner);
  tbLoadFormStatus(aOwner as TForm, (aOwner as TForm).Name);
   If not (csDesigning in ComponentState) then Begin
    FSavedCloseQuery := (aOwner as TForm).OnCloseQuery;
    (aOwner as TForm).OnCloseQuery := SaveFormPos;
  End;
end;

destructor TPosition.destroy;
begin
 Inherited destroy;
end;

However this means you may have problems implementing a Onclosequery procedure on the form...

Please Note: I haven't checked this code... bit busy (unfortunately).

Mainiacfreakus