tscrollpanel = class(TPanel)
private
{ Private declarations}
fbuttonitem:tstringlist; //button的caption
fbutton:array of tbutton; //button数组
procedure cliclbutn(sender:tobject); //单击button
protected
{ Protected declarations }
public
{ Public declarations }
constructor create(Aowner:Tcomponent);override;
destructor destroy;override;
procedure setbutton(butitems:tstringlist);
published
{Published declarations }
property items:tstringlist read fbuttonitem write setbutton;
end;目的:在一个panel上产生一系列的button,button由items产生。
问题:
1。希望在设计期,单击里面的button能够产生一些操作,实际却没能成功
2。在设计期改变items的值可以产生一系列的button,但为什么运行的时候,设计期输入的items却无效,显示什么也没有另附几个重要的程序段:
procedure tscrollpanel.setbutton(butitems:tstringlist);
var x:integer;
len:integer;
begin
for x:=butitems.Count-1 downto 0 do
if trim(butitems.Strings[x])='' then
butitems.Delete(x);
len:=length(fbutton);
for x:=len-1 downto 0 do
if assigned(fbutton[x]) then Freeandnil(fbutton[x]);
fbuttonitem.clear;
fbuttonitem.assign(butitems);
setlength(fbutton,fbuttonitem.count);
for x:=0 to fbuttonitem.count-1 do
begin
fbutton[x]:=tbutton.Create(self);
fbutton[x].Parent:=self;
fbutton[x].left:=0;
fbutton[x].Height:=20;
fbutton[x].top:=(x-1)*fbutton[x].Height;
fbutton[x].Width:=width;
fbutton[x].Tag:=x;
fbutton[x].Visible:=true;
fbutton[x].enabled:=true;
fbutton[x].Caption:=fbuttonitem.Strings[x];
fbutton[x].OnClick:=cliclbutn;
end;
end;constructor Tscrollpanel.create(Aowner:Tcomponent);
begin
inherited create(aowner);
fbuttonitem:=tstringlist.Create;
fcurrentbtn:=0;
end;
private
{ Private declarations}
fbuttonitem:tstringlist; //button的caption
fbutton:array of tbutton; //button数组
procedure cliclbutn(sender:tobject); //单击button
protected
{ Protected declarations }
public
{ Public declarations }
constructor create(Aowner:Tcomponent);override;
destructor destroy;override;
procedure setbutton(butitems:tstringlist);
published
{Published declarations }
property items:tstringlist read fbuttonitem write setbutton;
end;目的:在一个panel上产生一系列的button,button由items产生。
问题:
1。希望在设计期,单击里面的button能够产生一些操作,实际却没能成功
2。在设计期改变items的值可以产生一系列的button,但为什么运行的时候,设计期输入的items却无效,显示什么也没有另附几个重要的程序段:
procedure tscrollpanel.setbutton(butitems:tstringlist);
var x:integer;
len:integer;
begin
for x:=butitems.Count-1 downto 0 do
if trim(butitems.Strings[x])='' then
butitems.Delete(x);
len:=length(fbutton);
for x:=len-1 downto 0 do
if assigned(fbutton[x]) then Freeandnil(fbutton[x]);
fbuttonitem.clear;
fbuttonitem.assign(butitems);
setlength(fbutton,fbuttonitem.count);
for x:=0 to fbuttonitem.count-1 do
begin
fbutton[x]:=tbutton.Create(self);
fbutton[x].Parent:=self;
fbutton[x].left:=0;
fbutton[x].Height:=20;
fbutton[x].top:=(x-1)*fbutton[x].Height;
fbutton[x].Width:=width;
fbutton[x].Tag:=x;
fbutton[x].Visible:=true;
fbutton[x].enabled:=true;
fbutton[x].Caption:=fbuttonitem.Strings[x];
fbutton[x].OnClick:=cliclbutn;
end;
end;constructor Tscrollpanel.create(Aowner:Tcomponent);
begin
inherited create(aowner);
fbuttonitem:=tstringlist.Create;
fcurrentbtn:=0;
end;
改成
property items:tstringlist read fbuttonitem write FButtonitem;然后把你的setbutton的代码写到Fbuttonitem的onchange事件中试试
这个肯定要自己关联事件的
赋给button的onclick事件?这个应该在button初始化的时候给他赋值;这个事情很关键;
我看你写在setbutton里面了,但是setButton什么时候执行了,你真的知道吗?
2、fbutton[x].Parent:=self;//这一句应该改一下,self改成所在的panel试试看!
1。希望在设计期,单击里面的button能够产生一些操作,实际却没能成功
一般这些在运行期才能看出的
heluqing(鉴之小河) 1、属性的set方法在给属性赋值时执行,但对对象类型有点特殊。
items := Tmplist; 这个执行set方法
items.Assign(TmpList); 这个不会执行set方法
所以要在onchange事件中写代码,set中也要处理。
同样的还有TFont等常用对象。
2、self是指调用这个过程的对象,这里指的就是panel,而不是平时编程中的form。
您的提醒使我解决了第二个问题 谁来帮我解决第一个问题
以下的源码不能达到兄台的要求,但有一定借鉴价值。①新建项目:
unit untButtonsPanel;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ButtonsPanel, ExtCtrls;
//上面的ButtonsPanel单元文件源码列在后面type
TMainForm = class(TForm)
btnGenPanel: TButton;
edtCaption: TEdit;
lbCaption: TListBox;
btnGenButton: TButton;
procedure btnGenPanelClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnGenButtonClick(Sender: TObject);
private
{ Private declarations }
TestPanel : TScrollPanel;
public
{ Public declarations }
end;var
MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.btnGenPanelClick(Sender: TObject);
begin
if not Assigned(FindComponent('ButtonsPanel1')) then
begin
TestPanel := ButtonsPanel.TScrollPanel.Create(self);
TestPanel.Name := 'ButtonsPanel1';
TestPanel.Caption := '';
TestPanel.Parent := self;
TestPanel.SetBounds(360, 100, 200, 200);
end;
TestPanel.Items := TStringList(self.lbCaption.Items);
end;procedure TMainForm.FormCreate(Sender: TObject);
begin
self.btnGenPanel.Enabled := False;
end;procedure TMainForm.btnGenButtonClick(Sender: TObject);
begin
self.lbCaption.Items.Add(edtCaption.Text);
if self.lbCaption.Items.Count = 1 then
self.btnGenPanel.Enabled := True;
end;end.②ButtonsPanel单元文件源码
unit ButtonsPanel;interfaceuses
StdCtrls, Classes, ExtCtrls, SysUtils, Dialogs;type
TButtonArray = array of TButton; TScrollPanel = class(TPanel)
private
FButtonItem : TStringList;
FButton : TButtonArray;
procedure SetButton(Value : TStringList);
procedure ClickEvent(Sender : TObject);
public
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
property Items : TStringList read FButtonItem write SetButton;
end;implementation{ TScrollPanel }procedure TScrollPanel.ClickEvent(Sender: TObject);
begin
ShowMessage((Sender as TButton).Caption);
end;constructor TScrollPanel.Create(AOwner: TComponent);
begin
inherited;
FButtonItem := TStringList.Create;
end;destructor TScrollPanel.Destroy;
begin
FButtonItem.Free;
inherited;
end;procedure TScrollPanel.SetButton(Value: TStringList);
var
X, Len:Integer;
begin
for X := Value.Count - 1 downto 0 do
if Trim(Value.Strings[X]) = '' then
Value.Delete(X);
Len := Length(FButton);
for X := Len - 1 downto 0 do
if Assigned(FButton[X]) then FreeAndNil(FButton[X]);
FButtonItem.Clear;
FButtonItem.Assign(Value);
SetLength(FButton,FButtonItem.Count);
for X := 0 to FButtonItem.Count - 1 do
begin
FButton[X] := TButton.Create(self);
FButton[X].Name := 'Button' + IntToStr(X + 1);
FButton[X].Parent := self;
FButton[X].Left := 0;
FButton[X].Height := 20;
FButton[X].Top := X * FButton[X].Height;
FButton[X].Width := Width;
FButton[X].Tag := X;
FButton[X].Visible := True;
FButton[X].Enabled := True;
FButton[X].Caption := FButtonItem.Strings[X];
FButton[X].OnClick := ClickEvent;
end;
end;end.
运行期没问题,但现在是需要在设计期就能看到运行期的效果
可能, 將你要的功能, 放到右鍵菜單比較簡單, 類似如下:unit DJ161AEditor;interface
uses DesignEditors;
type
TDJ161AEditor = class (TComponentEditor)
function GetVerbCount: Integer; override;
function GetVerb(Index: Integer): string; override;
procedure ExecuteVerb(Index: Integer); override;
procedure Edit; override;
end;procedure Register;implementation
uses
SysUtils, Dialogs, StdCtrls, DesignIntf, DJ161A;
{ TDJ161AEditor }
{$I Dj161.res}procedure Register;
begin
RegisterComponentEditor(TDJ161A, TDJ161AEditor);
end;procedure TDJ161AEditor.Edit;
begin
Beep;
ExecuteVerb(0);
end;//procedure TDJ161AEditor.ExecuteVerb(Index: Integer);
//begin
// case Index of
// 0: ; // nothing to do
// 1: MessageDlg ('This is a telphone card component'#13 +
// 'built by aiirii'#13 +
// 'email: [email protected]', mtInformation, [mbOK], 0);
// end;
//end;
//
//function TDJ161AEditor.GetVerb(Index: Integer): string;
//begin
// case Index of
// 0: Result := ' TDJ161A (aiirii)';
// 1: Result := '&About this component...';
// end;
//end;function TDJ161AEditor.GetVerbCount: Integer;
begin
Result := 2;
end;end.但你想要的功能也是可以的, 也是Register 一個關聯, 但一時我不知道
begin
Beep;
ExecuteVerb(0);
end;就是你要的, 我一時沒注意, 這個就是雙擊控件時的調用的方法, 但你必須在裹面作些判斷,如:if Component is TButton then
begin
if Component.Parent is tscrollpanel thenend;