Link to home
Start Free TrialLog in
Avatar of Norm-al
Norm-alFlag for United States of America

asked on

SetFocus on grid on Form Mouse Over/Hover?

Now that most of our users are switching to multiple monitors, they are switching back and forth between my grids and their other applications. I would like the grid to be focused when the mouse hovers on the form, so when the user starts scrolling their mouse wheel, it is already focused on the grid, no need to click first.  Any ideas?

Thanks!
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Which grid are you speaking about: a StringGrid? A DBGrid?
Have you tried DBGrid1.SetFocus in OnMouseOver event?
Avatar of Norm-al

ASKER

It is a TcxDBGrid and I tried the OnMouseOver event on the grid and it causes other issues when user clicks in the cell to edit as some fields are editable in the grid. Exception about no scrollbars found. Basically I want the grid 'clicked' to gain control OnMouseOver. Thanks!
Try using Form.OnActivate and write your grid.setfocus there
This way any time your form become active the focus goes to your grid
Avatar of Norm-al

ASKER

the focus goes to the grid only AFTER I click on the form to activate it.
I don't know if I've exactly understood what you need, anyway if you want to trigger an OnMouseOver for the Grid without using itself event, you can do as follows:

drop a TApplicationEvent on your form
In its OnIdle event check if the control under the mouse is your grid and if so there you can do what you want with it (set the focus, scroll, update etc, that is you can access any published property or function of the control).

You can do this for any control, not just for your grids...

here an example on the fly

procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
var
  ctrl: TWinControl;
begin
  ctrl := FindVCLWindow(Mouse.CursorPos);
  if ctrl <> nil then
  begin
    if ctrl is TcxDBGrid then
    begin
      if not(TcxDBGrid(ctrl).Focused) then
        TcxDBGrid(ctrl).SetFocus;
    end;
  end;
end;

Open in new window

Avatar of Norm-al

ASKER

I have been able to 'set focus' to the grid using my previous OnMouseEnter event of the grid so I can scroll on mouseover BUT if I have clicked on any other window, setting focus to the grid does not work. It sets focus on the record, I can see from the highlighting bar, but the form itself is not 'focused' and the caption is 'Inactive Window' . I cannot scroll until I click on either the toolbar, caption, etc.
You want to use mouse wheel without you click/activate form first?
Avatar of Norm-al

ASKER

YES. Exactly. I want to mouseover the grid and scroll again (the focus is already on the record) but it does not work until AFTER i click on the toolbar, form, etc. somewhere on the form first.
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia 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