いつもお世話になっています。
自作コンポーネント内での処理についてなのですが、フォームに貼り付けた同じコントロールのプロパティにアクセスするにはどのように書けばいいのでしょうか?よろしくお願いします。
自作コンポーネントであれば、自作コンポーネント用のTListオブジェクトを用意して、
・unitのinitializationで生成
・unitのfinalizationで破棄
・コンポーネントのconstructorで追加
・コンポーネントのdestructorで削除
とすれば、TListオブジェクトに自作コンポーネントの配列ができあがります。
でも、それだと Form1 と Form2 に配置したコンポを区別できない。
Owner からたどっって、TForm であることを確認してから、その Components と
ComponentCount をつかって型チェックしていくといいと思う。
にしの様ありがとうございます。
自作コンポーネントにスピードボタンのGroupIndexような機能を付けたいと思っています。
自作コンポーネントのプロパティを変更した場合に同じGroupIndex番号であればそれらを無効にしたいのですが、具体的にはどのように書けばいいのでしょうか?
お手数をおかけします。よろしくお願いします。
りおりお様
ありがとうございます。
言葉足らずですみません。
上記の
>GroupIndex番号であればそれらを無効にしたいのですが、、、
の'無効'ですが、それらのプロパティを操作したいということです。
よろしくお願いします。
クリックするたびに同じ GroupIndex どうしで排他的に色を変化させるコンポを
つくり、動的に作成してテストしました。参考にしてください。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TGroupLabel = class(TLabel)
private
FGroupIndex:integer;
public
procedure AllClear;
published
property GroupIndex:integer read FGroupIndex write FGroupIndex;
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private 宣言 }
public
procedure LabelClick(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TGroupLabel.AllClear;
var
i:integer;
f:TForm;
begin
if FGroupIndex = 0 then exit;
if Owner is TForm then
begin
f := TForm(Owner);
for i := 0 to f.ComponentCount-1 do
if f.Components[i] is TGroupLabel then
if TGroupLabel(f.Components[i]).GroupIndex = FGroupIndex then
TGroupLabel(f.Components[i]).Color := clBtnFace;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
gl:TGroupLabel;
begin
for i := 0 to 3 do
begin
gl := TGroupLabel.Create(self);
gl.Parent := self;
gl.GroupIndex := 123;
gl.Top := 10;
gl.Left := 120*i+10;
gl.Caption := 'GroupLabel '+IntToStr(i);
gl.OnClick := LabelClick;
end;
end;
procedure TForm1.LabelClick(Sender: TObject);
begin
if Sender is TGroupLabel then
begin
TGroupLabel(Sender).AllClear;
TGroupLabel(Sender).Color := clAqua;
end;
end;
end.
りおりお様
ありがとうございます。
参考にさせていただきます。
何分、未熟者のため結果報告は時間がかかると思います。
りおりお様、にしの様
ありがとうございました。
うまくいきました。
りおりお様に教えていただいたように下記をアレンジして自作コンポーネントに組み込み作動確認しました。
> if FGroupIndex = 0 then exit;
> if Owner is TForm then
> begin
> f := TForm(Owner);
> for i := 0 to f.ComponentCount-1 do
> if f.Components[i] is TGroupLabel then
> if TGroupLabel(f.Components[i]).GroupIndex = FGroupIndex >then
> TGroupLabel(f.Components[i]).Color := clBtnFace;
> end;
ツイート | ![]() |