Difference between revisions of "TPanel"
From Free Pascal wiki
Jump to navigationJump to searchm |
|||
Line 3: | Line 3: | ||
'''TPanel''' [[image:tpanel.png]] is a component that creates a panel on a form. A TPanel is a descendant of [[TWinControl]] and is available under the [[Standard tab]] of the [[Component Palette]]. A TPanel can act as a visible container for other components. | '''TPanel''' [[image:tpanel.png]] is a component that creates a panel on a form. A TPanel is a descendant of [[TWinControl]] and is available under the [[Standard tab]] of the [[Component Palette]]. A TPanel can act as a visible container for other components. | ||
+ | == Example == | ||
+ | |||
+ | One way to use panels is when the control group is displayed and hidden. | ||
+ | Instead of showing or hiding individual controls, you can hide and show the panel and all of its child controls with a single command. | ||
+ | In this example, the following components are used: [[TButton]], [[TShape]] | ||
+ | |||
+ | === Create code === | ||
+ | |||
+ | * Create a new blank GUI application with the [[TForm|form]] Form1 | ||
+ | * Create the OnCreate event handler for the form, by clicking on your form, use the Object Inspector, the tab events, select the OnCreate event and click the button [...] or double click the button in the form. | ||
+ | * Add following code (Complete the missing parts): | ||
+ | |||
+ | <syntaxhighlight lang="pascal"> | ||
+ | |||
+ | unit Unit1; | ||
+ | |||
+ | {$mode objfpc}{$H+} | ||
+ | |||
+ | interface | ||
+ | |||
+ | uses | ||
+ | Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; | ||
+ | |||
+ | type | ||
+ | |||
+ | { TForm1 } | ||
+ | |||
+ | TForm1 = class(TForm) | ||
+ | procedure FormCreate(Sender: TObject); | ||
+ | private | ||
+ | aPanel:TPanel; | ||
+ | procedure aButtonClick(Sender: TObject); | ||
+ | public | ||
+ | |||
+ | end; | ||
+ | |||
+ | var | ||
+ | Form1: TForm1; | ||
+ | |||
+ | implementation | ||
+ | |||
+ | {$R *.lfm} | ||
+ | |||
+ | { TForm1 } | ||
+ | |||
+ | procedure TForm1.FormCreate(Sender: TObject); | ||
+ | var | ||
+ | aButton:TButton; | ||
+ | |||
+ | aShape1,aShape2:TShape; | ||
+ | begin | ||
+ | Caption :='Panel demo'; | ||
+ | aButton:=TButton.Create(Self); | ||
+ | aButton.Parent:=Self; | ||
+ | aButton.Caption:= 'Show/Hide'; | ||
+ | aPanel:=TPanel.Create(Self); | ||
+ | aPanel.Parent:=Self; | ||
+ | aPanel.Caption:=''; | ||
+ | aShape1:=TShape.Create(aPanel); | ||
+ | aShape1.Parent:=aPanel; | ||
+ | aShape1.Shape:=stStar; | ||
+ | aShape1.Top := 5; | ||
+ | aShape2:=TShape.Create(aPanel); | ||
+ | aShape2.Parent:=aPanel; | ||
+ | aShape2.Shape:=stStar; | ||
+ | aShape2.Top := 5; | ||
+ | aShape2.Left:=aShape1.Width+10; | ||
+ | aPanel.Height:=aShape1.Height+10; | ||
+ | aButton.Top:=aPanel.Height+10; | ||
+ | aButton.OnClick:=@aButtonClick; | ||
+ | Height := aButton.Top+aButton.Height+10; | ||
+ | end; | ||
+ | |||
+ | procedure TForm1.aButtonClick(Sender: TObject); //the event handler for the button | ||
+ | begin | ||
+ | if (Sender is TButton) | ||
+ | then begin | ||
+ | if aPanel.Visible then aPanel.Visible := false else aPanel.Visible := true; | ||
+ | end; | ||
+ | end; | ||
+ | end. | ||
+ | |||
+ | </syntaxhighlight> | ||
==See also== | ==See also== | ||
* [[doc:lcl/extctrls/tpanel.html|TPanel doc]] | * [[doc:lcl/extctrls/tpanel.html|TPanel doc]] |
Revision as of 19:01, 3 October 2019
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
TPanel is a component that creates a panel on a form. A TPanel is a descendant of TWinControl and is available under the Standard tab of the Component Palette. A TPanel can act as a visible container for other components.
Example
One way to use panels is when the control group is displayed and hidden. Instead of showing or hiding individual controls, you can hide and show the panel and all of its child controls with a single command. In this example, the following components are used: TButton, TShape
Create code
- Create a new blank GUI application with the form Form1
- Create the OnCreate event handler for the form, by clicking on your form, use the Object Inspector, the tab events, select the OnCreate event and click the button [...] or double click the button in the form.
- Add following code (Complete the missing parts):
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
aPanel:TPanel;
procedure aButtonClick(Sender: TObject);
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
aButton:TButton;
aShape1,aShape2:TShape;
begin
Caption :='Panel demo';
aButton:=TButton.Create(Self);
aButton.Parent:=Self;
aButton.Caption:= 'Show/Hide';
aPanel:=TPanel.Create(Self);
aPanel.Parent:=Self;
aPanel.Caption:='';
aShape1:=TShape.Create(aPanel);
aShape1.Parent:=aPanel;
aShape1.Shape:=stStar;
aShape1.Top := 5;
aShape2:=TShape.Create(aPanel);
aShape2.Parent:=aPanel;
aShape2.Shape:=stStar;
aShape2.Top := 5;
aShape2.Left:=aShape1.Width+10;
aPanel.Height:=aShape1.Height+10;
aButton.Top:=aPanel.Height+10;
aButton.OnClick:=@aButtonClick;
Height := aButton.Top+aButton.Height+10;
end;
procedure TForm1.aButtonClick(Sender: TObject); //the event handler for the button
begin
if (Sender is TButton)
then begin
if aPanel.Visible then aPanel.Visible := false else aPanel.Visible := true;
end;
end;
end.
See also