动态组件 ( 积分: 50 )

  • 主题发起人 主题发起人 dyxfkj
  • 开始时间 开始时间
D

dyxfkj

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

Type
TcyButton = Class(TButton)
Private
Public
procedure Click(Sender: TObject);
end;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Panel: TPanel;
Button: TcyButton;
begin
Panel := TPanel.Create(Nil);
Panel.Parent := Form1;

with Panel do
begin
Button := TcyButton.Create(Nil);
With Button do
begin
Parent := Panel;
OnClick := Click;
end;
end;
end;

{ Button }

procedure TcyButton.Click(Sender: TObject);
begin
Showmessage('a'); //ShowMessage 是没有问题的
Close; //Close 没有办法做, cyButton不知道应该Close谁啊? ^_^ 这儿应该怎么写?
end;

end.
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

Type
TcyButton = Class(TButton)
Private
Public
procedure Click(); override; //!!!!!
end;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Panel: TPanel;
Button: TcyButton;
begin
Panel := TPanel.Create(Nil);
Panel.Parent := Form1;

with Panel do
begin
Button := TcyButton.Create(Nil);
With Button do
begin
Parent := Panel;
//OnClick := Click; !!!!!
end;
end;
end;

{ Button }

procedure TcyButton.Click();
begin
Showmessage('a');
TForm1(Parent.Parent).Close;
end;

end.
 
TcyButton中加上一个Form特性

TcyButton = Class(TButton)
Private
FForm: TForm;
Public
procedure Click(); override; //!!!!!
property form: TForm read FForm write FForm;
end;

procedure TcyButton.Click();
begin
Showmessage('a');
if assigned(FForm) then FForm.close;
end;

动态生成时
Button := TcyButton.Create(Nil);
With Button do
begin
Parent := Panel;
form := Form1;
OnClick := Click;
end;
 
还要改这里
OnClick := Click; --------> OnClick := Button1Click;
 
还得初始Form属性?

期待更佳答案...
 
FForm: TForm;
property form: TForm read FForm write FForm;
form := Form1;
if assigned(FForm) then FForm.close;

就这几句话, 还复杂吗?
初始Form属性是什么意思?
 
设置Button 的 Form属性!
楼上兄弟,这样变通倒也可以,但我总觉得还有更好的办法!
^_^
 
那你可以重载create, 把这个设置放到create里去, 你的create(nil), 是一个空指针, 不如传进去一个Tform进去, 就省事了.
 
To
0桁骀:
虽然可以,但觉得不够严谨!
 
procedure TcyButton.Click();
var
p: Tcontrol;
begin
Showmessage('a');
p := Parent;
while assigned(p.parent) do p := p.parent;
if p is TForm then (p as TForm).close;
end;
 
改造了一下

if Assigned(Parent) then
WinCon := Parent;

while (not(WinCon is TForm)) and (Assigned(WinCon.parent)) do
WinCon := WinCon.Parent;

TForm(WinCon).close;

嘿嘿
 
用控件Parent找到最终的窗体,然后把它关了,
你的控件是不可以没有parent的。
下面代码你不用改也应该可以用。
var
wCrl:TWinControl;
begin
if Sender is Tbutton then
begin
wcrl:=(Sender as Tbutton).Parent ;
while Assigned(wcrl)do
begin
if wcrl is Tform then
begin
(wcrl as Tform).Close;
exit;
end else
begin
wcrl:= wcrl.Parent;
end;
end;
end;
end;
 

Similar threads

I
回复
0
查看
709
import
I
I
回复
0
查看
566
import
I
I
回复
0
查看
686
import
I
I
回复
0
查看
763
import
I
后退
顶部