Link to home
Start Free TrialLog in
Avatar of Alpha_AI
Alpha_AI

asked on

Get access to the parent control and modify it when you have access to the child (Form1.FindComponent(TWinControl(Sender).Name) as TEdit)

I have this editbox which is on a panel
this code gets triggered every time I hover over a editbox.

(Form1.FindComponent(TWinControl(Sender).Name) as TEdit)

I am able to get access to the control
but am unable to get access to the parent panel and modify its color.

I thought it might be something like this:

         ((FOrm1.FindComponent(Form1.FindComponent(previous) as TEdit).Parent.Name) as TPanel).Color := $00EDE8D9;

but even that doesnt work.

ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands 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
Avatar of Alpha_AI
Alpha_AI

ASKER

Thanks Merijnb
Lets say I want to get access to another component which is on the panel as well

Lets say I trigger
(Form1.FindComponent(TWinControl(Sender).Name) as TEdit)  every time i hover over a TEdit object
and I now have access to modify properties of the parent control

how can i get access to say another control (TSpeedButton) if it is on the same parent control?

So i know how to get access to the parent but how do i get access to another control on the parent control?

Ben
Thanks I figured out the answer

                TEdit(((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).Controls[i]).Color := $00D0FEFE;

its a long one.

Cheers

Ben
FindComponent is a very expensive call and you hardly never need it.
Why do you use it here, can you show the whole code you have in this event ("every time i hover over a TEdit object")?
     if AnsiContainsText(TWinControl(Sender).Name,'SpeedButton') then
      begin
          TPanel((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).Color := $00D0FEFE;
            for i:=0 to TPanel((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).ControlCount-1 do
            begin
              if TEdit((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).Controls[i] is TEdit then
              begin
                TEdit(((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).Controls[i]).Color := $00D0FEFE;
              end;
            end;

 //         (Form1.FindComponent(TWinControl(Sender).Name) as TEdit).Color := $00D0FEFE;
      end;

eg. If I hover over the SpeedButton then I want to colour my parent panel colour to another colour
and I also want to get access to the edit control that sits on that Panel and also modify the panel's colour as well.

I use this hover file that I found somewhere on the net to give me the control once i hover over it.
if you want me to paste it I can.

Funny thing is also that when i put the application I built onto a windows xp machine the hover function doesnt work
and the buttons dont appear.

It works fine on my vista. Weird. Im wondering whether the dependency scanner left something out when i built a exe setup file.

Ben


Oops thats wrong its supposed to read


eg. If I hover over the SpeedButton then I want to colour my parent panel colour to another colour
and I also want to get access to the edit control that sits on that Panel and also modify the edit's colour as well.

Ben
Here is the hook file, i found it somewhere on experts-exchange I believe. Thanks to whoever built the code.

unit uHoverHook;

interface

uses
  Classes, Controls;

type
  THookedItem = class
  public
    Control: TControl;
    OldMouseMove: TMouseMoveEvent;
  end;

  THoverHook = class
  private
    FOnMouseMove: TMouseMoveEvent;
    FHookList: TStringList;
    FHookStarted: Boolean;

    function GetHookedItem(AControl: TControl): Integer;
    function GetHookedItemAt(AIndex: Integer): THookedItem;
    function GetHookCount: Integer;
  protected
    property HookedItem[AIndex: Integer]: THookedItem
      read GetHookedItemAt;

    procedure NewMouseMove(Sender: TObject;
      Shift: TShiftState; X, Y: Integer);
  public
    constructor Create; virtual;
    destructor Destroy; override;

    function AddToHook(AObject: TControl): Boolean; virtual;
    function RemoveFromHook(AObject: TControl): Boolean; virtual;

    procedure StartHook; virtual;
    procedure StopHook; virtual;
  published
    property OnMouseMove: TMouseMoveEvent
      read FOnMouseMove write FOnMouseMove;
    property HookCount: Integer
      read GetHookCount;
  end;

implementation

uses
  SysUtils;

type
  TControlHack = class(TControl);

{ THoverHook }

function THoverHook.AddToHook(AObject: TControl): Boolean;
var
  i: Integer;
  NewHookedItem: THookedItem;
begin
  Result := False;
  i := GetHookedItem(AObject);
  if i > -1 then
  // already exists
    Exit;

  NewHookedItem := THookedItem.Create;
  NewHookedItem.Control := AObject;
  NewHookedItem.OldMouseMove := TControlHack(AObject).OnMouseMove;
  TControlHack(AObject).OnMouseMove := NewMouseMove;
  FHookList.AddObject(AObject.Name, NewHookedItem);
  Result := True;
end;

constructor THoverHook.Create;
begin
  inherited;
  FHookList := TStringList.Create;
  FHookStarted := False;
end;

destructor THoverHook.Destroy;
var
  i: Integer;
begin
  for i := FHookList.Count - 1 downto 0 do
  begin
    THookedItem(FHookList.Objects[i]).Free;
    FHookList.Delete(i);
  end;
  FHookList.Free;
  inherited;
end;

function THoverHook.GetHookCount: Integer;
begin
  Result := FHookList.Count;
end;

function THoverHook.GetHookedItem(AControl: TControl): Integer;
var
  i: Integer;
begin
  Result := -1;
  for i := 0 to FHookList.Count - 1 do
    if HookedItem[i].Control = AControl then
    begin
      Result := i;
      Break;
    end;
end;

function THoverHook.GetHookedItemAt(AIndex: Integer): THookedItem;
begin
  Result := THookedItem(FHookList.Objects[AIndex]);
end;

procedure THoverHook.NewMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  i: Integer;
  HookedItem: THookedItem;
begin
  if not Assigned(Sender) then
    Exit;
  if not (Sender is TControl) then
    Exit;

  i := GetHookedItem(Sender as TControl);
  if i = -1 then
    Exit;
  HookedItem := GetHookedItemAt(i);

  if FHookStarted then
    if Assigned(FOnMouseMove) then
      FOnMouseMove(Sender, Shift, X, Y);

  if Assigned(HookedItem.OldMouseMove) then
    HookedItem.OldMouseMove(Sender, Shift, X, Y);
end;

function THoverHook.RemoveFromHook(AObject: TControl): Boolean;
var
  i: Integer;
begin
  i := GetHookedItem(AObject);
  if i = -1 then
    raise Exception.Create('Unable to find control to unhook!');
  THookedItem(FHookList.Objects[i]).Free;
  FHookList.Delete(i);
end;

procedure THoverHook.StartHook;
begin
  FHookStarted := True;
end;

procedure THoverHook.StopHook;
begin
  FHookStarted := False;
end;

end.

and then on a button click or whatever i can put this

    AddToHook(SpeedButton1); // enter in the control you want to hook when you hover over it
    StartHook;

and then when Im on the speedbutton it will respond back at this event

procedure TForm1.HoverMouseOver(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
// do your stuff here
end;






     if AnsiContainsText(TWinControl(Sender).Name,'SpeedButton') then
      begin
          TPanel((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).Color := $00D0FEFE;
            for i:=0 to TPanel((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).ControlCount-1 do
            begin
              if TEdit((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).Controls[i] is TEdit then
              begin
                TEdit(((Form1.FindComponent(TWinControl(Sender).Name) as TSpeedButton).Parent).Controls[i]).Color := $00D0FEFE;
              end;
            end;

 //         (Form1.FindComponent(TWinControl(Sender).Name) as TEdit).Color := $00D0FEFE;
      end;


The reason for this is that I have more than 40 panels and 40 editboxes.
1 editbox, 1 label, 1 Button, 1 Image per Panel.

How expensive is the find component routine, i find that it goes fast enough for me.

Do you know why it wont work on my Windows XP computer?

Ben
Do you know why the hover code will not work on my application installed on windows xp?

Ben