我在程序中动态生成了一些控件,但不知怎么标识动态生成的这些控件,给Name属性赋值类型没用对,看了帮助也不是太懂,有没有高手帮忙解决一下
解决方案 »
- D2010中的字符串操作问题。
- 向word中追加内容,大家看看这个函数有什么问题?
- 请大家留下QQ联系,多交流DELPHI思想。加我的送一套大商场管理系统源代码?
- 哪位高人有InstallShield Professional Evaluation7.01的序列号啊!
- 请问出现未注册类型,一般是怎么搞的?
- 给字符串某位赋值怎么出错了???
- DELPHI+ADO+MSSQL,客户端分发是是否要和SQL客户端一起分发?
- **** 100分 ***** ansipos , ics
- 请问如何实现四不舍而且五入
- 在Delphi中如何使用ADO?
- 请教:如何让SQL中的日期默认为日期无时间,如2002-10-10
- 请教关于文语转换技术细节,高分!(再加分)
//其他属性
....
Free;
var ButtonTest:TButton;
begin
ButtonTest:=TButton.Create(Self);
ButtonTest.Name:='abc';
//我在这一步想给他一个标识,以便在程序中控制它,但错误提示类
不对,帮助说是TComponentName类型的,但对这个类型的解释却是如下:
Type TComponentName=Type Strings;
不知你明不明白问题怎么回事
end;
begin
ButtonTest:=TButton.Create(Self);
ButtonTest.Name:='abc';
ButtonTest.Free;
end;在DELPHI 5 里能编译通过!
你说的好像能实现,但我觉得效率会比较低,因为我可能会在一个窗口放很多控件
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm) //动态按钮的响应事件
Procedure buttononclick(Sender:TObject); procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.buttononclick(Sender: TObject);
begin
case TButton(Sender).Tag of
1: showmessage('This is Test1');
2: showmessage('This is Test2');
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var
ButtonTest:TButton;
btntop,
btnleft:Integer;begin
btntop:=10;
btnleft:=10;
ButtonTest:=TButton.Create(Self);
With ButtonTest do
begin
tag:=1;
Caption:='test1';
parent:= self;
Top:=btntop;
Left:=btnleft;
onclick := buttononclick;
end; btntop:=100;
btnleft:=100;
ButtonTest:=TButton.Create(Self);
With ButtonTest do
begin
tag:=2;
Caption:='test2';
parent:= self;
Top:=btntop;
Left:=btnleft;
onclick := buttononclick;
end;
end;end.
//动态创建控件******************************************************************
//==============================================================================
function DynaCreateComponent(Owner: TComponent; CompType: TControlClass; CompName: String; Left,Top,Width,Height:Integer): TControl;
begin
if (Owner.FindComponent(CompName)<>nil) and not(Owner.FindComponent(CompName) is TControl) then
begin
Result := nil;
exit;
end;
Result := Owner.FindComponent(CompName) as TControl;
if Result=nil then
begin
Result := CompType.Create(Owner);
with Result do
begin
if Owner is TwinControl then
begin
SetBounds(Left,Top,Width,Height);
Parent := TwinControl(Owner);{如果是可视构件,则显示之}
if Owner is TForm then TForm(Owner).ActiveControl := TWinControl(Result);{设置窗口焦点}
end;
end;
Result.Name := CompName;
end
else {Result<>Nil}
if not(Result is CompType) then
begin
Result := nil;
Exit;
end;
Result.Visible := True;
end;
{ 对于未知数量的控件组,利用TList
var ControlList: Tlist; CreateNum: integer;
const CreateClass : TControlClass = TButton;//可以任意修改TControlClass = TEdit或TPanel等。效果一样。
var i:integer; APoint: Pointer;
ControlList := TList.Create;
ControlList.Clear;
CreateNum := 10;
for i:=1 to CreateNum do
begin
APoint := Pointer(DynaCreateComponent(self,CreateClass,'Button_' + IntToStr(i),0,i*20+1,60,20));//创建
ControlList.Add(APoint);
end;
TButton(ControlList.Items[i]).Caption := 'XXXX';}
比如caption来储存string名称,tag来储存integer名称