How to create a OnCellDblClick for Delphi's TDBGrid

I'm a newbie when it comes to creating new events to components... and I noticed that TDBGrid has a OnCellClick, but sadly it doesnt have an OnCellDblClick event and it bothers me a lot the fact that if I double click anywhere in my grid the selected row will be used...

So, I wanted to create the OnCellDblClick event, but as I've said earlier, I don't know how to do it.

I noticed that a lot of people want this same event, but I can't find the answerd to it.

Is it possible to know if the double click was on a record, if it was, then do all the stuff I want with it?

 

Maybe you can use the DblClick together with the MouseDown event of the DBGrid
But this will only work if the Options.dgRowSelect is false

for example

复制代码
type
  TForm1 = class(TForm)
  private
    FEmptySpace : boolean;
 ...

procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  // in my test I only get here when clicked on empty part of the dbgrid
  FEmptySpace := TRUE;
end;

procedure TForm1.DBGrid1DblClick(Sender: TObject);
begin
  if FEmptySpace then
    showmessage('clicked in the middle of nowhere...')
  else
    showmessage(DBGrid1.Fields[DBGrid1.SelectedField.Index].AsString);

  FEmptySpace := FALSE;
end;
复制代码

I have not tested this in all situations

In my test

  • Options.Editing := false
  • Options.dgRowSelect := false
  • Readonly := true

posted on   癫狂编程  阅读(207)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2019-02-26 delphi假死线程堵塞解决办法
2019-02-26 delphi CheckListBox 添加滚动条
2019-02-26 delphi Timage 加上滚动条方法
2019-02-26 Delphi: TMemo垂直滚动条自动显示
2019-02-26 如何设置Listbox的行间距?
2019-02-26 delphi 如何让ScrollBox的内容与滚动条一起实时滚动
2019-02-26 界面控件 - 滚动条ScrollBar(对滚动条消息和鼠标消息结合讲的不错)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
好的代码像粥一样,都是用时间熬出来的
点击右上角即可分享
微信分享提示