We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Creating Component

rafaelrgl
rafaelrgl asked
on
Medium Priority
273 Views
Last Modified: 2010-04-05
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.
Comment
Watch Question

Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
what error You recive and what does  tbLoadFormStatus(Self, Self.Name); do?
ziolko.
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

Author

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'

Author

Commented:
Do you Speak Portuguese.

I´m From Brasil.
try this:

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

Mainiacfreakus

Author

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;

Author

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'
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

Author

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;

Author

Commented:
I Plus more 100 Points.
Try freeing your component in the form's on free event

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

Mainiacfreakus

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
how are you creating the component in your maincode?

Mainiacfreakus

Author

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.
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.