同じコントロールのプロパティにアクセスするには?

解決


こたつ  2005-02-22 06:02:40  No: 13378

いつもお世話になっています。
自作コンポーネント内での処理についてなのですが、フォームに貼り付けた同じコントロールのプロパティにアクセスするにはどのように書けばいいのでしょうか?よろしくお願いします。


にしの  2005-02-22 18:49:12  No: 13379

自作コンポーネントであれば、自作コンポーネント用のTListオブジェクトを用意して、
・unitのinitializationで生成
・unitのfinalizationで破棄
・コンポーネントのconstructorで追加
・コンポーネントのdestructorで削除
とすれば、TListオブジェクトに自作コンポーネントの配列ができあがります。


りおりお  2005-02-22 19:21:47  No: 13380

でも、それだと Form1 と Form2 に配置したコンポを区別できない。
Owner からたどっって、TForm であることを確認してから、その Components と
ComponentCount をつかって型チェックしていくといいと思う。


こたつ  2005-02-22 19:37:57  No: 13381

にしの様ありがとうございます。
自作コンポーネントにスピードボタンのGroupIndexような機能を付けたいと思っています。
自作コンポーネントのプロパティを変更した場合に同じGroupIndex番号であればそれらを無効にしたいのですが、具体的にはどのように書けばいいのでしょうか?
お手数をおかけします。よろしくお願いします。


こたつ  2005-02-22 19:54:20  No: 13382

りおりお様
ありがとうございます。

言葉足らずですみません。
上記の
>GroupIndex番号であればそれらを無効にしたいのですが、、、
の'無効'ですが、それらのプロパティを操作したいということです。
よろしくお願いします。


りおりお  2005-02-22 20:21:30  No: 13383

クリックするたびに同じ 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.


こたつ  2005-02-22 20:56:40  No: 13384

りおりお様
ありがとうございます。
参考にさせていただきます。
何分、未熟者のため結果報告は時間がかかると思います。


こたつ  2005-02-22 21:13:43  No: 13385

りおりお様、にしの様
ありがとうございました。
うまくいきました。

りおりお様に教えていただいたように下記をアレンジして自作コンポーネントに組み込み作動確認しました。

>  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;


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加