掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
TControlを変数で指定したい。 (ID:48872)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
こんな方法ではどうですか? 環境が書かれていなかったのですが、とりあえず DelphiXE8 で試してみました。 【 FMXの場合 】 unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.Edit, FMX.StdCtrls, FMX.Layouts, FMX.ListBox; type TForm1 = class( TForm ) procedure FormCreate ( Sender : TObject ); function CreateMyControl( Parent : TObject; AName : String ) : TControl; private { private 宣言 } MyControl : TControl; public { public 宣言 } end; TTest = class( TControl ); var Form1: TForm1; implementation {$R *.fmx} // フォーム生成処理 procedure TForm1.FormCreate( Sender : TObject ); begin // コントロール作成 MyControl := CreateMyControl( Self, 'TLabel' ); // MyControl := CreateMyControl( Self, 'TEdit' ); // MyControl := CreateMyControl( Self, 'TButton' ); // MyControl := CreateMyControl( Self, 'TListBox' ); // プロパティ設定 // ※ もちろんプロパティはキャストするクラスにより変わります。 TLabel( MyControl ).Width := 100; TLabel( MyControl ).Height := 100; TLabel( MyControl ).Position.X := 100; TLabel( MyControl ).Position.Y := 100; TLabel( MyControl ).Enabled := TRUE; TLabel( MyControl ).Visible := TRUE; TLabel( MyControl ).Text := 'KONNOYA'; end; // コントロール生成処理 function TForm1.CreateMyControl( Parent : TObject; AName : String ) : TControl; var ClassFinder : TClassFinder; AClass : TPersistentClass; AControl : TControl; begin // クラスファインダー生成 ClassFinder := TClassFinder.Create; try // クラスを取得 AClass := ClassFinder.GetClass( AName ); AControl := AClass.NewInstance as TControl; AControl.Create( Self ); AControl.Parent := Parent; finally // クラスファインダー解放 ClassFinder.DisposeOf; end; // コントロールを返す Result := AControl; end; end. 【 VCLの場合 】 unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections; type TForm1 = class( TForm ) procedure FormCreate ( Sender : TObject ); function CreateMyControl( Parent : TObject; AName : String ) : TControl; private { Private 宣言 } MyControl : TControl; public { Public 宣言 } end; var Form1: TForm1; implementation {$R *.dfm} // フォーム生成時処理 procedure TForm1.FormCreate( Sender : TObject ); begin // コントロール生成 MyControl := CreateMyControl( Self, 'TLabel' ); // MyControl := CreateMyControl( Self, 'TEdit' ); // MyControl := CreateMyControl( Self, 'TButton' ); // MyControl := CreateMyControl( Self, 'TListBox' ); // プロパティ設定 // ※ もちろんプロパティはキャストするクラスにより変わります。 TLabel( MyControl ).Width := 100; TLabel( MyControl ).Height := 100; TLabel( MyControl ).Left := 100; TLabel( MyControl ).Top := 100; TLabel( MyControl ).Enabled := TRUE; TLabel( MyControl ).Visible := TRUE; TLabel( MyControl ).Caption := 'KONNOYA'; end; // コントロール生成処理 function TForm1.CreateMyControl( Parent : TObject; AName : String ) : TControl; type TPCDictionary = TDictionary< String, TPersistentClass >; var ClassList : TPCDictionary; ClassFinder : TClassFinder; AClass : TPersistentClass; AControl : TControl; begin // ディクショナリ作成 ClassList := TPCDictionary.Create; ClassList.Add( 'TLabel' , TLabel ); ClassList.Add( 'TEdit' , TEdit ); ClassList.Add( 'TButton' , TButton ); ClassList.Add( 'TListBox', TListBox ); // クラスを取得 ClassList.TryGetValue( AName, AClass ); AControl := AClass.NewInstance as TControl; AControl.Create( Self ); AControl.Parent := Parent; // コントロールを返す Result := AControl; end; end. VCLでは何故か TClassFinder が参照する FClassList.Items に クラスのリストが格納されていない為、 自力でTDictionaryを作り、そこから探す様にしています。
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.