如何从一个窗体操作另一个窗体上的组件?(200分)

  • 主题发起人 主题发起人 qiujue1
  • 开始时间 开始时间
Q

qiujue1

Unregistered / Unconfirmed
GUEST, unregistred user!
1。例:在form1操作form2上的button?
2。如何知道form2是否存在,不存在怎么构建等等
 
我刚好有个例子,应该能满足你的要求。

function FindExistingForm(FFormName: string): TForm;
var
i: integer;
begin
Result := nil;
try
if FFormName = '' then raise Exception.Create('需要类名!');
with Application do
for i := 0 to ComponentCount - 1 do
if Components.ClassName = ('T' + FFormName) then begin
Result := TForm(Components);
Break;
end;
except
on E: Exception do
ShowMessage(E.Message)
else raise;
end;
end;
function CreateForm(FFormName: string
AOwner: TComponent): TForm;
type
TFormClass = class of TForm;
var
AFormClass: TFormClass;
begin
Result := nil;
try
if FFormName = '' then raise Exception.Create('需要类名!');
AFormClass := TFormClass(FindClass('T' + FFormName));
Result := AFormClass.Create(AOwner);
except
on E: Exception do
ShowMessage(E.Message)
else raise;
end;
end;
procedure OtherFormComponentClick(FFormName: string
FComponentName: string
AOwner: TComponent);
var
AForm :TForm;
AComponent :TComponent;
begin
AForm:= FindExistingForm(FFormName);
if AForm=nil then
begin
AForm:=CreateForm(FFormName, AOwner);
AForm.Show;
end;
AComponent:=AForm.FindComponent(FComponentName);
if AComponent<>nil then TButton(AComponent).Click;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
OtherFormComponentClick('Form2', 'MyButton', Self);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i: integer;
AComponent :TComponent;
begin
with Application do
for i := 0 to ComponentCount - 1 do
begin
AComponent:=TForm(Components).FindComponent('MyButton');
if AComponent<>nil then TButton(AComponent).Click;
end;
end;
procedure TForm1.MyButtonClick(Sender: TObject);
begin
application.MessageBox('TForm1.MyButtonClick','提示');
end;
form2要自注册。
例:
initialization
RegisterClass(TForm2);


 
哇,好快呀,谢谢。我看看,测试中。。。
 
接受答案了.
 
后退
顶部