Difference between revisions of "Frames/ru"

From Lazarus wiki
Jump to navigationJump to search
Line 8: Line 8:
 
[[Image:Frame_new.PNG]]
 
[[Image:Frame_new.PNG]]
  
== How to place a frame to the form or another frame? ==
+
== Как поместить фрейм на форму или в другой фрейм? ==
The [[Standard tab]] of the [[Component Palette]] has a special component item "[[TFrame]]". When you drop it to the form or frame the IDE asks you to choose one of project frames. You have to save the project and the the new frame first, only saved frames will appear in the choose from list. Frames in packages are not yet implemented. But you can create frames in code even in packages. You can't create frame circles - the IDE will forbid this, i.o.w. you can't place FrameA to FrameA and you can't place any frame which contains FrameA to FrameA.
+
 
 +
Вкладка [[Standard_tab/ru|Standard]] в [[Component Palette|Палитре компонентов]] имеет специальный компонентный элемент «[[TFrame]]». Когда вы перетаскиваете его на форму или фрейм, IDE предлагает вам выбрать один из фреймов проекта. Вы должны сначала сохранить проект и новый фрейм, потому что только предварительно сохраненные фреймы могут появиться в списке выбора. Фреймы в пакетах пока не реализованы. Но вы можете создавать фреймы в коде даже в пакетах. Вы не можете создавать циклические фрйемы - IDE запретит это, т.е. вы не можете поместить FrameA в FrameA, и вы не можете поместить любой фрейм, который содержит FrameA, в FrameA.
  
 
[[Image:Frame_add.PNG]]
 
[[Image:Frame_add.PNG]]

Revision as of 20:22, 31 August 2020

Deutsch (de) English (en) suomi (fi) русский (ru)

Фреймы являются именованными контейнерами для компонентов и очень похожи на Формы. Их уникальная способность заключается в том, что они могут быть встроены в формы или другие фреймы в дизайнере. В виде форм они хранятся в двух файлах: код хранится в файле .pas, а дизайн - в файле .lfm.

Как создать фрейм?

Нажмите пункт меню File->New... и откройте диалоговое окно, выбрав пункт "Frame".

Frame new.PNG

Как поместить фрейм на форму или в другой фрейм?

Вкладка Standard в Палитре компонентов имеет специальный компонентный элемент «TFrame». Когда вы перетаскиваете его на форму или фрейм, IDE предлагает вам выбрать один из фреймов проекта. Вы должны сначала сохранить проект и новый фрейм, потому что только предварительно сохраненные фреймы могут появиться в списке выбора. Фреймы в пакетах пока не реализованы. Но вы можете создавать фреймы в коде даже в пакетах. Вы не можете создавать циклические фрйемы - IDE запретит это, т.е. вы не можете поместить FrameA в FrameA, и вы не можете поместить любой фрейм, который содержит FrameA, в FrameA.

Frame add.PNG

What can they be used for?

They are needed when you have a group of components that you want to reuse on multiple forms. The group should have the same set of controls and logic between them at different windows (forms) of your application. You can group that repeated controls and logic into one frame and use that frame in different places. Therefore you don't need to repeat the work of control layouting and writing their logic.

For example, you have two listboxes and buttons to move items between them. So you can create a frame with two listboxes and needed buttons, write the logic for items moving and then use your frame on all forms where needed. Moreover, if you find an error in the frame code then you can fix it once in the frame code instead of fixing it n-times among all forms.

a frame in designer:

Frame example.PNG


the frame placed on a form:

Frame embedded.PNG

the same frame placed on another form:

Frame embedded2.PNG

Initialising private variables

TFrame does not have OnCreate or OnDestroy events in which private variables can be initialised and released. Override the default constructor and destructor to do this.

TFrame1 = class(TFrame)
  private
    MyObj: TObject;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
  end; 

constructor TFrame1.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  MyObj := TObject.Create;
end;

destructor TFrame1.Destroy;
begin
  MyObj.Free
  inherited Destroy;
end;

Sample of dynamic creation

More easy. Because TFrame component in component palette is not required. And you can make a page change type application(like a smart phone application) with save resource.

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    GroupBox1: TGroupBox;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
    Frame: TFrame;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation
uses
  Unit2{TFrame1}, Unit3{TFrame2}, Unit4{TFrame3};

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  Frame := TFrame1.Create(GroupBox1);
  Frame.Parent := GroupBox1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not Assigned(Frame) then
  begin
    Frame := TFrame1.Create(GroupBox1);
    Frame.Parent := GroupBox1;
  end else if Frame is TFrame1 then begin
    Frame.Free;
    Frame := TFrame2.Create(GroupBox1);
    Frame.Parent := GroupBox1;
  end else if Frame is TFrame2 then begin
    Frame.Free;
    Frame := TFrame3.Create(GroupBox1);
    Frame.Parent := GroupBox1;
  end else begin
    FreeAndNil(Frame);
  end;
end;

end.

See also