[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating Component

Posted on 2006-03-20
17
Medium Priority
?
245 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.
0
Comment
Question by:rafaelrgl
17 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 16245092
what error You recive and what does  tbLoadFormStatus(Self, Self.Name); do?
ziolko.
0
 
LVL 2

Expert Comment

by:Mainiacfreakus
ID: 16246895
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
 
LVL 1

Expert Comment

by:chascasvars
ID: 16246993
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:rafaelrgl
ID: 16251114
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
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16251142
Do you Speak Portuguese.

I´m From Brasil.
0
 
LVL 2

Expert Comment

by:Mainiacfreakus
ID: 16252214
try this:

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

Mainiacfreakus
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16253027
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
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16253061
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
 
LVL 2

Expert Comment

by:Mainiacfreakus
ID: 16255125
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
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16275820
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
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16275824
I Plus more 100 Points.
0
 
LVL 2

Accepted Solution

by:
Mainiacfreakus earned 1400 total points
ID: 16278275
Try freeing your component in the form's on free event

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

Mainiacfreakus
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16285903
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
 
LVL 2

Expert Comment

by:Mainiacfreakus
ID: 16286230
how are you creating the component in your maincode?

Mainiacfreakus
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 16288218
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
 
LVL 2

Expert Comment

by:Mainiacfreakus
ID: 16293618
Will look into it as soon as I can...

Mainiacfreakus
0
 
LVL 2

Expert Comment

by:Mainiacfreakus
ID: 16348653
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question