We help IT Professionals succeed at work.

Obtain control of the other object (on-fly)

FredDzjuba
FredDzjuba asked
on
181 Views
Last Modified: 2010-04-05
Hello, all!

I have quite a tricky question. I have rated it as very difficult so go ahead. (I couldn't find the solution to it, spent 4 hours trying to solve it)

say i have created a number of labels on the form by:

var
  Labels:array of TLabel;
  i:Integer;
begin
   for i:=0 to length(Labels)-1 do Labels:=Create.TLabel(form1);
...bla bla bla

I want to allow user to move them around by clicking on the object and dragging to a another possition. No problem with that. Using DragOver method and allowing the object to be dragable (sorry about the word).

When I process dragging in DragOver i know the source that i am dragging, however I can't obtain the control of that object. For exapmple idealy i would like to do something like following:
---------
procedure TForm1.GroupBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
if Sender=GroupBox1 then
begin

       Source.Left:=x;        
       Source.Top:=y;

Accept := True;
--------------
I have tried to bind the Source with newly created component by GetInterface(), but it all looks ugly, as I don't want to care about the source type at all. I need to be able to change properties of the dragged component without knowing the name,type or any other refferences.

Please help, bosses kicking my a*se already for wasting time creating beauty instead of making it working.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Just a suggestion:
With TWinControl descendent you can use this method:
for example a TButton...
procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
const
  SC_DragMove = $F012;
begin
  Begin
   ReleaseCapture;
   (Sender as TControl).perform(WM_SysCommand, SC_DragMove, 0);
  End;
end;

Unfortunally this can't be done with TLabel (TGraphicCotrol), but can be done with TStaticText (TwinControl)....

SO you can create StaticTexts instead of Labels and link use this method to drag them automatically around a form...
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
nope

with Sender as Sender.ClassType do
begin
   left:=0;
   top:=0;
end;

don't work, because of some reason it returns parent class, which is form itself. Why? Only **** knows!

by the way if i do

with Sender as TLabel do
begin
   left:=0;
   top:=0;
end;

it works perfectly

however when its something like

procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);

var
   WhoIam:TClass;
begin
WhoIam:=TLabel;

with Sender as WhoIam do
begin
   left:=0;
   top:=0;
end;

end;

it doesn't work, gets control of the parent again, funny stuff isn't it, i have tried nearly everything, and still the same sh*t happens :(

Author

Commented:
Appologies, gmayo!!!!
TControl worked!

You are life saver,mate !!!! bloody hell, all I needed is TControl, simple and straight forward! I have been inventing a wheel all this time !!!
:)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.