如何在tdbgrid.dblclick(发件人:tobject)上获取单击列?

发布于 2025-01-23 19:10:07 字数 94 浏览 0 评论 0原文

当使用TDBGrid的OnDblClick事件时,我怎么知道哪列是双击的?

对于OnCellClick,它具有TCOLUMN参数,但在Ondblick上不容易。

When using the OnDblClick event of a TDBGrid, how can i know what column was double clicked ?

This is easy with the OnCellClick as it has a TColumn parameter, but not on OnDblClick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作死小能手 2025-01-30 19:10:07

tdbgrid.ondblick tdbgrid 中,数据集位于单击记录中,可以使用 tdbgrid.selectedIndex 属性检索列。如果您对基础数据集字段感兴趣,则可以使用 tdbgrid.selectedfield 直接访问它。

During TDBGrid.OnDblClick the dataset is positioned to the clicked record and the column can be retrieved with the TDBGrid.SelectedIndex property. If you are interested in the underlying dataset field, you can directly access it with TDBGrid.SelectedField.

别再吹冷风 2025-01-30 19:10:07

ondblick事件不会为您提供有关单击的任何信息,特别是在执行单击的情况下,更不用说单击哪个网格单元格了。因此,您必须手动确定该信息。

尝试以下尝试:

  • 通过将鼠标传递给tdbgrid.screentoclient()
  • 然后,使用tdbgrid.mousecoord() tdbgrid.screentoclient()代码>确定鼠标下方的单元格的行/列索引。
  • 然后,检查单元行/列是否对应数据单元格,如果是这样,则使用tdbgrid.selectedIndex属性将属性索引到tdbgrid.columns属性。

这基本上是与tdbgrid在触发oncellClick事件时内部执行的事情,只有它对Mouseup事件进行响应,该事件提供了小鼠在网格中坐标,从而跳过上方的第一步。

例如:

type
  TDBGridAccess = class(TDBGrid)
  end;

procedure TMyForm1.DBGrid1DblClick(Sender: TObject);
var
  TitleOffset: Byte;
  Pt: TPoint;
  Cell: TGridCoord;
  Column: TColumn;
begin
  TitleOffset := Ord(dgTitles in DBGrid1.Options);
  Pt := DBGrid1.ScreenToClient(Mouse.CursorPos);
  Cell := DBGrid1.MouseCoord(Pt.X, Pt.Y);
  if (Cell.X >= TDBGridAccess(DBGrid1).IndicatorOffset) and (Cell.Y >= TitleOffset) then
  begin
    Column := DBGrid1.Columns[DBGrid1.SelectedIndex];
    // use Column as needed...
  end;
end;

update :基于 @uweraabe的评论,您应该能够单独使用tdbgrid.selectedIndex本身:

procedure TMyForm1.DBGrid1DblClick(Sender: TObject);
var
  Index: Integer;
  Column: TColumn;
begin
  Index := DBGrid1.SelectedIndex;
  if (Index <> -1) then
  begin
    Column := DBGrid1.Columns[Index];
    // use Column as needed...
  end;
end;

The OnDblClick event doesn't give you any information about the click, in particular where the click was performed, let alone which grid cell was clicked on. So, you will have to determine that information manually.

Try this:

  • Get the current mouse position within the grid, by passing Mouse.CursorPos to TDBGrid.ScreenToClient()
  • Then, use TDBGrid.MouseCoord() to determine the row/column indexes of the cell that is underneath the mouse.
  • Then, check if the cell row/column corresponds to a data cell, and if so then use the TDBGrid.SelectedIndex property to index into the TDBGrid.Columns property.

This is basically the same thing that TDBGrid does internally when firing the OnCellClick event, only it does this in response to a MouseUp event, which provides the mouse coordinates within the grid, thus skipping the 1st step above.

For example:

type
  TDBGridAccess = class(TDBGrid)
  end;

procedure TMyForm1.DBGrid1DblClick(Sender: TObject);
var
  TitleOffset: Byte;
  Pt: TPoint;
  Cell: TGridCoord;
  Column: TColumn;
begin
  TitleOffset := Ord(dgTitles in DBGrid1.Options);
  Pt := DBGrid1.ScreenToClient(Mouse.CursorPos);
  Cell := DBGrid1.MouseCoord(Pt.X, Pt.Y);
  if (Cell.X >= TDBGridAccess(DBGrid1).IndicatorOffset) and (Cell.Y >= TitleOffset) then
  begin
    Column := DBGrid1.Columns[DBGrid1.SelectedIndex];
    // use Column as needed...
  end;
end;

UPDATE: based on @UweRaabe's comments, you should be able to just use TDBGrid.SelectedIndex by itself:

procedure TMyForm1.DBGrid1DblClick(Sender: TObject);
var
  Index: Integer;
  Column: TColumn;
begin
  Index := DBGrid1.SelectedIndex;
  if (Index <> -1) then
  begin
    Column := DBGrid1.Columns[Index];
    // use Column as needed...
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文