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(TWinC ontrol(Sen der).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(Form 1.FindComp onent(prev ious) as TEdit).Parent.Name) as TPanel).Color := $00EDE8D9;
but even that doesnt work.
this code gets triggered every time I hover over a editbox.
(Form1.FindComponent(TWinC
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(Form
but even that doesnt work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Lets say I want to get access to another component which is on the panel as well
Lets say I trigger
(Form1.FindComponent(TWinC ontrol(Sen der).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
Lets say I trigger
(Form1.FindComponent(TWinC
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
ASKER
Thanks I figured out the answer
TEdit(((Form1.FindComponen t(TWinCont rol(Sender ).Name) as TSpeedButton).Parent).Cont rols[i]).C olor := $00D0FEFE;
its a long one.
Cheers
Ben
TEdit(((Form1.FindComponen
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")?
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")?
ASKER
if AnsiContainsText(TWinContr ol(Sender) .Name,'Spe edButton') then
begin
TPanel((Form1.FindComponen t(TWinCont rol(Sender ).Name) as TSpeedButton).Parent).Colo r := $00D0FEFE;
for i:=0 to TPanel((Form1.FindComponen t(TWinCont rol(Sender ).Name) as TSpeedButton).Parent).Cont rolCount-1 do
begin
if TEdit((Form1.FindComponent (TWinContr ol(Sender) .Name) as TSpeedButton).Parent).Cont rols[i] is TEdit then
begin
TEdit(((Form1.FindComponen t(TWinCont rol(Sender ).Name) as TSpeedButton).Parent).Cont rols[i]).C olor := $00D0FEFE;
end;
end;
// (Form1.FindComponent(TWinC ontrol(Sen der).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
begin
TPanel((Form1.FindComponen
for i:=0 to TPanel((Form1.FindComponen
begin
if TEdit((Form1.FindComponent
begin
TEdit(((Form1.FindComponen
end;
end;
// (Form1.FindComponent(TWinC
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
ASKER
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
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
ASKER
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(AObje ct: 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).OnMo useMove;
TControlHack(AObject).OnMo useMove := NewMouseMove;
FHookList.AddObject(AObjec t.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.Obje cts[i]).Fr ee;
FHookList.Delete(i);
end;
FHookList.Free;
inherited;
end;
function THoverHook.GetHookCount: Integer;
begin
Result := FHookList.Count;
end;
function THoverHook.GetHookedItem(A Control: 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.Obje cts[AIndex ]);
end;
procedure THoverHook.NewMouseMove(Se nder: 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.OldMou seMove) then
HookedItem.OldMouseMove(Se nder, 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.Obje cts[i]).Fr ee;
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(Send er: TObject; Shift: TShiftState; X,
Y: Integer);
begin
// do your stuff here
end;
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(AObje
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).OnMo
FHookList.AddObject(AObjec
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.Obje
FHookList.Delete(i);
end;
FHookList.Free;
inherited;
end;
function THoverHook.GetHookCount: Integer;
begin
Result := FHookList.Count;
end;
function THoverHook.GetHookedItem(A
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
begin
Result := THookedItem(FHookList.Obje
end;
procedure THoverHook.NewMouseMove(Se
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.OldMou
HookedItem.OldMouseMove(Se
end;
function THoverHook.RemoveFromHook(
var
i: Integer;
begin
i := GetHookedItem(AObject);
if i = -1 then
raise Exception.Create('Unable to find control to unhook!');
THookedItem(FHookList.Obje
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(Send
Y: Integer);
begin
// do your stuff here
end;
ASKER
if AnsiContainsText(TWinContr
begin
TPanel((Form1.FindComponen
for i:=0 to TPanel((Form1.FindComponen
begin
if TEdit((Form1.FindComponent
begin
TEdit(((Form1.FindComponen
end;
end;
// (Form1.FindComponent(TWinC
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
ASKER
Do you know why the hover code will not work on my application installed on windows xp?
Ben
Ben
ASKER