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.
LVL 1
rafaelrglAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ziolkoCommented:
what error You recive and what does  tbLoadFormStatus(Self, Self.Name); do?
ziolko.
0
MainiacfreakusCommented:
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
0
chascasvarsCommented:
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
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

rafaelrglAuthor Commented:
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'
0
rafaelrglAuthor Commented:
Do you Speak Portuguese.

I´m From Brasil.
0
MainiacfreakusCommented:
try this:

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

Mainiacfreakus
0
rafaelrglAuthor Commented:
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;
0
rafaelrglAuthor Commented:
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'
0
MainiacfreakusCommented:
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
0
rafaelrglAuthor Commented:
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;
0
rafaelrglAuthor Commented:
I Plus more 100 Points.
0
MainiacfreakusCommented:
Try freeing your component in the form's on free event

OnFormFree(....)
begin
  position.free;
end;

Mainiacfreakus
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rafaelrglAuthor Commented:
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.
0
MainiacfreakusCommented:
how are you creating the component in your maincode?

Mainiacfreakus
0
rafaelrglAuthor Commented:
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.
0
MainiacfreakusCommented:
Will look into it as soon as I can...

Mainiacfreakus
0
MainiacfreakusCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.