Link to home
Start Free TrialLog in
Avatar of pr2501
pr2501

asked on

Restore of component after new starting of app from ini

With code below I'm creating a _Shape (TShape) while run time.
Is it enough if i record next (listed below) proprieties to restore TShape  when app is started again? Or i must add some other things for creating of component in general?

Brush.Color
Width
Height
Top
Left
procedure TForm1.nov1Click(Sender: TObject);
begin
  _Shape:=TShape.Create(self);
  _Shape.Width:=10;
  _Shape.Height:=10;
  _Shape.Brush.Color:=clwhite;
  With _Shape do   begin
     _Shape.Parent:=self;
     Top:=10;
     Left:=10;
   onMouseDown:=ShapeMouseDown;
   onMouseMove:=ShapeMouseMove;
   onMouseUp:=ShapeMouseUp;
  end;
 _Shape:=nil;
end;

Open in new window

Avatar of Geert G
Geert G
Flag of Belgium image

you normally store all properties you change

the odd thing in your code : _Shape:=nil;
this removes the reference to the object
where did you declare the variable _Shape ?
The attached code is a little more consistant.

Rule of the thumb. If you want to create a component at run time, you can always put it on a form first, make it work, then look into the dfm file to see which properties are set and copy them to you're code. After this copy you can remove the component from the form and make the copied dfm text work.

