Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Coordinates of dynamically created items

Posted on 2001-06-18
6
Medium Priority
?
189 Views
Last Modified: 2010-04-06
I wonder if anyone knows how to figure out what coordinates the controls have when they are dynamically created.
I want to create a number of buttons on the canvas and then connect them to each other with lines.
The number of buttons is dynamic and the positions of them are also dynamic and varies with screen size and so on.

Below is a light example of what i want to achieve.

procedure TForm1.Button1Click(Sender: TObject);
Begin
  Vname := Edit1.text;
  Vleft := StrToInt(Edit2.text);
  VTop := StrToInt(Edit3.text);
 
  with TspeedButton.Create(Self) do
  begin
    Parent := Self;
    Left := Vleft;
    Top := Vtop;
    font.Name := 'Arial';
    font.Size := 7;
    Height := 25;
    Width := 42;
    Caption := copy(VName,1,3) + #13 + copy(Vname,4,5);
    Name := Vname;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // draw line between the buttons.
  moveto("button with name X".left,"button with name X".top);
  canvas.lineto("button with name Y".left,"button with name Y".top);
  canvas.lineto("button with name Z".left,"button with name Z".top);
  {"And so on"}
end;


Please suggest a good way to do this.
0
Comment
Question by:samone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:TheNeil
ID: 6202139
Can't you access the Left and Top properties of the dynamically created objects?

Searching for the component you just created by name isn't really advisable but here goes:

Result := -1;
FOR iCount := 0 TO (Form1.ComponentCount - 1)
DO
  IF Form1.Components[iCount] IS TSpeedButton
  THEN
  BEGIN
    IF (Form1.Components AS TSpeedButton).Name = sSearchName
    THEN
      Result := iCount;
  END;
...

Then you simply need to access the component selected ie:

IF result <> -1
THEN
  Left := (Form1.Components[Result] AS TSpeedButton).Left;
...

The Neil =:)
0
 
LVL 5

Accepted Solution

by:
TheNeil earned 400 total points
ID: 6202158
Oops. Sorry. What you need to do is simply supply the name of the component to the sSearchName variable (type String) and it should search through all of the components on the form (Form1 in this case) looking for a speedbutton with the name required. If you want to search for other component types (e.g.TButton) then you simply need to add additional IF's to check if the component is of the correct type, and then typecast it as that type (Similarly when you get you result value back, typecast the component as the correct type)

The Neil =:)
0
 

Author Comment

by:samone
ID: 6202497
Is there an other way than searching for them to access the left and top values since you say it's not advisable ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:TheNeil
ID: 6202506
The searching approach is fine, it's just searching by name that isn't ideal. Unfortuneatly in this situation I can't think of a better solution so all I'd say is make extra sure that all of your components are named uniquely and you should have no problems

The Neil =:)
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6202515
Hi samone,

may be easy to put dynamically generated buttons into TList? Then you can easy access those buttons. Here is sample how to draw lines between buttons which is in TList.

type
  TForm1 = class(TForm)
...
  private
  public
    BtnList: TList;
    procedure Paint; override;
  end;

.....

procedure TForm1.FormCreate(Sender: TObject);
begin
  BtnList := TList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  BtnList.Free;
end;

// paint lines between buttons
procedure TForm1.Paint;
var
  B: TSpeedButton;
  I: Integer;
begin
  Inherited;
  for I := 0 to BtnList.Count-1 do
  begin
    B := BtnList[I];
    if I <> 0 then
      Canvas.LineTo(B.Left, B.Top);

    Canvas.MoveTo(B.Left + B.Width, B.Top + B.Height);
  end;
end;

// generate random button
procedure TForm1.Button1Click(Sender: TObject);
var
  B: TSpeedButton;
begin
  B := TSpeedButton.Create(Self);
  B.Parent := Self;
  B.Left := Random(10) * (Width div 11);
  B.Top := Random(10) * (Height div 11);
  BtnList.Add(B);
  Invalidate;
end;

------
Igor.

0
 

Author Comment

by:samone
ID: 6202696
Thanks ! for the quick response.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question