フォーラム


ゲスト  

ようこそ ゲスト さん。このフォーラムに投稿するには 登録が必要です。

ページ: [1]
トピック: Button1, Button2, Button3...をまとめて処理するには (DelWiki)
RAN
メンバー
投稿数: 34
Button1, Button2, Button3...をまとめて処理するには (DelWiki)
on: 2013/04/14 08:17 Sun

Q:Button1, Button2, Button3…をまとめて処理するには
A:FindComponentを使って文字列でコントロールを検索できます。

Button1..10をクリックするサンプルコード

var
i: Integer;
btn: TButton;
begin
for i := 1 to 10 do
begin
btn := FindComponent('Button' + IntToStr(i)) as TButton;
if Assigned(btn) and Assigned(btn.OnClick) then
btn.OnClick(btn);
end;
end;

 
Q:Button1, Label1, CombBox1等同じ番号のコントロールを関連付けて処理するには。
A:コントロール名から番号を抜き出してそれを元にFindComponentでコントロールを検索します。

Button1が押されたらComboBox1の名前をLabel1に表示するサンプル
Button2, Button3も同じように処理するならButton2.OnClickにButton1Clickをセットします。

// Component.Nameの数値を取り出す
// 'Button1'->1
// 'Button'->0
function GetComponentNum(Name: string): Integer;
var
i: Integer;
begin
for i := Length(Name) downto 1 do
begin
if Name[i] in ['0'..'9'] then
continue;
Result := StrToInt(Copy(Name, i+1, High(Integer)));
Exit;
end;
Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
idx: Integer;
cb: TComboBox;
lb: TLabel;
begin
idx := GetComponentNum((Sender as TComponent).Name);
Assert(idx>0);
cb := FindComponent('ComboBox' + IntToStr(idx)) as TComboBox;
Assert(Assigned(cb));
lb := FindComponent('Label' + IntToStr(idx)) as TLabel;
Assert(Assigned(lb));
lb.Caption := cb.Name;
end;
ページ: [1]
WP Forum Server by ForumPress | LucidCrew
バージョン: 1.7.5 ; ページロード: 0.026 sec.