ども、Norimakiです。
今回はコンポーネントの初期化処理を書いてみようかと。
コンポーネントの初期化処理というのは、こんな感じです。
こんな感じで、ループしてフォームを検索し、
その後、フォーム内にあるコンポーネントで
目的のコンポーネントを探し出して、
目的の処理をするというもの。
これをコンポーネント別に毎度毎度、改めてコードを書く
というのは、ちょいと面倒くさいと思い立ちまして。
ということで、こんな感じで書いてみました。
(宣言部) type TNYProc = reference to procedure (Sender:TComponent); (実装部) procedure NP_InitComponent(Comp:TComponent;Ev:TNYProc); var i,j:integer; begin for i:=0 to Application.ComponentCount -1 do begin if Application.Components[I] is TForm then begin for J:=0 to TForm(Application.Components[I]).ComponentCount -1 do begin if TForm(Application.Components[I]).Components[J] is Comp.ClassType then EV(TForm(Application.Components[I]).Components[J]); end; end; end; end; (呼び出し) procedure TForm1.Button1Click(Sender: TObject); var EV:TNYProc; begin EV:=procedure(Comp:TComponent) begin TPageControl(Comp).ActivePageIndex:=0; end; NP_InitComponent(PageControl1,EV); end;
無名メソッドというのを使って、こんな書き方ができるみたいです。
(参考)http://edn.embarcadero.com/article/39701
あとは各フォームに対して処理をしたいということもあるでしょうから、
その場合は、引数などを考慮して改良する必要があります。
ではでは。
Norimakiでした。