Link to home
Start Free TrialLog in
Avatar of CodedK
CodedKFlag for Greece

asked on

DBGrid and mouse.

I'd like the following to happen with a dbgrid:

When the mouse pointer is over a particular column then ...

take the number that lies under the mouse pointer in the current cell and store it in a string !.
Then take the number of the next column in the same line...and store it in a string

Thanks in advance. :)
Avatar of pcsentinel
pcsentinel

Add this line after type at the top of your form code

      TCGrid = class(TCustomDBGrid);


then use this in the mousemove of the grid


procedure TfrmMain.dbgDataMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
var
  lCoord:      tGridCoord;
  ls: string;
begin
    lCoord:=TCGrid(dbgData).MouseCoord(x,y);
    if (lCoord.x>0) and (lCoord.y>0 )then
    begin
          ls:=ls+TCGrid(dbgData).GetEditText(lCoord.x,lCoord.y);
          Caption:=ls;
    end;

end;

regards
Sorry I take that back I will continue to look at it
Make your own vcl derived from DBGrid.

OnMouseMove:

...
var f : TField;
    LogCol,
    LogRow    : Longint;
    OldActive : integer;
    text: string;
    Cell:TGridCoord;
begin
  Cell:=MouseCoord(X,Y);
  LogCol:= Cell.X;
  LogRow:= Cell.Y;

  if dgTitles in Options then
    Dec(LogRow);

  if dgIndicator in Options then
    Dec(LogCol);

  if (DataLink.Active) and (LogCol >= 0) and (LogRow >= 0) then
    begin
      OldActive:= DataLink.ActiveRecord;
      try

        DataLink.ActiveRecord:= LogRow;
        f:=Columns[LogCol].field;

        try
          Text:= Columns[LogCol].Field.AsString
        except
          Text:= Columns[LogCol].Field.DisplayText;
        end;

      finally
        DataLink.ActiveRecord:= OldActive ;
      end;
  end;

end;

For next colum text use:
Text:= Columns[LogCol+1].Field.AsString

 

 
   
Avatar of CodedK

ASKER

Hi.

Ginsonic there is a problem with DataLink
(Undeclared udentifier)
Tried Dbgrid1.Data... but no luck.. how should i write that ?

Thanks :)
You must create your own DBGrid to say MyDBGrid: TDBGrid;
And then use MyDBGrid instead of DBGrid1 in you project.

type
  TMyDBGrid = class(TDBGrid)
  protected
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  end;

...

procedure TMyDBGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
Avatar of CodedK

ASKER

Ok i'll check it. :)

But will this do the job for the existing dbgrid ?

By the way have you checked the "Screen Cam Component" ?

Thanks :)
Avatar of CodedK

ASKER

Ok i created MyDbgrid.

the code is this..

procedure TMyDBGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
 var f : TField;
    LogCol,LogRow: Longint;
    OldActive : integer;
    text: String;
    Cell:TGridCoord;

begin
  Cell:=MouseCoord(X,Y);
  LogCol:= Cell.X;
  LogRow:= Cell.Y;

  if dgTitles in Options then
    Dec(LogRow);

  if dgIndicator in Options then
    Dec(LogCol);

  if (DataLink.Active) and (LogCol >= 0) and (LogRow >= 0) then
    begin
      OldActive:= DataLink.ActiveRecord;
      try

        DataLink.ActiveRecord:= LogRow;
        f:=Columns[LogCol].field;

        try
          Text:= Columns[LogCol].Field.AsString
        except
          Text:= Columns[LogCol].Field.DisplayText;
        end;

      finally
        DataLink.ActiveRecord:= OldActive ;
      end;
  end;
end
Avatar of CodedK

ASKER

How to invoke the text to appear?
you can add a property into public declaration. Or a function.

......
private
  Text: string;
public
  function GetText: string;
.....

function TMyDBGrid: string;
begin
  Result:=Text;
end;

Don't forget to remove Text: string from OnMouse procedure ( don't declare twice ).

About Scam I don't think that willhelp me. I need something regarding overlaying.

Nick

And to read use in form:

 Label1.Caption:=MyDBGrid1.GetText;
Avatar of CodedK

ASKER

Ginsonic.. Sorry for not understanding :))
but this is what i use :

TForm1.DBGrid1MouseMove    //Dbgrid1 the old dbgrid
Begin
Form1.Caption:=Text1
End;

I've declared text1 as string at general variables..
But i get nothing when the mouse move on dbgrid1...

???Is it necessary to create a function ?

Thanks for ur time :)
Avatar of CodedK

ASKER

And ofcourse text1 is the string name i use in the code u gave me .
ASKER CERTIFIED SOLUTION
Avatar of ginsonic
ginsonic
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Problems ?
Avatar of CodedK

ASKER

No :)

No problems ginsonic ... Sorry for the delay and thank you for your help :)))

I'll just need one more day to check the codes ... i'm away :/

Thank you :)
Avatar of CodedK

ASKER

Ok ...

I checked it.

