Sign in Join
Wiki copytemporary

TRadioButton/ja

Deutsch · English · Français · Suomi · 日本語

TRadioButton は相互排他的 - もし1つのボタンが選択されると、同じグループの他のものが選択できない - に他のラジオボタンともに機能するボタン選択を現すコンポーネントである。TRadioButtonはコンポーネントパレットのStandardタブで選択できる。

FormでTRadioButtonを使うには、単にコンポーネントパレットStandardから、選んでフォーム上でクリックし、配置する。
ラジオボタンは何でも選択可能なので、通常は単一のラジオボタンを用いることは意味をなさない。そのため、個々のラジオボタンの代わりにTRadioGroupを用いることもできる。

ソースコードのどこからでも、クエリ、Status := <RadioButton>.Checked;により、有効か、無効かラジオボタンの状態を取得できる。通常のブール値としてCheckedを使うことができる。このため、割り当て、<RadioButton>.Checked := True;が可能である。

簡単な例

pascal
procedure TForm1.btnPaintClick(Sender: TObject);
begin
  if rbRed.Checked   then Color:=clRed;
  if rbGreen.Checked then Color:=clLime;
  if rbBlue.Checked  then Color:=clBlue;
end;

->

イベントの使い方

上の例との相違は、ボタンのクリックではなく、すでにラジオボタンの1つが選択された状態で再描画することである。

ソースコードより、ボタンとそのOnClickイベントハンドラを削除することにより、前の例を変更できる。新しい例もまた、簡単に作れる:

pascal
procedure TForm1.rbRedChange(Sender: TObject);
begin
  Self.Color:=clRed;    //「Self」で、メソッドが存在するオブジェクトを選択する(メソッド:rbRedChange/オブジェクト:Form1)
end;

procedure TForm1.rbGreenChange(Sender: TObject);
begin
  Form1.Color:=clLime;  //直接オブジェクト''Form1''を選択できるが、お粗末である。 
                        //なぜなら、クラス「Form1」の他のオブジェクトが生成されてないからだ
end;                    //訳注: clLimeとなっているが本文中ではclGreenである

procedure TForm1.rbBlueChange(Sender: TObject);
begin
  Color:=clBlue;        //もしくは「Self」を取り除く、そしてコンパイラは自動的に
end;                    //それ自身のオブジェクトを探し出すだろう

->

グルーピング

ラジオボタンをフォームに追加すると、そのフォームが親(ラジオボタンを含むコントロール)となる。ラジオボタンを選択するたびに(クリックまたはコードによって)、各設定で<RadioButton>.Checked:=True;が行われると、同じ親を持つ異なるラジオボタンが選択されているかどうかを確認し、もしそうであれば、そのプロパティCheckedがFalseに変更される。
もし複数のラジオボタンをフォームで使い、異なる、独立した選択を提供するように設計したいのであれば、ラジオボタンをグループ化しなければならない。このため完成したコンポーネントTRadioGroupがある、または(例えば、TPanel、TGroupBox、TNotebook、TPageControlなどで)コントロールにまとめることになる。
以下の例はどのようにラジオボタンをまとめることができるかを示している:

簡単な例を変更するか、新しいアプリケーションを作り:

pascal
procedure TForm1.btnPaintClick(Sender: TObject);
begin
  if rbRed.Checked   then Color:=Brightness or clRed;
  if rbGreen.Checked then Color:=Brightness or clLime;
  if rbBlue.Checked  then Color:=Brightness or clBlue;  
end;
pascal
function TForm1.Brightness: TColor;
begin
  Result:=0;
  if rbBrightMedDark.Checked then Result:=$888888;
  if rbBrightBright.Checked  then Result:=$DDDDDD;
end;

->

以下も参照のこと


From the Free Pascal wiki, page "TRadioButton/ja", as it stood on 2024-03-20 after a change by Ariben, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2024

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
AribenCreated page
Ariben
Ariben以下も参照のこと
Ariben
Ariben簡単な例
Aribenイベントの使い方
Aribenグルーピング
Ariben簡単な例
Aribenイベントの使い方
Aribenグルーピング
Aribenイベントの使い方

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.