Recent

Author Topic: Component with TGraphicControl and TEdit together  (Read 568 times)

domasz

  • Hero Member
  • *****
  • Posts: 536
Component with TGraphicControl and TEdit together
« on: March 27, 2023, 10:21:33 pm »
I want a TGraphicControl inside my TEdit and to automatically adapt it's position and size then TEdit's size/position changes.
With my current approach I get "TControl.ChangeBounds loop detected".
Can I do it differently? Change position in a different event?



Code: Pascal  [Select][+][-]
  1. uses
  2.   Windows, Graphics, Controls, Classes, SysUtils, StdCtrls, ExtCtrls;
  3.  
  4. type
  5.   TEdit = class(StdCtrls.TEdit)
  6.   private
  7.     Bgg: TGraphicControl;
  8.     FLeft, FTop, FWidth, FHeight: Integer;
  9.   protected
  10.       procedure Resize; override;
  11.   public
  12.     constructor Create(AOwner: TComponent); override;
  13.     destructor Destroy; override;
  14.   end;
  15.  
  16.  
  17. implementation
  18.  
  19.  
  20. procedure TEdit.Resize;
  21. begin
  22.   inherited Resize;
  23.  
  24.   Bgg.SetBounds(FLeft-2, FTop-2, FWidth+4, FHeight+4);
  25.  
  26.   Bgg.Canvas.Brush.Color := clRed;
  27.   Bgg.Canvas.Rectangle(0,0,FWidth,FHeight);
  28. end;
  29.  
  30. constructor TEdit.Create(AOwner: TComponent);
  31. begin
  32.   inherited Create(AOwner);
  33.  
  34.   Bgg := TGraphicControl.Create(Self);
  35.   Bgg.Parent := Self;
  36. end;
  37.  
  38. destructor TEdit.Destroy;
  39. begin
  40.   Bgg.Free;
  41.   inherited Destroy;
  42. end;

jamie

  • Hero Member
  • *****
  • Posts: 6576
Re: Component with TGraphicControl and TEdit together
« Reply #1 on: March 27, 2023, 10:43:41 pm »
Set the parent of your control to the parent of the EDIT control?

Currently you have it set to the Edit as the parent and your boundaries are incorrect for that.

Looks like you are trying to draw a border around the edit control ?

if a border is all you want then you maybe better off overriding the Paint cycle of the EDIT control and paint on the EDIT's parent.

The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Component with TGraphicControl and TEdit together
« Reply #2 on: March 27, 2023, 10:49:06 pm »
I had the same problems when I was developing ECControls and I forgot how exactly I resolved it.  O:-)
I guess I used DoOnChangeBounds method. Try to move resizing code there, if it won't help we can investigate further.
Code: Pascal  [Select][+][-]
  1.  TEdit = class(StdCtrls.TEdit)
  2.   ...
  3.   procedure DoOnChangeBounds; override;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

domasz

  • Hero Member
  • *****
  • Posts: 536
Re: Component with TGraphicControl and TEdit together
« Reply #3 on: March 27, 2023, 11:04:53 pm »
I guess I used DoOnChangeBounds method. Try to move resizing code there, if it won't help we can investigate further.
Thank you but @jamie was right- I set a wrong Parent.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Component with TGraphicControl and TEdit together
« Reply #4 on: March 27, 2023, 11:09:25 pm »
Has that answered your question?
If not, what do you want your composite control to look like?
Simply a bordered TEdit, or something else such as an edit with text or shapes overlaying it at some position?

domasz

  • Hero Member
  • *****
  • Posts: 536
Re: Component with TGraphicControl and TEdit together
« Reply #5 on: March 27, 2023, 11:10:07 pm »
Set the parent of your control to the parent of the EDIT control?

Currently you have it set to the Edit as the parent and your boundaries are incorrect for that.

Looks like you are trying to draw a border around the edit control ?

if a border is all you want then you maybe better off overriding the Paint cycle of the EDIT control and paint on the EDIT's parent.

Thank you, Parent was the problem.
For now I just play with border but I want to draw more advanced things. And Paint method is quite hard to support properly for all use cases (focused, selected...).

 

TinyPortal © 2005-2018