The code produce no errors.
But when i put the 'GetText' on the 'DBGrid1MouseMove' event :
-----------------------------------------------------------------------------------------------
procedure TForm2.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Label1.Caption:=DBGrid1.GetText;
end;
-----------------------------------------------------------------------------------------------
I get undeclared identifier for GetText !
I also tried:
  Label1.Caption:=MyDBGrid.GetText;

Did u check it?

Please help coz i cant make it work ...
:)
Don't use DBGrid. Use our new class MyDBGrid.
Take the component code and save it into a unit named myDBGrid. Install the component. Open the Form1 and replace the standard DBGrid with MyDBGrid. Can find it in Nick's category on vcl pallete.
Here work perfect.
Avatar of CodedK

ASKER

Ok.

Increased points to 500...
Please help coz its the first time i do this :)

I saved it with the name 'MyDBGrid'.
I installed it as a component - 'Into Existing Package'.
I closed everything and went back to my Project.

Now ... this is a huge project (for me it is) and i can do it from the beggining again. :/
So... OnMouse event on DBGrid1, i tried to write 'MyDBGrid. Something' but errors occured... ofcourse...
So there is a' Nicks' tab, with a DBGrid that i can drop in my form... :/ :/ But then i should right all the code from the start... :(
(2352 lines) ...

Is there any other way ?
I m trying to capture the string from the cell to invoke a hint (or a form)
to come up everytime the mouse is over a particular cell...

I searched EE and found some codes:
This one points to a 3rd party software
https://www.experts-exchange.com/questions/20636690/Hints-in-dbGrid.html?query=hint+dbgrid&clearTAFilter=true

This one is small and very promissing ... but cant make it work.
It didnt worked for the asker either.. I guess.
https://www.experts-exchange.com/questions/21048106/Hint-on-a-cell-in-a-DBGrid.html?query=hint+dbgrid&clearTAFilter=true

This one refers to a sample that was never there !
https://www.experts-exchange.com/questions/21040980/When-I-click-a-memo-in-a-Dbgrid-Can-I-dispaly-a-small-box-that-shows-the-detail-of-the-memo.html?query=hint+dbgrid&clearTAFilter=true

Anyway .. I appreciate your time.
Plz tell if there is an other way.. or even a more simple solution (than replacing .. rewriting the whole project)


Thank u very much !
You don't need rewrite the all application. Just drop the new VCL. Delete the old DBGrid. Rename MyDBGrid1 with DBGrid1. Assign all events of the old grid to the new. Is a DBGrid but with a new property. I think that you can replaced in max. 10 minutes.
I will thinking to a directly way but is a problem that if isn't inside a vcl unit DataLink can't be accessed. Don't understand way. Still digging.
Avatar of CodedK

ASKER

@I think that you can replaced in max. 10 minutes.

:)

I know... Allready did that.
But there is a problem with the language i use in the first DBGrid.
It keeps showing '@$##%#'...

I dropped our dbgrid and by pressing Shift i gave to 'MyDBGrid' all the things DBGrid1 has.. Fonts,Size,Data and all the other stuff...

Anyway :)
I'll find out whats wrong and replace anything with 'Mydbgrid' ...
I'll keep this question open, for a while, if u find anything about Hinds&Dbgrid ...
Thank u very much :)
Take a look at http://www.torry.net/quicksearchd.php?SID=b5f74814bda808b8c35b4bb803ce5fec&String=bkgdbg&Title=Yes and see ve. 3.5 for BkgDBGrid. Something like this ?
>I dropped our dbgrid and by pressing Shift i gave to 'MyDBGrid' all the things DBGrid1 has.. Fonts,Size,Data and all the other stuff...

Can you tell me more ? Idon't know this way to replace. Sound great.

>But there is a problem with the language i use in the first DBGrid.
Don't understand the problem. Can you give me more details
Avatar of CodedK

ASKER

:P

Glad to help :)

Well with shift you can select 2-3 or more objects and see their common attributes.
In the case of the 2 dbgrids all the attributes are the same.

By selecting for example on textbox and one label you have many common attributes..
Lets say Fonts.
The first object you'll select by pressing shift is the one that gives the attributes to the others..
(Hold Shift) click on a button then on a form... Then go to the 'Fonts' it shows the font of the button by pressing enter the form
borrows the button font attribute..

Thats what i did with 2 dbgrids, one by one i pressed enter etc...

:)

The link from Torry has a new dbgrid :/, i prefer to use 'MyDBGrid'.. If the case is to replace the 1st dbgrid...
I will leave this 1-2 days open ... Thanks :)
BkgDBGrid is my vcl. The solution proposed by me is part of this vcl.
I ask you to know if you need to show the text inside a hint or into label for example.
Avatar of CodedK

ASKER

Inside a form like a hint... so i can configure it ... more...
or a hint... The thing is just to capture the string from the cell in dbgrid then i can do it what ever i want... :)
I'll give you the points now since no function ... or something like that is possible...
Thanks very much for your help ...

P.s if you find something else beside other vcl's please post ...
 :)
Glad to can help you. Keep in touch!