如何在 OnMouseMove 事件期间确定 DbGrid 单元格的值

发布于 2024-09-13 03:16:29 字数 94 浏览 12 评论 0原文

我有 OnMouseMove 事件,在此期间我想找到某个单元格的值(不一定是鼠标下的单元格)。基本上问题是: 如何使用 x 和 y 坐标访问单元格数据而不选择它、改变焦点等?

I have OnMouseMove event, during which I want to find a value of certain cell (not neccesarily the one under the mouse). Basically the question is:
How to access cell data using its x and y coordinates without selecting it, changing focus etc?

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

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

发布评论

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

评论(1

恋竹姑娘 2024-09-20 03:16:29

Tofig,您可以使用 MouseCoord 过程来获取当前行和列,但要显示 pos [Col,Row] 的值,您必须设置 DataLink.ActiveRecord 属性到 Row 值并创建一个新的类后代来访问受保护的属性。

检查这个代码

type
 THackGrid = class(TCustomDBGrid); //Create a new class to access the protected properties


procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
    Cell             : TGridCoord;
    Row,Col          : integer;
    OrigActiveRecord : integer;
begin
    inherited;
    Cell:=DBGrid1.MouseCoord(X,Y);
    Col:= Cell.X;
    Row:= Cell.Y;

  if dgTitles in DBGrid1.Options    then  Dec(Row); //if the titles are shown then adjust Row index (-1);
  if dgIndicator in DBGrid1.Options then  Dec(Col); //if the indicator is shown then adjust the Column index (-1);

    if THackGrid(DBGrid1).DataLink.Active and (Row>=0) and (Col>=0)  then
    begin
       OrigActiveRecord:=THackGrid(DBGrid1).DataLink.ActiveRecord; //save the original index
      try
       THackGrid(DBGrid1).DataLink.ActiveRecord:= Row;
       Label1.Caption:=DBGrid1.Columns[Col].Field.AsString; //show the current value in a tlabel
      finally
        THackGrid(DBGrid1).DataLink.ActiveRecord:= OrigActiveRecord; //restore the index
      end;
    end;
end;

Tofig, you can use the MouseCoord procedure to get the current row and col, but to show the value of the pos [Col,Row] you must set the DataLink.ActiveRecord property to the Row value and create a new class descendent to access the protected property.

check this code

type
 THackGrid = class(TCustomDBGrid); //Create a new class to access the protected properties


procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
    Cell             : TGridCoord;
    Row,Col          : integer;
    OrigActiveRecord : integer;
begin
    inherited;
    Cell:=DBGrid1.MouseCoord(X,Y);
    Col:= Cell.X;
    Row:= Cell.Y;

  if dgTitles in DBGrid1.Options    then  Dec(Row); //if the titles are shown then adjust Row index (-1);
  if dgIndicator in DBGrid1.Options then  Dec(Col); //if the indicator is shown then adjust the Column index (-1);

    if THackGrid(DBGrid1).DataLink.Active and (Row>=0) and (Col>=0)  then
    begin
       OrigActiveRecord:=THackGrid(DBGrid1).DataLink.ActiveRecord; //save the original index
      try
       THackGrid(DBGrid1).DataLink.ActiveRecord:= Row;
       Label1.Caption:=DBGrid1.Columns[Col].Field.AsString; //show the current value in a tlabel
      finally
        THackGrid(DBGrid1).DataLink.ActiveRecord:= OrigActiveRecord; //restore the index
      end;
    end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文