动态控件响应事件

newpeng 2013-07-17 04:55:29
请教一个简单的问题:动态产生的按钮控件,parent不是窗口,只是窗口里面的表格,怎么响应事件?

btns[i]:=TButton.Create(nil);
btns[i].Parent:=tb;
btns[i].OnClick:=MyButtonClick;


procedure TForm1.MyButtonClick(Sender: TObject);
begin
ShowMessage('abc');
end;


...全文
132 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
newpeng 2013-07-18
  • 打赏
  • 举报
回复
引用 7 楼 s11ss 的回复:
TStringGrid继承关系: TCustomGrid在响应WM_Command时未调用inherited(原因未知),所以不能处理其子控件按钮的点击事件。 解决办法是让TStringGrid自行处理WM_COMMAND。
type
  TStringGrid=class(Grids.TStringGrid)
    procedure WMCommand(var Message: TWMCommand); message WM_COMMAND;
  end;

  TForm1 = class(TForm)
    sg: TStringGrid;//是我们重新定义过的TStringGrid

...

procedure TStringGrid.WMCommand(var Message: TWMCommand);
begin
  inherited;//保留TCustomGrid的处理代码
  with Message, TMessage(Message) do//将消息传给子控件
    Message.Result := SendMessage(Ctl, Msg + CN_BASE, WParam, LParam);
end;
可以了,测试成功。非常感谢。同时也感谢楼上所有的网友。
newpeng 2013-07-18
  • 打赏
  • 举报
回复
引用 6 楼 jankercsdn 的回复:
TStringGrid不是容器类,所以不能是别的控件的Parent,就把Parent:=self,这也不影响你的按钮使用。 如果要把Button和Grid的行对应起来,可以在生成Button的时候,把行号记录在Button的Tag里。 更简单的办法是用第三方的StringGrid,比如Raize的,TMS的StringGrid都支持Cell里自动显示Button
如果让Parent:=self;会有一个大问题,就是显示,比如我界面上有tpagecontrol时,切换到别的tabsheet也会显示按钮,每次切换去隐藏很不方便的。
引用 7 楼 s11ss 的回复:
TStringGrid继承关系: TCustomGrid在响应WM_Command时未调用inherited(原因未知),所以不能处理其子控件按钮的点击事件。 解决办法是让TStringGrid自行处理WM_COMMAND。
type
  TStringGrid=class(Grids.TStringGrid)
    procedure WMCommand(var Message: TWMCommand); message WM_COMMAND;
  end;

  TForm1 = class(TForm)
    sg: TStringGrid;//是我们重新定义过的TStringGrid

...

procedure TStringGrid.WMCommand(var Message: TWMCommand);
begin
  inherited;//保留TCustomGrid的处理代码
  with Message, TMessage(Message) do//将消息传给子控件
    Message.Result := SendMessage(Ctl, Msg + CN_BASE, WParam, LParam);
end;
试试你提供的这种方法,看行不行。
s11ss 2013-07-18
  • 打赏
  • 举报
回复
TStringGrid继承关系:


TCustomGrid在响应WM_Command时未调用inherited(原因未知),所以不能处理其子控件按钮的点击事件。

解决办法是让TStringGrid自行处理WM_COMMAND。
type
TStringGrid=class(Grids.TStringGrid)
procedure WMCommand(var Message: TWMCommand); message WM_COMMAND;
end;

TForm1 = class(TForm)
sg: TStringGrid;//是我们重新定义过的TStringGrid

...

procedure TStringGrid.WMCommand(var Message: TWMCommand);
begin
inherited;//保留TCustomGrid的处理代码
with Message, TMessage(Message) do//将消息传给子控件
Message.Result := SendMessage(Ctl, Msg + CN_BASE, WParam, LParam);
end;

看那山瞧那水 2013-07-18
  • 打赏
  • 举报
回复
TStringGrid不是容器类,所以不能是别的控件的Parent,就把Parent:=self,这也不影响你的按钮使用。 如果要把Button和Grid的行对应起来,可以在生成Button的时候,把行号记录在Button的Tag里。 更简单的办法是用第三方的StringGrid,比如Raize的,TMS的StringGrid都支持Cell里自动显示Button
newpeng 2013-07-17
  • 打赏
  • 举报
回复
tb是我的表格[TStringGrid]的名称。
s11ss 2013-07-17
  • 打赏
  • 举报
回复
tb是 啥?
newpeng 2013-07-17
  • 打赏
  • 举报
回复
不行,如果 改为 btns[i].Parent:=self;,就没问题,可以正常响应。
看那山瞧那水 2013-07-17
  • 打赏
  • 举报
回复
btns[i]:=TButton.Create(self); 一般子控件要有Owner,这里是窗体
haitao 2013-07-17
  • 打赏
  • 举报
回复
btns[i].OnClick:=MyButtonClick;就是了,与parent没什么关系 parent只是决定显示在什么位置

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