(Personally i really dislike the with statement and if Embarcadero decided to remove it from the language i would rejoice. Expecially nested with 's are a pain to debug and understand after a while)
procedure TForm1.nov1Click(Sender: TObject);
begin
  With TShape.Create(self) do begin
   Width:=10;
   Height:=10;
   Brush.Color:=clwhite;
   Parent:=self;
   Top:=10;
   Left:=10;
   onMouseDown:=ShapeMouseDown;
   onMouseMove:=ShapeMouseMove;
   onMouseUp:=ShapeMouseUp;
  end;
end;

Open in new window

and, yes, it seemes enough to get it working (parent and owner are set), unless you want something different than the default shape.
Avatar of pr2501
pr2501

ASKER

Pen.Width :=1; (from dfm) but in my case irelevant.

(Geert_Gruwez:)
public
  _Shape:TShape;

Do i have to add parent and owner to ini?

Can You pleas help me repair attached code?
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  cmp: tcomponent;
  i: integer;
begin
  //with TINIFile.Create(ChangeFileExt(Paramstr(0), '.ini')) do
     with TINIFile.Create('c:\PC1\Montaza\Montaza.ini') do
  try
    i := 1;
    //cmp := FindComponent('SpeedButton' + IntToStr(i));
    cmp := FindComponent('Shape' + IntToStr(i));
    while (cmp <> nil) do
    begin
      //WriteInteger('Form1', cmp.Name + ' Font Color', integer(TSpeedButton(cmp).Font.Color));
       WriteInteger('Form1', cmp.Name + ' Brush Color', integer(TShape(cmp).Brush.Color));
         WriteInteger('Form1', cmp.Name + ' Width', integer(TShape(cmp).Width));
         WriteInteger('Form1', cmp.Name + ' Height', integer(TShape(cmp).Height));
         WriteInteger('Form1', cmp.Name + ' Top', integer(TShape(cmp).Top));
         WriteInteger('Form1', cmp.Name + ' Left', integer(TShape(cmp).Left));
           i := i + 1; 
     // cmp := FindComponent('SpeedButton' + IntToStr(i));
     cmp := FindComponent('Shape' + IntToStr(i)); 
    end; 
  finally
    free; 
  end; 
end;
// get ini
procedure TForm1.FormCreate(Sender: TObject); 
   var 
  cmp: tcomponent; 
  i: integer;
begin 
  with TINIFile.Create('c:\PC1\Montaza\Montaza.ini') do 
  try 
    i := 1;
   // cmp := FindComponent('SpeedButton' + IntToStr(i)); 
   cmp := FindComponent('Shape' + IntToStr(i)); 
    while (cmp <> nil) do 
    begin
      //TSpeedButton(cmp).Font.Color := TColor(ReadInteger('Form1', cmp.Name + ' Font Color', integer(clGreen))); 
      TShape(cmp).Brush.Color := TColor(ReadInteger('Form1', cmp.Name + ' Brush Color', integer(clGreen))); 
  TShape(cmp).Width := ReadInteger('Form1', cmp.Name + ' Width', integer(TShape(cmp).Width)); 
  TShape(cmp).Height := ReadInteger('Form1', cmp.Name + ' Height', integer(TShape(cmp).Height));
  TShape(cmp).Top := ReadInteger('Form1', cmp.Name + ' Top', integer(TShape(cmp).Top)); 
  TShape(cmp).Left := ReadInteger('Form1', cmp.Name + ' Left', integer(TShape(cmp).Left)); 
      i := i + 1; 
      //cmp := FindComponent('SpeedButton' + IntToStr(i));
      cmp := FindComponent('Shape' + IntToStr(i)); 
    end; 
  finally 
    free;
  end; 
    Application.Showhint:=true; 
end;
end.

Open in new window

On first sight i see no problems with the code assuming you have created enough shape components.

But if that is the problem you will have to store the number of shapes in the ini and use that count to create shapes + reading the relevant information from the ini.
If that is not the case please tell what need to be fixed.
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pr2501

ASKER

Wery good, it works.
thank you

"you have algorithm missing when you delete shapes"
What do i need to do?

now i can see another problem: For my _Shapes i have popupmenu . After reopening of the form it is not present any more.Can you help me fourther?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pr2501

ASKER

No, i don't create popup menu dimamcli.
Avatar of pr2501

ASKER

And i have also lost hint for _Shape.
Please give more information about your problem which menu, how connected and created who is the owner etc.
by hint do you mean the hint for each shape? It is just a property for a shape that can be set.
Avatar of pr2501

ASKER

For other things pleas give me instructions.
popupmenu.JPG
popupmenuitem.JPG
Avatar of pr2501

ASKER

And next is the event ofclick on popupitem:
procedure TForm1.N0pen1Click(Sender: TObject);
begin
 F:=TForm2.Create(Application);
 F.Caption:=_Shape.hint;
 mname:=_Shape.hint;
 if mname<>'' then  F.Show
 else begin
 s:='';
_shape.Hint:=InputBox('','Najprej poimenuj stroj', s) ;
   end;
end;

Open in new window

Avatar of pr2501

ASKER

And thn also other two click events.
procedure TForm1.tag1Click(Sender: TObject);
begin
s:='';
  _shape.Hint:= InputBox('','Poimenuj stroj', s) ;
    _Shape.Brush.Color:=clgreen;
    inc(i);
    _shape.Name:= 'shape'+inttostr(i);
    edit1.Text:= _shape.Name;
end;

procedure TForm1.delate1Click(Sender: TObject);
  var ShouldClose: Boolean;
begin
  if MessageDlg('Ali res ~elia zbrisati stroj?', mtConfirmation,
      [mbYes, mbNo], 0) = mrYes then
   _Shape.Free;
  // if Assigned(_Shape) then _Shape.Free;
end;

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you want to use the popupmenu for every shape then you need to identify the shape clicked

procedure TForm1.popMenuItem1Click(Sender: TObject);
var shape: TShape;
begin
  shape := TShape(PopupMenu1.Tag);
  ShowMessage(shape.Name);
end;

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var P: TPoint;
begin
  if Button = mbRight then
  begin
    PopupMenu1.Tag := Integer(Sender);
    P := TShape(Sender).ClientToScreen(Point(X, Y));
    PopupMenu1.Popup(P.X, P.Y);
  end;
end;
your delete would be :

procedure TForm1.popMenuItem1Click(Sender: TObject);
var shape: TShape;
begin
  shape := TShape(PopupMenu1.Tag);
  ShowMessage(shape.Name);
end;

procedure delate1Click(Sender: TObject);
 // var ShouldClose: Boolean;  --> this is not used
begin
  shape := TShape(PopupMenu1.Tag);
  if MessageDlg('Ali res ~elia zbrisati stroj?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
    Shape.Free;
end;
Avatar of pr2501

ASKER


I was not working over the weekend. But now i see that i need more time to understand Your replies (even they are very accurate an simple writed) because i don't have enough knowledge about Delphi.
hey,
we all lack knowledge, if anything is unclear
just ask

i was very good at delphi
then they decided to see how good i was at oracle
i know what it is to be in that situation
i'm currently working 5 minutes and reading 20 or 40 minutes to solve every new problem about oracle
the good thing is, some problems are reoccuring
Avatar of pr2501

ASKER

I will ask relative question in a new post.
thank you