为什么按钮会不见了?(50分)

P

pnljh

Unregistered / Unconfirmed
GUEST, unregistred user!
想做一个这样的控件,在一个DBEdit的右边加入一个按钮(我知道有现成的,但想自己做)
为何在WMSize中加入设置大小的代码,按钮就不见了(创建时也不见)?不加就行,但不加的话
在dBEdit1调整大小时,按钮不能跟着改变大小,怎么回事?
主要代码如下:
其中,
fEditButton:TButton;
TDBEdit1 = class(TDBEdit)
有如下代码:
constructor TDBEdit1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if Assigned(fEditButton) then Exit;
fEditButton := TButton.Create(self);
fEditButton.Parent := Self;
fEditButton.Caption := '...';
setEditButtonPos(Width,height);
end;

procedure TDBEdit1.WMSize(var Message: TWMSize);
begin
setEditButtonPos(Width,height);//删除此句,则按钮出现了。
end;

procedure TDBEdit1.setEditButtonPos(w:Integer;h:integer);
begin
fEditButton.SetBounds(Left + W - 29,0,25,h-2)
end;
 
要实现这种功能,还不如做一个 frames 呢。

procedure TDBEdit1.WMSize(var Message: TWMSize);
begin
setEditButtonPos(Width,height); //这句有可能得到窗口的Width,height
end;
 
为什么要减29呀,另外,top应该和dbedit1的top相同,不能为0吧。
 
减29,是算好了按钮的宽度为25。
另,top必须为0,因为button的parent是DBedit。
tinytao说的是不可能的因为我说过删除那一句就没事,在创建时一样执行了那代码。
 
你继承TdbEdit,整个控件的大小即为dbedit的大小,你设在edit的外面,当然就看不到了。
你可以从panel继承,在上面放两个控件dbedit,button.
 
多人接受答案了。
 
顶部