Difference between revisions of "TButton/ja"
Miyatakejiro (talk | contribs) |
Miyatakejiro (talk | contribs) |
||
Line 6: | Line 6: | ||
このページでは、[[doc:lcl/stdctrls/tbutton.html|TButton]] コンポーネントの使い方を説明します。 | このページでは、[[doc:lcl/stdctrls/tbutton.html|TButton]] コンポーネントの使い方を説明します。 | ||
− | + | <br> | |
+ | (以下の記述で「○○をクリック」とあればそれは左クリックのことです。右クリックの場合はそのように記述します。) | ||
== IDE上の表示 == | == IDE上の表示 == | ||
Line 22: | Line 23: | ||
<br> | <br> | ||
<br> | <br> | ||
− | + | マウスクリックに対するイベントハンドラは、フォーム上に置いたボタンをダブルクリックすることで (または、オブジェクトインスペクタの「イベント」タブから、OnClick というイベントを選択することで) 簡単に作ることができます。 ''Form1''フォーム上の''Button1'' のイベントハンドラは、以下のような記述になります。: | |
<syntaxhighlight> | <syntaxhighlight> | ||
procedure TForm1.Button1Click(Sender: TObject); | procedure TForm1.Button1Click(Sender: TObject); | ||
Line 29: | Line 30: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | '''begin''' と '''end''' の間に、'''Button1''' をクリックしたときの処理を書きましょう。 | + | 次に、'''begin''' と '''end''' の間に、'''Button1''' をクリックしたときの処理を書きましょう。 |
Line 36: | Line 37: | ||
* [[Form_Tutorial#The_first_GUI_application|The first GUI application]] :初心者のためのチュートリアル | * [[Form_Tutorial#The_first_GUI_application|The first GUI application]] :初心者のためのチュートリアル | ||
* [[Lazarus_Tutorial/ja#Lazarus.E3.81.A7.E3.83.97.E3.83.AD.E3.82.B0.E3.83.A9.E3.83.A0.E3.82.92.E3.81.AF.E3.81.98.E3.82.81.E3.81.A6.E3.81.BF.E3.82.88.E3.81.86|Your first Lazarus program]] : Lazarus の チュートリアル | * [[Lazarus_Tutorial/ja#Lazarus.E3.81.A7.E3.83.97.E3.83.AD.E3.82.B0.E3.83.A9.E3.83.A0.E3.82.92.E3.81.AF.E3.81.98.E3.82.81.E3.81.A6.E3.81.BF.E3.82.88.E3.81.86|Your first Lazarus program]] : Lazarus の チュートリアル | ||
− | * [[Object_Oriented_Programming_with_FreePascal_and_Lazarus#Programming_Example|Programming Example]] : | + | * [[Object_Oriented_Programming_with_FreePascal_and_Lazarus#Programming_Example|Programming Example]] : Free Pascal と Lazarus を用いたオブジェクト指向プログラミングのチュートリアル |
==== 簡単な例 ==== | ==== 簡単な例 ==== |
Revision as of 14:35, 3 June 2016
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
>> LCL Components >> TButton
このページでは、TButton コンポーネントの使い方を説明します。
(以下の記述で「○○をクリック」とあればそれは左クリックのことです。右クリックの場合はそのように記述します。)
IDE上の表示
「Standard」タブの4番目のコントロールであり、「"OK"のある四角い押しボタン」のアイコンで表示されています。
使い方
TButton は TForm/ja におけるもっとも基本的なコントロールです。 ボタンをマウスでクリックすることで(または[Tab]キーを押してボタンが選択された状態にして[Enter]キーで押すことで)、アクションが実行されます。 この「クリックすること」は「イベント」と呼ばれます。 このため、For this you need event_handlerが必要になります。 that are called after the jump.
「ボタン」コントロールをフォームに追加するには、Standard/ja コンポーネントパレットの左から4番目の TButton ("OK" と書かれた四角形のボタン) を押して選択し、ボタンを配置したいフォームの上でクリックしてください。
マウスクリックに対するイベントハンドラは、フォーム上に置いたボタンをダブルクリックすることで (または、オブジェクトインスペクタの「イベント」タブから、OnClick というイベントを選択することで) 簡単に作ることができます。 Form1フォーム上のButton1 のイベントハンドラは、以下のような記述になります。:
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
次に、begin と end の間に、Button1 をクリックしたときの処理を書きましょう。
ほとんどすべての初心者用チュートリアルでは、Almost all available beginner tutorials use TButtons as an easy entry into the Object Oriented Programming with Lazarus。. 以下に示すチュートリアルも、初心者がボタンの使い方を理解するためにぴったりです。:
- The first GUI application :初心者のためのチュートリアル
- Your first Lazarus program : Lazarus の チュートリアル
- Programming Example : Free Pascal と Lazarus を用いたオブジェクト指向プログラミングのチュートリアル
簡単な例
- 新規プロジェクトからアプリケーションを選択し、表示されたフォーム上に TButton を置きます。
- フォーム上に置いたButton1をダブルクリックしてください。(Button1のためのデフォルトのイベントハンドラであるOnClickが生成され、ソースエディタが開きます。)
- イベントハンドラとして、以下のコードを追加します。:
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Lazarus はサイコーだ。'); //ボタンを押すと、左に書いた文字列がメッセージダイアログで表示されます。
end;
- プログラムを実行しましょう。(<F9>キーを押してください。)
ボタンの自動生成
Sometimes, instead of creating buttons (or other components) with the Lazarus form designer, it is easier to create them dynamically at run time. This approach is useful especially if you have continually repeated buttons on a form.
This can be achieved as in the the following example (a quick calculator):
- Create a new blank GUI application with the form Form1 and add StdCtrls to the uses clause (here the TButton is).
- Change caption Form1 to QuickAdd.
- Create the OnCreate event handler of Form1 (go in the Object Inspector to the event OnCreate and click the button [...]).
- Add following code:
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
aButton: TButton;
begin
for i:=0 to 9 do begin //create 10 Buttons
aButton:=TButton.Create(Self); //create Button, Owner is Form1, where the button is released later
aButton.Parent:=Self; //determine where it is to be displayed
aButton.Width:=aButton.Height; //Width should correspond to the height of the buttons
aButton.Left:=i*aButton.Width; //Distance from left
aButton.Caption:=IntToStr(i); //Captions of the buttons (0.9)
aButton.OnClick:=@aButtonClick; //the event handler for the button -> will be created yet
end;
Self.Height:=aButton.Height; //Height of the form should correspond to the height of the buttons
Self.Width:=aButton.Width*10; //Width of the form to match the width of all buttons
end;
- Now you must create the event handler for the button clicks.
- In the source editor, entering your class TForm1 in the section
private
. - Add
procedure aButtonClick(Sender: TObject);
and then press the keys [CTRL] + [Shift] + [c] (the code completion becomes active and creates the procedureTForm1.aButtonClick(Sender: TObject);
. - Paste following code:
procedure TForm1.aButtonClick(Sender: TObject);
const
Cnt: Integer = 0;
var
i: Integer;
begin
if (Sender is TButton) //called the event handler of a button out?
and TryStrToInt(TButton(Sender).Caption, i) then //then try to convert the label in a integer
begin
Cnt:=Cnt + i; //the adding counter is incremented by the number of entrechende
Caption:='QuickAdd: '+IntToStr(Cnt); //write the result to the caption of the form
end;
end;
- Start your application.
procedure <class>.<name of procedure>(Sender: TObject);
has. Thus, you can use one from another class!See also
Return To: LCL Components | — Previous: TPopupMenu/ja | Next: TLabel/ja |
--Michl 12:25, 15 May 2014 (CEST)