Difference between revisions of "Form Tutorial/ja"

From Lazarus wiki
Jump to navigationJump to search
 
(40 intermediate revisions by the same user not shown)
Line 9: Line 9:
 
フォーム (class [[TForm]])は[[Application|application]]のユーザーインターフェースとなるウインドウもしくはダイアログを表す。それは他の[[LCL Components|components]](例えば、ボタン、ラベル、編集ボックス、画像など)が挿入されるコンテナである。
 
フォーム (class [[TForm]])は[[Application|application]]のユーザーインターフェースとなるウインドウもしくはダイアログを表す。それは他の[[LCL Components|components]](例えば、ボタン、ラベル、編集ボックス、画像など)が挿入されるコンテナである。
  
== 最初のGUIアプリケーション ==
+
== 初めてのGUIアプリケーション ==
  
Upon successfully installing Lazarus and starting a new Application Project, an empty form is created. Otherwise you can create a new [[Overview_of_Free_Pascal_and_Lazarus#GUI_apps|GUI application]] under Main menu -> Project -> New Project -> Application OK.
+
Lazarusのインストールが成功し、新しいアプリケーションプロジェクトを始めると、空のフォームが作られる。そうでなければ、新しい
 +
[[Overview_of_Free_Pascal_and_Lazarus#GUI_apps|GUI アプリケーション]]を メインメニュー -> プロジェクト -> 新規プロジェクト -> アプリケーション OKで作ることができる。
  
Now you have created a new, fully functional project with a form:
+
今、新しい、全機能が使えるフォームを持つプロジェクトが生成された:
  
 
[[Image:Form1_Designmodus.png]]
 
[[Image:Form1_Designmodus.png]]
  
To run the newly created project, you can simply press the  {{keypress|F9}} key or click with the mouse on the main menu icon [[Image:Start.png]] (or Main menu: Run -> Run). The project will be compiled and executed.
+
新しく作られたプロジェクトを実行するには、単に{{keypress|F9}}キーを押す、もしくはマウスでメインメニューアイコン[[Image:Start.png]](あるいは メインメニュー: 実行 -> 実行)をクリックする。プロジェクトはコンパイル、実行される。
 +
 
 +
特に面白いことが起こるわけではない。フォーム(Form1)はわずかに外見が変わり、ラスタグリッドの点々(個々のコンポーネントの位置付けの助けとなる)が消える(グリッドの点が見えてる間は、それがデザインモードにあることがわかる):<br>
  
Nothing really exciting happens. The form (Form1) changes slightly in appearance and the points from the raster grid (help for positioning of individual components) disappear (as long as the grid points are visible, you also know that it is still in design mode):<br>
 
 
[[Image:Form1_Designmodus.png]]  ->  [[Image:Form1_Runmodus.png]]  
 
[[Image:Form1_Designmodus.png]]  ->  [[Image:Form1_Runmodus.png]]  
  
Next, you can place a [[TButton]] onto the form. This will be used later to allow the opening of a second form:
+
次にフォームの上に[[TButton/ja|TButton]]を配することができる。これは後に第2のフォームを開くことを可能にするために使われる:
  
Select the TButton from the [[Standard tab]] on the [[Component Palette]].
+
[[Component Palette/ja|コンポーネントパレット]]上の[[Standard tab/ja|Standardタブ]]からTButtonを選ぶ。
  
 
[[Image:TButtonStandardpalette.png]]
 
[[Image:TButtonStandardpalette.png]]
  
and click on the form: There is now a button placed on Form1 with the name and caption "Button1".
+
そしてフォームをクリックする: 「Button1」の名前とキャプションでForm1にボタンが配される。
  
In order for this button to do something, you have to announce that you intend to do something when you click it. This can be regarded as a simple event. For this you need an [[Event_order | event handler]] that is called after the click.
+
このボタンに何かをさせるために、それをクリックすると、何か意図したことをするようにボタンに告げなければならない。これは単純なイベントである。このためクリックされた後に呼ばれる [[Event_order | イベントハンドラ]]が必要である。マウスに対するイベントハンドラはボタンはButton1をダブルクリックすることにより、極めて簡単に行える(もしくはオブジェクトインスペクタで、Button1 -> イベントタブ -> OnClick、次いで[...]をクリック)。これはButton1がクリックされると(設計段階ではなく[[runtime]])にいつも呼ばれるプロシージャTForm1.Button1Clickをコードエディタに作り出す。
The event handler for a mouse click can be quite easily reached by double-clicking on Button1 (or in the Object Inspector, select Button1 -> Events tab -> OnClick, then click on the [...] button). This creates a procedure TForm1.Button1Click in the code editor, which is always called (at [[runtime]], not in design time) when Button1 is clicked:
 
  
 
[[Image:NeuesProjekt2.png]]
 
[[Image:NeuesProjekt2.png]]
  
To make the application do something after clicking on Button1, add some code between the [[Begin]] and [[End]] of the procedure TForm1.Button1Click like this:
+
 
 +
アプリケーションにButton1をクリックした後に何かをさせるために、プロシージャTForm1.Button1Clickの[[Begin]][[End]]の間にこのようにコードを加える:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
   Caption:='My first Form';  //Caption of Form1 is changed (text in formhead)
+
   Caption:='My first Form';  //Form1のキャプションが変更される(フォーム冒頭のテキスト)
 
   //or
 
   //or
   Button1.Caption:='Hello';  //Caption of button is changed (shown text of button)
+
   Button1.Caption:='Hello';  //ボタンのキャプションが変更される(ボタンのテキストが表示される)
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now run the form by pressing {{keypress|F9}}, and notice that when the button is clicked the form caption and the button caption both change.
+
そして、{{keypress|F9}}を押し、フォームを起動させる、ボタンをクリックしたときにフォームキャプションとボタンキャプションの双方が変わることに注意すること。
  
You can now experiment a little bit and learn how to use the standard components. To get started, I would recommend you to try samples of the following components:
+
ちょっとした実験を行い、スタンダードコンポーネントの使い方を学んだ。始めるにあたり、以下のコンポーネントの例を試してみることを勧める:
  
 
{|
 
{|
 
|-
 
|-
| [[TButton]] || [[image:tbutton.png]]
+
| [[TButton/ja|TButton]] || [[image:tbutton.png]]
 
|-
 
|-
| [[TLabel]]  || [[image:tlabel.png]]
+
| [[TLabel/ja|TLabel]]  || [[image:tlabel.png]]
 
|-
 
|-
| [[TEdit]]  || [[image:tedit.png]]
+
| [[TEdit/ja|TEdit]]  || [[image:tedit.png]]
 
|-
 
|-
| [[TMemo]]  || [[image:tmemo.png]]
+
| [[TMemo/ja|TMemo]]  || [[image:tmemo.png]]
 
|-
 
|-
| [[TToggleBox]] || [[image:ttogglebox.png]]
+
| [[TToggleBox/ja|TToggleBox]] || [[image:ttogglebox.png]]
 
|-
 
|-
| [[TCheckBox]] || [[image:tcheckbox.png]]
+
| [[TCheckBox/ja|TCheckBox]] || [[image:tcheckbox.png]]
 
|-
 
|-
| [[TRadioButton]] || [[image:tradiobutton.png]]
+
| [[TRadioButton/ja|TRadioButton]] || [[image:tradiobutton.png]]
 
|-
 
|-
| [[TListBox]] || [[image:tlistbox.png]]
+
| [[TListBox/ja|TListBox]] || [[image:tlistbox.png]]
 
|-
 
|-
| [[TComboBox]] || [[image:tcombobox.png]]
+
| [[TComboBox/ja|TComboBox]] || [[image:tcombobox.png]]
 
|-
 
|-
| [[TScrollBar]] || [[image:tscrollbar.png]]
+
| [[TScrollBar/ja|TScrollBar]] || [[image:tscrollbar.png]]
 
|}
 
|}
  
There is additional helpful information in the [[Lazarus Tutorial]].
+
ここにさらに役に立つ情報がある[[Lazarus Tutorial/ja|Lazarusチュートリアル]]
  
 
== 2つ目のフォームを使う ==
 
== 2つ目のフォームを使う ==
  
The tutorial shows how to use multiple forms in a project. In this example, only two forms, Form1 (main form) and Form2, are created, but the process is identical for additional forms.
+
このチュートリアルでは1つのプロジェクトで複数のフォームを使う方法を示す。この例で2つだけのフォーム、Form1(メインフォーム)とForm2が作られるが、追加のフォームについてもその仕方は同一である。
 
 
There is a main form with a button, which when clicked opens the new form. The new form also receives a button which when clicked closes the new form and returns you to the main form.
 
  
If you have worked through the first tutorial [[Form Tutorial#The first GUI app | The first GUI app]], you will need to delete the code between the begin and end of procedure TForm1.Button1Click. Otherwise you will need to create a new project (application), drop a button on the form, and create the OnClick event handler for this Button1.<br>
+
クリックされると新しいフォームを開くボタンがメインフォームにある。新しいフォームはまた、クリックされるとそれ自身を閉じ、メインフォームに帰ってくるボタンを有する。
Click the new button to enter a caption (visible text on the button). To do this, select the button (just click it once) and in the Object Inspector click Properties -> Caption, and enter the text "Open Form2".
+
もし最初のチュートリアル、 [[Form_Tutorial/ja#初めてのGUIアプリケーション | 初めてのGUIアプリケーション]]を終えているのなら、TForm1.Button1Clickプロシージャのbeginとendの間のコードを削除しておく必要がある。そうしないと、新たなプロジェクト(アプリケーション)フォームにボタンを置き、キャプション(ボタンの上の表示されるテキスト)を入力し、OnClickイベントハンドラをこのButton1に作らなければならない。<br>
 +
これを行うためボタンを選択(一度クリックする)し、オブジェクトインスペクタで、プロパティ -> Caption、「Open Form2」と入力する。
  
[[Image:Objektinspektor.png]]
+
[[Image:ObjectInspector_ja.png]]
  
In the Main menu click File -> New Form, to add a second form (Form2) to the project. Place a button (Button1) on Form2 and create the OnClick event handler for it. Change the caption of this button to "Close".
+
2つ目のフォーム(Form2)をプロジェクトに加えるため、メインメニューで ファイル -> 新規フォームを実行する。Form2にボタンを置きそれにOnClickイベントを作る。このボタンのキャプションを「Close」に変更する。
  
Now the project has two forms, each of which can be selected and displayed with Main menu Project -> Forms (or key combination {{keypress|Shift}} + {{keypress|F12}}). Alternatively, you can select the tab for a Unit in the Source Editor, then use {{keypress|F12}} to switch between the code editor and form designer for the form of the associated [[Unit]].
+
いま、このプロジェクトは2つのフォームを持ち、それぞれはメインメニュー、プロジェクト -> フォーム...(もしくはキーの組み合わせ{{keypress|Shift}} + {{keypress|F12}})で選択、表示できる。また別に、ソースエディタでUnitタブで選択もでき、{{keypress|F12}}でコードエディタと[[Unit]]に割り当てられたフォームデザイナとを切り替えできる。
  
In the [[source code editor]], go to the Form1 associated [[Unit]] (Unit1) and add "Unit2" to the [[Uses]] clause:
+
[[source code editor]][[Unit]] (Unit1)が割り当てられたForm1に行き[[Uses]]に節「Unit2を加える:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
 
uses
 
uses
   Classes, SysUtils, ... , Unit2;  // It is important that the individual units are separated by a comma(,)
+
   Classes, SysUtils, ... , Unit2;  // それぞれのユニットがコンマ(,)で区切られていることと
                                   // and the uses clause ends with a semicolon(;).
+
                                   // uses 節がセミコロン(;)で終わることは重要である。
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now you can call Unit2 (and thus Form2) from Unit1.
+
今、Unit2(即ちForm2)をUnit1から呼び出すことができる。
  
Next, edit the OnClick event of the button belonging to Form1:
+
次に、Form1に属するボタンのOnClickイベントを編集する:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 107: Line 108:
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
   Form2.ShowModal;  //Displays Form2, the focus is placed on Form2.
+
   Form2.ShowModal;  //Form2を表示、フォーカスはForm2に来る。
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 115: Line 116:
 
procedure TForm2.Button1Click(Sender: TObject);
 
procedure TForm2.Button1Click(Sender: TObject);
 
begin
 
begin
   Close;  //Close Form2.
+
   Close;  //Form2を閉じる
 
end;  
 
end;  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now you can start the project ([[Image:Start.png]] or {{keypress|F9}}) and open Form2 by clicking the button on Form1.
+
これで、プロジェクトを起動([[Image:Start.png]] もしくは {{keypress|F9}})し、Form1のボタンをクリックすることでForm2を開くことができる。
  
 
=== ShowとShowModalの違い ===
 
=== ShowとShowModalの違い ===
  
Both methods ''Show'' and ''ShowModal'' make a form visible. To see the difference between ''Show'' and ''ShowModal'':
+
''Show''および ''ShowModal''メソッドは双方ともフォームを見えるようにする。''Show''''ShowModal''の違いを見るために:
  
* Extend the previous example [[Form Tutorial#The use of a second form|The use of a second form]] by adding a second TButton to ''Form1''.
+
* 前の例、''Form1''[[Form Tutorial/ja#2つ目のフォームを使う|2つ目のフォームを使う]]に2番目のTButtonを加え拡張する。
* Select ''Button1'' and change the properties ''Name'' to ''btnShow'' and ''Caption'' to ''Show''.
+
* ''Button1''を選択しプロパティ''Name''''btnShow''に、''Caption''''Show''に変更する。
* Select ''Button2'' and change the properties ''Name'' to ''btnShowModal'' and ''Caption'' to ''ShowModal''.
+
* ''Button2''を選択しプロパティ''Name'' ''btnShowModal''に、''Caption''''ShowModal''に変更する。
* Create (or modify) the ''OnClick'' event handler of the two buttons as follows:
+
* ''OnClick''イベントを作る(もしくは、変更)し2つのボタンのイベントハンドラを以下のようにする:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 144: Line 145:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
* Start and watch the difference!
+
* 起動してその差を見よう!
  
 
;Show:
 
;Show:
This procedure makes a form visible and continues to execute the code of the calling control (''Form1''). In our example, the message ([[Dialog_Examples#ShowMessage|ShowMessage]]) appears almost simultaneously with ''Form2''. You can also continue to use the calling form ''Form1''. For example, it is possible to move it, or click the ''Show'' button again (but clicking ''ShowModal'' will cause an error).
+
このプロシージャはフォームを見える状態にし、呼んだコントロール(''Form1'')のコードの実行を継続する。この例ではメッセージ、([[Dialog_Examples#ShowMessage|ShowMessage]]) はほとんど''Form2''と同時に現れる。また''Form1''を呼んで使い続けることができる。例えばそれを動かしたり、''Show''ボタンを押すことができる(しかし''ShowModal''をクリックするとエラーが発生するだろう)
  
Instead of using '''<code><myForm>.Show;</code>''', it is also possible to use '''<code><myForm>.Visible:=True;</code>'''. The method ''Show'' does the following:
+
'''<code><myForm>.Show;</code>'''を用いる代わりに、'''<code><myForm>.Visible:=True;</code>'''を用いることもできる、''Show''メソッドは以下を行う:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 159: Line 160:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The only difference between '''<code><myForm>.Show;</code>''', and '''<code><myForm>.Visible:=True;</code>''' is that the form displayed using ''Show'' will be on top of all other forms currently displayed.
+
'''<code><myForm>.Show;</code>''''''<code><myForm>.Visible:=True;</code>'''の唯一の違いは、''Show''を用いてフォームを表示すると、現在表示されているすべての他のフォームの上に表示されることである。
  
 
;ShowModal:
 
;ShowModal:
  
''ShowModal'' halts the processing of code in the calling control (''Form1'') until the newly opened form (''Form2'') has been closed. In the example, the ''ShowMessage'' line immediately following ''ShowModal'' is executed only after ''Form2'' is closed. The calling form is frozen: You can neither move ''Form1'' nor click any of its buttons.
+
''ShowModal''は新しく開かれているフォーム(''Form2'')が閉じられるまで呼び出したコントロール(''Form1'')を停止させる。例では、''ShowModal''の直後の''ShowMessage''行は''Form2''が閉じた後にのみ実行される。フォームの呼び出しは固まってしまう;''Form1''を動かすこともそれが持つどのボタンも押すことができない。
  
ShowModal is a function that returns an integer result, see [[doc:lcl/forms/tmodalresult.html|TModalResult]]. The returned value allows branching based on how the form was closed.
+
ShowModalは整数値の結果を返す関数である。[[doc:lcl/forms/tmodalresult.html|TModalResult]]を参照のこと。返された値によりどのようにフォームを閉じたかを分岐できる。
  
== お互いを呼び出す2つのフォーム ==
+
== お互いを呼び出すことのできる2つのフォーム ==
  
It is generally good program design to avoid calling the main form from a second form. It is better to return to the main form by closing the second form (as in the previous example) and letting focus revert to the main form. It is, however, possible to call the main from from a sub-form, as this example will show.
+
2番目のフォームからメインフォームを呼ぶことを避けるのは一般的に良いプログラム設計である。2番目のフォームを閉じることによって、(前の例のように)メインフォームへ戻り、メインフォームにフォーカスを返すこともより好ましい。しかしこの例が示すように、サブフォームからメインフォームを呼ぶことは可能である。
  
In order for Form1 to call Form2, Unit1 must have Unit2 in its Uses clause. Conversely, for Form2 to call Form1, Unit2 must have Unit1 in its Uses clause. This leads to a potential Circular Unit Reference compiler error, with each Unit referring to the other. This example also shows how to avoid the Circular Unit Reference error.
+
Form1がForm2を呼ぶために、Unit1はそのUses節の中にUnit2を持っていなければならない。
 +
逆にForm2がForm1を呼ぶために、Unit2はそのUses節にUnit1を持っていなければならない。これはお互いのユニットが他のユニットを参照しあう、潜在的な循環ユニット参照コンパイラエラーにつながる。この例は循環ユニット参照エラーをどのように避けるかも示している。
  
To set up this example, either modify the forms from the previous example ([[Form_Tutorial#The_use_of_a_second_form|The Use of a Second Form]]), or create a new project with two forms, each having a single button. Set the caption of Form1.Button1 to "Open Form2", and set the caption of Form2.Button1 to "Open Form1".
+
この例を設定するために、前の例([[Form Tutorial/ja#2つ目のフォームを使う|2つ目のフォームを使う]])のフォームを改造する、、もしくはそれぞれが1個のボタンを持つ新しいプロジェクトを作る。Form1.Button1のキャプションを「Open Form2」、Form2.Button1のキャプションを「Open Form1」と設定する。
  
If you are modifying the previous example, remove "Unit2" from the Uses clause in Unit1.
+
もし、前の例を改造するなら、Unit1のUses節から「Unit2」を除くこと。
  
Add a Uses clause to the [[Implementation]] section of each [[Unit]]. Add Unit2 to the Unit1 clause, and add Unit1 to the Unit2 clause, as below:
 
  
 +
それぞれの[[Unit]]の[[Implementation]]セクションにUses節を加える。Unit1の節にUnit2を、Unit2の節にUnit1を加えると以下のようになる:
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
 
unit Unit1;
 
unit Unit1;
Line 198: Line 200:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now change the code located behind the OnClick event of the button event handler:
+
ボタンイベントハンドラのOnClickイベント以後にあるコードを変更する:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 220: Line 222:
 
== 他のフォームに変数を渡す ==
 
== 他のフォームに変数を渡す ==
  
In accordance with the above example [[Form Tutorial#The use of a second form | The Use of a Second Form]], it is possible to declare a [[Global_variables | global variable]] in the interface part of Unit2. This would allow access in both Unit1 and Unit2 to one and the same [[Variable | variable]] (This is because by putting Unit2 in the Uses clause of Unit1, all variables declared in the interface part of Unit2 are also available in Unit1). This practice should be limited to a minimum, as it quickly becomes difficult to remember which individual variables are in scope, potentially increasing errors. It is better to use [[Local_variables|local variables]], define a [[Object_Oriented_Programming_with_Free_Pascal_and_Lazarus#Properties| Property]] in a [[Class]] or, alternatively, as a variable in a class.
+
上記の例、[[Form Tutorial/ja#2つ目のフォームを使う|2つ目のフォームを使う]]によれば、Unit2のinterface部で[[Global_variables | グローバル変数]]を宣言することができる。これはUnit1とUnit2の双方が同じ[[Variable | 変数]]にアクセスすることを可能にする(Unit1のUses節にUnit2を置くことにより、Unit2のinterface部で宣言されたすべての変数も、Unit1で利用できるようになる)。この習慣は、どの個々の変数がスコープにあるかを記憶することが急速に困難となり、潜在的なエラーの元となるので、最小限にすべきである。[[Local_variables|ローカル変数]]を使い、[[Class]]の中の[[Object_Oriented_Programming_with_Free_Pascal_and_Lazarus#Properties| プロパティ]]を、あるいは別にクラスの中の変数として定義することがより好ましい。
  
In the next project, a second form is opened by clicking on the button. In this case, in the unit of the main form is counted how often the second shape has been shown. In this second form you can ask to see by clicking on the button, how often it has been opened:
+
次のプロジェクトでは、2番目のフォームをボタンのクリックで開く。この時、メインフォームのユニットが2番目のフォームが何回表示されたかを勘定する。この2番目のフォームでボタンをクリックすることで、何回それが開かれたを見るために尋ねることができる:
* a new project with two forms, each a button on it and their OnClick events to evaluate.
+
* 2つのフォームを持ち、それぞれボタンがあり、それらを評価するOnClickイベントがある新しいプロジェクト。
* now, in the public part of the [[Class]] TForm2 (Unit2) a variable (in this case a [[Const | constant]], which we defined as variable abuse) of type integer with the name Count create:
+
* ここで、Countという名前の整数型の[[Class]] TForm2のpublic部の変数(この場合変数の悪用として定義する[[Const | 定数]])が作り出す:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 233: Line 235:
 
     procedure Button1Click(Sender: TObject);
 
     procedure Button1Click(Sender: TObject);
 
   private
 
   private
     { private declarations }
+
     { プライベート宣言 }
 
   public
 
   public
     { public declarations }
+
     { パブリック宣言 }
     const Count: Integer = 0;  //here
+
     const Count: Integer = 0;  //ここ
 
   end;
 
   end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
* now even customize the Button event handler accordingly:
+
* Buttonイベントハンドラをそれに応じてカスタマイズする:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 247: Line 249:
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
   inc(Form2.Count);  //Increase the counter by 1 on each call to Form2
+
   inc(Form2.Count);  //Form2が呼ばれるたびにカウンタを1つ増やす
 
   Form2.ShowModal;   
 
   Form2.ShowModal;   
 
end;
 
end;
Line 261: Line 263:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In this way you can in Unit1 access all public variables/properties/functions/procedures (general methods) of Unit2.
+
このようにUnit1の中でUnit2のすべてのパブリック変数/プロパティ/関数/プロシージャ(一般的なメソッド)にアクセスできる。
  
== 他のフォールに関する事柄 ==
+
== 他のフォームに関する事柄 ==
  
=== 他のフォームをメインフォームとして使う ===
+
=== もう1つのフォームをメインフォームとして使う ===
  
If you determine after a while that you want to have displayed rather a different form or new form for the view at startup, you can under the main menu Project -> Project Options -> Forms:
+
もし起動時にしばらく異なるフォームもしくは視覚的に新しいフォームを表示させたいと決めたとすると、メインメニュー、プロジェクト -> プロフェクトオプション -> フォーム...を実行できる:
  
[[Image:Projekteinstellungen_Formulare.png]]
+
[[Image:ProjectOptionForms_ja.png]]
  
Alternatively, you can display, in Mainmenu -> Project -> Show .lpr file, that Project.lpr (Pascal code of the main program) and create the first form to be displayed as the first:
+
また別に、メインメニュー、プロフェクト -> 表示(Lazarus 3.2ではここに表示 - Showというメニューはない) .lprファイル、Project.lpr(メインプログラムのPascalコード)から、最初に表示されるフォームを作ることができる:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
 
program Project1;
 
program Project1;
 
...
 
...
   Application.CreateForm(TForm2, Form2);  //Form2 is created in first and shown when the application starts
+
   Application.CreateForm(TForm2, Form2);  //Form2が最初に作られ、アプリケーションが起動されるときに表示される
 
   Application.CreateForm(TForm1, Form1);
 
   Application.CreateForm(TForm1, Form1);
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 284: Line 286:
 
* [[Remember form position and size#Using_TForm.SessionProperties and TXMLPropStorage]]
 
* [[Remember form position and size#Using_TForm.SessionProperties and TXMLPropStorage]]
  
== 動的にフォームを作るGenerate the form dynamically ==
+
== 動的にフォームを作る ==
  
 
=== Lazarusによってデザインされたフォームを動的に作る ===  
 
=== Lazarusによってデザインされたフォームを動的に作る ===  
  
You do not have to create all the forms that may be called during the term of an application at startup. Some developers generally keep none of it and delete the code automatically created when you insert a new form in a project from the Projekt.lpr out immediately. If you want to write a library that contains a couple of GUIs, you can not get around the dynamic creation of forms.
+
アプリケーション起動中に呼び出されるかもしれないすべてのフォームを作る必要はない。開発者によっては一般的にそれを保持せずにProject.lprから、プロジェクトに新しくフォームを挿入するときに自動的に生成されたコードを削除する。もし、いくつかのGUIを含むライブラリを書きたいとすると、フォームの動的生成を避けては通れない。
  
An example:
+
:
  
* new application with two forms, Form1 a button (add "Unit2" in the uses clause of unit1)
+
* 2つのフォームを持つ新しいアプリケーション、Form1にはボタン(「Unit2」を加える、Uses節にUnit1)
* open the Projekt.lpr (Project -> lpr file.)
+
* open the Project.lpr (プロジェクト -> lprファイル (注)先にあるように現在このメニューはない、lprファイルを開きたいときは、ファイル -> 開く...から)
* delete the line "Application.CreateForm(TForm2, Form2);"
+
* 「Application.CreateForm(TForm2, Form2);」行を削除
* in the OnClick event of Button1, Form1 add following code:
+
* Form1、Button1のOnClickイベントに以下のコードを追加する:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
   Form2:=TForm2.Create(Nil);  //Form2 is created
+
   Form2:=TForm2.Create(Nil);  //Form2が生成される
   Form2.ShowModal;            //Form2 is displayed
+
   Form2.ShowModal;            //Form2が表示される
   FreeAndNil(Form2);          //Free Form2
+
   FreeAndNil(Form2);          //Form2解放
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
* now simply start
+
* 単純に起動
  
=== 新しいフォームを動的に作り出す ===
+
=== 新しいフォームを動的に生成する ===
  
The following example will demonstrate how new forms can be generated in code, without using  the Form Designer,.
+
以下はフォームデザイナを使わずにコードで新しいフォームを生成する例を示している。
  
In this example clicking a button should open another form that itself contains a button. Clicking this second button makes a message appear warning that the form will close; then the form is closed.
+
この例ではボタンをクリックすることで、自身にボタンを含むもう1つのフォームが開くはずである。この2番目のボタンをクリックするとフォームを閉じると警告が出て、フォームが閉じられる。
  
* Create a new application with a form and a button
+
* 1つのフォームと1つのボタンを持つ新しいアプリケーションを作る
* In the OnClick event of Button1, in Form1, add the following code:
+
* Form1、Button1のOnClickイベントに以下のコードを加える:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 340: Line 342:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
{{Note| If a component has been created with an owner (TButton.Create(Owner: TComponent)), it belongs to the owner, who is then responsible to free the component. Thus MyButton is freed automatically by freeing MyForm.}}
+
{{Note|もしコンポーネントが所有者(TButton.Create(Owner: TComponent))から生成されると、それはコンポーネントを解放する責任のある所有者に属する。そのため、MyButtonはMyFormを解放することによって自動的に解放される。}}
  
* now the event handler for MyButton.OnClick must be created. To this end, write in the private section of class TForm1 the following procedure declaration:
+
* ここでMyButton.OnClickに対するイベントハンドラ、MyButton.OnClickが生成されなければならない。これを終了させるため、class TForm1のprivateセクションに以下のプロシージャ宣言を書くこと:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 352: Line 354:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
press {{keypress|Ctrl]}}+{{keypress|Shift}}+{{keypress|C}} (code completion) and insert the following code as the handler implementation:
+
*  {{keypress|Ctrl]}}+{{keypress|Shift}}+{{keypress|C}} (コード補完)を押し、ハンドラ実装として以下を挿入すること:
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 363: Line 365:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
* now the project can be compiled and executed
+
* ここでプロジェクトがコンパイルされ、実行できる。
  
 
== 以下も参照のこと ==
 
== 以下も参照のこと ==

Latest revision as of 16:44, 25 March 2024

Deutsch (de) English (en) suomi (fi) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

日本語版メニュー
メインページ - Lazarus Documentation日本語版 - 翻訳ノート - 日本語障害情報

Lazarusでフォームを用いるための短いイントロダクション。

フォームとは何か

フォーム (class TForm)はapplicationのユーザーインターフェースとなるウインドウもしくはダイアログを表す。それは他のcomponents(例えば、ボタン、ラベル、編集ボックス、画像など)が挿入されるコンテナである。

初めてのGUIアプリケーション

Lazarusのインストールが成功し、新しいアプリケーションプロジェクトを始めると、空のフォームが作られる。そうでなければ、新しい GUI アプリケーションを メインメニュー -> プロジェクト -> 新規プロジェクト -> アプリケーション OKで作ることができる。

今、新しい、全機能が使えるフォームを持つプロジェクトが生成された:

Form1 Designmodus.png

新しく作られたプロジェクトを実行するには、単にF9キーを押す、もしくはマウスでメインメニューアイコンStart.png(あるいは メインメニュー: 実行 -> 実行)をクリックする。プロジェクトはコンパイル、実行される。

特に面白いことが起こるわけではない。フォーム(Form1)はわずかに外見が変わり、ラスタグリッドの点々(個々のコンポーネントの位置付けの助けとなる)が消える(グリッドの点が見えてる間は、それがデザインモードにあることがわかる):

Form1 Designmodus.png -> Form1 Runmodus.png

次にフォームの上にTButtonを配することができる。これは後に第2のフォームを開くことを可能にするために使われる:

コンポーネントパレット上のStandardタブからTButtonを選ぶ。

TButtonStandardpalette.png

そしてフォームをクリックする: 「Button1」の名前とキャプションでForm1にボタンが配される。

このボタンに何かをさせるために、それをクリックすると、何か意図したことをするようにボタンに告げなければならない。これは単純なイベントである。このためクリックされた後に呼ばれる イベントハンドラが必要である。マウスに対するイベントハンドラはボタンはButton1をダブルクリックすることにより、極めて簡単に行える(もしくはオブジェクトインスペクタで、Button1 -> イベントタブ -> OnClick、次いで[...]をクリック)。これはButton1がクリックされると(設計段階ではなくruntime)にいつも呼ばれるプロシージャTForm1.Button1Clickをコードエディタに作り出す。

NeuesProjekt2.png


アプリケーションにButton1をクリックした後に何かをさせるために、プロシージャTForm1.Button1ClickのBeginEndの間にこのようにコードを加える:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Caption:='My first Form';  //Form1のキャプションが変更される(フォーム冒頭のテキスト)
  //or
  Button1.Caption:='Hello';  //ボタンのキャプションが変更される(ボタンのテキストが表示される)
end;

そして、F9を押し、フォームを起動させる、ボタンをクリックしたときにフォームキャプションとボタンキャプションの双方が変わることに注意すること。

ちょっとした実験を行い、スタンダードコンポーネントの使い方を学んだ。始めるにあたり、以下のコンポーネントの例を試してみることを勧める:

TButton tbutton.png
TLabel tlabel.png
TEdit tedit.png
TMemo tmemo.png
TToggleBox ttogglebox.png
TCheckBox tcheckbox.png
TRadioButton tradiobutton.png
TListBox tlistbox.png
TComboBox tcombobox.png
TScrollBar tscrollbar.png

ここにさらに役に立つ情報があるLazarusチュートリアル

2つ目のフォームを使う

このチュートリアルでは1つのプロジェクトで複数のフォームを使う方法を示す。この例で2つだけのフォーム、Form1(メインフォーム)とForm2が作られるが、追加のフォームについてもその仕方は同一である。

クリックされると新しいフォームを開くボタンがメインフォームにある。新しいフォームはまた、クリックされるとそれ自身を閉じ、メインフォームに帰ってくるボタンを有する。 もし最初のチュートリアル、 初めてのGUIアプリケーションを終えているのなら、TForm1.Button1Clickプロシージャのbeginとendの間のコードを削除しておく必要がある。そうしないと、新たなプロジェクト(アプリケーション)フォームにボタンを置き、キャプション(ボタンの上の表示されるテキスト)を入力し、OnClickイベントハンドラをこのButton1に作らなければならない。
これを行うためボタンを選択(一度クリックする)し、オブジェクトインスペクタで、プロパティ -> Caption、「Open Form2」と入力する。

ObjectInspector ja.png

2つ目のフォーム(Form2)をプロジェクトに加えるため、メインメニューで ファイル -> 新規フォームを実行する。Form2にボタンを置きそれにOnClickイベントを作る。このボタンのキャプションを「Close」に変更する。

いま、このプロジェクトは2つのフォームを持ち、それぞれはメインメニュー、プロジェクト -> フォーム...(もしくはキーの組み合わせ Shift + F12)で選択、表示できる。また別に、ソースエディタでUnitタブで選択もでき、F12でコードエディタとUnitに割り当てられたフォームデザイナとを切り替えできる。

source code editorUnit (Unit1)が割り当てられたForm1に行きUsesに節「Unit2を加える:

uses
  Classes, SysUtils, ... , Unit2;  // それぞれのユニットがコンマ(,)で区切られていることと
                                   // uses 節がセミコロン(;)で終わることは重要である。

今、Unit2(即ちForm2)をUnit1から呼び出すことができる。

次に、Form1に属するボタンのOnClickイベントを編集する:

unit Unit1;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.ShowModal;  //Form2を表示、フォーカスはForm2に来る。
end;
unit Unit2;
...
procedure TForm2.Button1Click(Sender: TObject);
begin
  Close;  //Form2を閉じる
end;

これで、プロジェクトを起動(Start.png もしくは F9)し、Form1のボタンをクリックすることでForm2を開くことができる。

ShowとShowModalの違い

Showおよび ShowModalメソッドは双方ともフォームを見えるようにする。ShowShowModalの違いを見るために:

  • 前の例、Form12つ目のフォームを使うに2番目のTButtonを加え拡張する。
  • Button1を選択しプロパティNamebtnShowに、CaptionShowに変更する。
  • Button2を選択しプロパティNamebtnShowModalに、CaptionShowModalに変更する。
  • OnClickイベントを作る(もしくは、変更)し2つのボタンのイベントハンドラを以下のようにする:
procedure TForm1.btnShowClick(Sender: TObject);
begin
  Form2.Show;
  ShowMessage('Form2 is opened!');
end;

procedure TForm1.btnShowModalClick(Sender: TObject);
begin
  Form2.ShowModal;
  ShowMessage('Form2 is opened and closed again!');
end;
  • 起動してその差を見よう!
Show

このプロシージャはフォームを見える状態にし、呼んだコントロール(Form1)のコードの実行を継続する。この例ではメッセージ、(ShowMessage) はほとんどForm2と同時に現れる。またForm1を呼んで使い続けることができる。例えばそれを動かしたり、Showボタンを押すことができる(しかしShowModalをクリックするとエラーが発生するだろう)。

<myForm>.Show;を用いる代わりに、<myForm>.Visible:=True;を用いることもできる、Showメソッドは以下を行う:

procedure TCustomForm.Show;
begin
  Visible := True;
  BringToFront;
end;

<myForm>.Show;<myForm>.Visible:=True;の唯一の違いは、Showを用いてフォームを表示すると、現在表示されているすべての他のフォームの上に表示されることである。

ShowModal

ShowModalは新しく開かれているフォーム(Form2)が閉じられるまで呼び出したコントロール(Form1)を停止させる。例では、ShowModalの直後のShowMessage行はForm2が閉じた後にのみ実行される。フォームの呼び出しは固まってしまう;Form1を動かすこともそれが持つどのボタンも押すことができない。

ShowModalは整数値の結果を返す関数である。TModalResultを参照のこと。返された値によりどのようにフォームを閉じたかを分岐できる。

お互いを呼び出すことのできる2つのフォーム

2番目のフォームからメインフォームを呼ぶことを避けるのは一般的に良いプログラム設計である。2番目のフォームを閉じることによって、(前の例のように)メインフォームへ戻り、メインフォームにフォーカスを返すこともより好ましい。しかしこの例が示すように、サブフォームからメインフォームを呼ぶことは可能である。

Form1がForm2を呼ぶために、Unit1はそのUses節の中にUnit2を持っていなければならない。 逆にForm2がForm1を呼ぶために、Unit2はそのUses節にUnit1を持っていなければならない。これはお互いのユニットが他のユニットを参照しあう、潜在的な循環ユニット参照コンパイラエラーにつながる。この例は循環ユニット参照エラーをどのように避けるかも示している。

この例を設定するために、前の例(2つ目のフォームを使う)のフォームを改造する、、もしくはそれぞれが1個のボタンを持つ新しいプロジェクトを作る。Form1.Button1のキャプションを「Open Form2」、Form2.Button1のキャプションを「Open Form1」と設定する。

もし、前の例を改造するなら、Unit1のUses節から「Unit2」を除くこと。


それぞれのUnitImplementationセクションにUses節を加える。Unit1の節にUnit2を、Unit2の節にUnit1を加えると以下のようになる:

unit Unit1;
...
implementation

uses
  Unit2;  
...
unit Unit2;
...
implementation

uses
  Unit1;  
...

ボタンイベントハンドラのOnClickイベント以後にあるコードを変更する:

unit Unit1;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;  
end;
unit Unit2;
...
procedure TForm2.Button1Click(Sender: TObject);
begin
  Form1.Show; 
end;

他のフォームに変数を渡す

上記の例、2つ目のフォームを使うによれば、Unit2のinterface部で グローバル変数を宣言することができる。これはUnit1とUnit2の双方が同じ 変数にアクセスすることを可能にする(Unit1のUses節にUnit2を置くことにより、Unit2のinterface部で宣言されたすべての変数も、Unit1で利用できるようになる)。この習慣は、どの個々の変数がスコープにあるかを記憶することが急速に困難となり、潜在的なエラーの元となるので、最小限にすべきである。ローカル変数を使い、Classの中の プロパティを、あるいは別にクラスの中の変数として定義することがより好ましい。

次のプロジェクトでは、2番目のフォームをボタンのクリックで開く。この時、メインフォームのユニットが2番目のフォームが何回表示されたかを勘定する。この2番目のフォームでボタンをクリックすることで、何回それが開かれたを見るために尋ねることができる:

  • 2つのフォームを持ち、それぞれボタンがあり、それらを評価するOnClickイベントがある新しいプロジェクト。
  • ここで、Countという名前の整数型のClass TForm2のpublic部の変数(この場合変数の悪用として定義する 定数)が作り出す:
unit Unit2;
...
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { プライベート宣言 }
  public
    { パブリック宣言 }
    const Count: Integer = 0;   //ここ
  end;
  • Buttonイベントハンドラをそれに応じてカスタマイズする:
unit Unit1;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
  inc(Form2.Count);  //Form2が呼ばれるたびにカウンタを1つ増やす
  Form2.ShowModal;   
end;
unit Unit2;
...
procedure TForm2.Button1Click(Sender: TObject);
begin
  ShowMessage('Form2 would be displayed '+IntToStr(Count)+' times');
end;

このようにUnit1の中でUnit2のすべてのパブリック変数/プロパティ/関数/プロシージャ(一般的なメソッド)にアクセスできる。

他のフォームに関する事柄

もう1つのフォームをメインフォームとして使う

もし起動時にしばらく異なるフォームもしくは視覚的に新しいフォームを表示させたいと決めたとすると、メインメニュー、プロジェクト -> プロフェクトオプション -> フォーム...を実行できる:

ProjectOptionForms ja.png

また別に、メインメニュー、プロフェクト -> 表示(Lazarus 3.2ではここに表示 - Showというメニューはない) .lprファイル、Project.lpr(メインプログラムのPascalコード)から、最初に表示されるフォームを作ることができる:

program Project1;
...
  Application.CreateForm(TForm2, Form2);  //Form2が最初に作られ、アプリケーションが起動されるときに表示される
  Application.CreateForm(TForm1, Form1);

プログラムの終わりで形のプロパティを保存する

動的にフォームを作る

Lazarusによってデザインされたフォームを動的に作る

アプリケーション起動中に呼び出されるかもしれないすべてのフォームを作る必要はない。開発者によっては一般的にそれを保持せずにProject.lprから、プロジェクトに新しくフォームを挿入するときに自動的に生成されたコードを削除する。もし、いくつかのGUIを含むライブラリを書きたいとすると、フォームの動的生成を避けては通れない。

例:

  • 2つのフォームを持つ新しいアプリケーション、Form1にはボタン(「Unit2」を加える、Uses節にUnit1)
  • open the Project.lpr (プロジェクト -> lprファイル (注)先にあるように現在このメニューはない、lprファイルを開きたいときは、ファイル -> 開く...から)
  • 「Application.CreateForm(TForm2, Form2);」行を削除
  • Form1、Button1のOnClickイベントに以下のコードを追加する:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2:=TForm2.Create(Nil);  //Form2が生成される
  Form2.ShowModal;            //Form2が表示される
  FreeAndNil(Form2);          //Form2解放
end;
  • 単純に起動

新しいフォームを動的に生成する

以下はフォームデザイナを使わずにコードで新しいフォームを生成する例を示している。

この例ではボタンをクリックすることで、自身にボタンを含むもう1つのフォームが開くはずである。この2番目のボタンをクリックするとフォームを閉じると警告が出て、フォームが閉じられる。

  • 1つのフォームと1つのボタンを持つ新しいアプリケーションを作る
  • Form1、Button1のOnClickイベントに以下のコードを加える:
procedure TForm1.Button1Click(Sender: TObject);
var
  MyForm: TForm;
  MyButton: TButton;
begin
  MyForm:=TForm.Create(nil);             
  MyForm.SetBounds(100, 100, 220, 150);  
  MyForm.Caption:='My dynamic created form';

  MyButton:=TButton.create(MyForm);  
  MyButton.Caption:='Close my form';
  MyButton.SetBounds(10, 10, 200, 30);
  MyButton.Parent:=MyForm;

  MyButton.OnClick:=@MyButtonClick;     

  MyForm.ShowModal;                     

  FreeAndNil(MyForm);     
end;
Light bulb  Note: もしコンポーネントが所有者(TButton.Create(Owner: TComponent))から生成されると、それはコンポーネントを解放する責任のある所有者に属する。そのため、MyButtonはMyFormを解放することによって自動的に解放される。
  • ここでMyButton.OnClickに対するイベントハンドラ、MyButton.OnClickが生成されなければならない。これを終了させるため、class TForm1のprivateセクションに以下のプロシージャ宣言を書くこと:
TForm1 = class(TForm)
...
private
  { private declarations }
  procedure MyButtonClick(Sender: TObject);
  • Ctrl]+ Shift+C (コード補完)を押し、ハンドラ実装として以下を挿入すること:
procedure TForm1.MyButtonClick(Sender: TObject);
begin
  Showmessage('Attention, closing my dynamically created form!');
  if Sender is TButton then
    TForm(TButton(Sender).Parent).Close;
end;
  • ここでプロジェクトがコンパイルされ、実行できる。

以下も参照のこと