Recent

Author Topic: How to use ActiveControl, LastActiveControl and DefocusConctrol?  (Read 298 times)

flaza

  • Jr. Member
  • **
  • Posts: 53
How to use ActiveControl, LastActiveControl and DefocusConctrol?
« on: November 28, 2022, 04:08:20 pm »
Hi,

I'm using Lazarus 2.2.4 on Win11.
I have a form with a tstringgrid to show statusmessages of certain actions. The grid should not receive focus. For using the Tab key that is easy, set property TabStop = False.
But that is no solution for a mouseclick. Of course, I can set the property Enabled = False but that changes the colorsettings.
So I tried a OnEnter method to check the LastActiveControl and giving back the focus. But in all my tests, ActiveControl and LastActiveControl are always the stringgrid.
Thus, I tried the DefocusControl method. That works, the stringgrid doesn't noticiabbly receive the focus. But the focus always goes back to the first control in the form and not to the control that had the focus before the stringgrid.
Therefor I have a few questions:
1. Can anyone explain why ActiveControl and LastActiveControl are always the same?
2. Is the method OnEnter the right place to use DefocusControl?
3. Is there anything else I can try to prevent an enabled control to receive the focus by mouseclick?
4. How can I set the colors of an enabeld control as if it is not enabled?

Thx

KodeZwerg

  • Hero Member
  • *****
  • Posts: 974
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to use ActiveControl, LastActiveControl and DefocusConctrol?
« Reply #1 on: November 28, 2022, 04:55:45 pm »
If you want to have a "ReadOnly" kind of control, Enabled is a good choice but the control looks not good, what I do when I need such is a little trick.
Add a panel, client align on that panel your grid, enable = false that panel, voila, you have same colored grid but it can not be focused.

Was it that what you wanted to achieve?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 5077
Re: How to use ActiveControl, LastActiveControl and DefocusConctrol?
« Reply #2 on: November 28, 2022, 05:01:49 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   S:TControlStyle;
  4. begin
  5.   S := StringGrid1.ControlStyle;
  6.   inClude(S, csNoFocus);
  7.   StringGrid1.ControlStyle := S;
  8. end;                                    
  9.  

 This prevents it from getting focused if you click on it.
 THat is just test code there.
 I tried to also remove the flag for mouse input but that seems to get ignored, maybe there is a bug in the control for that.
 
 But at least with that you can move the grid around using the mouse without it pulling focus away from the other controls.
The only true wisdom is knowing you know nothing

KodeZwerg

  • Hero Member
  • *****
  • Posts: 974
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to use ActiveControl, LastActiveControl and DefocusConctrol?
« Reply #3 on: November 28, 2022, 05:45:41 pm »
If you want to have a "ReadOnly" kind of control, Enabled is a good choice but the control looks not good, what I do when I need such is a little trick.
Add a panel, client align on that panel your grid, enable = false that panel, voila, you have same colored grid but it can not be focused.

Was it that what you wanted to achieve?
I am sorry, I just tested it but Lazarus/FPC/LCL works different than the Vcl for Delphi does works.
Panel enable True/False will sadly affect all components within that panel.
Is that a bug or on purpose?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 974
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to use ActiveControl, LastActiveControl and DefocusConctrol?
« Reply #4 on: November 28, 2022, 06:05:59 pm »
After a little testing, at least on Windows I can do my panel trick like this:
Code: Pascal  [Select][+][-]
  1. EnableWindow(PanelName.Handle, True/False);
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

flaza

  • Jr. Member
  • **
  • Posts: 53
Re: How to use ActiveControl, LastActiveControl and DefocusConctrol?
« Reply #5 on: December 01, 2022, 10:32:01 am »
Thx KodeZwerg, using a disabled TPanel does the trick. But the solution of jamie is even better because then the focused element keeps that focus.
Thank you all for your input.

 

TinyPortal © 2005-2018