I have a 20 different Timages on the form named tile1, tile2, tile3 ... tile20.
tiles[] is just an array of Timage (1..20)
With the following code I am unsuccessfully loading each Timage into its respective index of the tiles[] array
procedure TForm3.FormCreate(Sender: TObject);
var
I : integer;
begin
for I := 1 to 20 do
begin
tiles[I] := TImage( ( 'tile' + inttostr(I) ) );
end;
end;
the code doesn't produce any warnings or fatal errors, the problem arises when I try to access the data of each TImage through the array
eg ShowMessage(tiles[1].name) should produce 'tile1', instead I get nothing
How can I achieve this without manually coding each Timage into the array
eg.
tiles[1] := tile1;
tiles[2] := tile2;
Thanks!
Assuming the 20 TImage components are actually on the form (since your example doesn't show it) you can use the FindComponent method of the form.
type
TForm1 = class(TForm)
tile1: TImage;
tile2: TImage;
tile3: TImage;
// ... (tiles 4 through 18 omitted to shorten the code sample)
tile19: TImage;
tile20: TImage;
procedure FormCreate(Sender: TObject);
private
tiles : array[1..20] of TImage;
public
end;
//...
procedure TForm1.FormCreate(Sender: TObject);
var
i : integer;
begin
for i := 1 to 20 do
begin
tiles[i] := TImage(FindComponent('tile'+inttostr(i)));
if tiles[i] = nil then
ShowMessage('tile'+inttostr(i)+' not found !');
end
end;
"code doesn't produce any warnings or fatal errors,"
tiles[I] := TImage( ( 'tile' + inttostr(I) ) );
The code compiles fine because you are simply casting a string as a TImage. As to not receiving any run-time messages, well, that is just pure amazing luck.
The code:
ShowMessage(tiles[1].Name);
should raise an access violation exception since tiles[1] points to a string and not to an instance of TImage.
Related
i want to ask how to retain controlls when im making a copy of a control. for example i have an edit box that can be controlled with a slider for value change. when i make a copy using this code i achieve a copy of the items but the slider stops controlling editbox values. how can i fix that?
TypInfo;
procedure CloneProperties(const Source: TControl; const Dest: TControl);
var
ms: TMemoryStream;
OldName: string;
begin
OldName := Source.Name;
Source.Name := ''; // needed to avoid Name collision
try
ms := TMemoryStream.Create;
try
ms.WriteComponent(Source);
ms.Position := 0;
ms.ReadComponent(Dest);
finally
ms.Free;
end;
finally
Source.Name := OldName;
end;
end;
procedure CloneEvents(Source, Dest: TControl);
var
I: Integer;
PropList: TPropList;
begin
for I := 0 to GetPropList(Source.ClassInfo, [tkMethod], #PropList) - 1 do
SetMethodProp(Dest, PropList[I], GetMethodProp(Source, PropList[I]));
end;
procedure DuplicateChildren(const ParentSource: TWinControl;
const WithEvents: Boolean = True);
var
I: Integer;
CurrentControl, ClonedControl: TControl;
begin
for I := ParentSource.ControlCount - 1 downto 0 do
begin
CurrentControl := ParentSource.Controls[I];
ClonedControl := TControlClass(CurrentControl.ClassType).Create(CurrentControl.Owner);
ClonedControl.Parent := ParentSource;
CloneProperties(CurrentControl, ClonedControl);
ClonedControl.Name := CurrentControl.Name + '_';
if WithEvents then
CloneEvents(CurrentControl, ClonedControl);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
DuplicateChildren(Panel1);
end;
Unless I'm misunderstanding you, your CloneProperties doesn't seem to have anything to do with the question you're asking. In your example of an edit control E1 and a slider S1, you can clone both of them to produce E2 and S2, but somewhere in your code there must be a statement that changes the value in E1 depending on the value of S1. However, in the way you've most likely written it, that statement doesn't apply to E2 and S2.
The simplest way around that is to write a method which takes the component instances and links the operation of the two together. e.g.
procedure TForm1.SetEditControlFromSlider(AnEdit : TEdit; ASlider : { TWhatever the slider actually is);
begin
// Set AnEdit's value from ASlider's properties
end;
Then, you can call this with Edit/Slider pairs like this
SetEditControlFromSlider(E1, S1);
[...]
SetEditControlFromSlider(E2, S2);
I can imagine you might not like having to do that.
IMO, the cleanest solution is to avoid attempting to clone components altogether and create a TFrame containing the Edit, Slider and the code that connects them, and then add to your form as many instances of the frame as you need. It's as easy as falling off a log.
type
TEditFrame = class(TFrame) // needs to be in its own unit, Used by your form
Edit1: TEdit;
TrackBar1: TTrackBar;
procedure TrackBar1Change(Sender: TObject);
private
public
end;
[...]
procedure TEditFrame.TrackBar1Change(Sender: TObject);
begin
Edit1.Text := IntToStr(TrackBar1.Position)
end;
Then, you can add clones of the frame to TForm1 by
procedure TForm1.Button1Click(Sender: TObject);
var
AFrame : TEditFrame;
begin
Inc(FrameCount); // Field of TForm1
AFrame := TEditFrame.Create(Self);
AFrame.Name := AFrame.Name + IntToStr(FrameCount);
AFrame.Parent := Self;
AFrame.Top := AFrame.Height * FrameCount;
end;
Note that because the code which links the two components, TrackBar1Change, it compiled into the frame's unit, it is automatically shared by every instance of the frame you create, without any need to "clone" the code.
I want to know if there is a way to address a tool which I put in my form after the program is executed? For example:
Suppose there are 100 label components in a form and you put an edit box in your form and ask the user to enter a number in the edit. When the number is written in the edit, the label with the same number will change the font colour.
But you cannot code it before running the program and need sth like this:
Label[strtoint(edit1.text)].color:=clblue;
But as you know this code does not work. What should I write to do what I want?
Yes, you can do something like you demonstrate, you just need to store the form’s controls into some type of array or list.
Sorry, I currently do not have access to my Delphi IDE, but I think I can give you an overview to what you need to do. I will also provide a link that better demonstrate the concept.
Here are the steps:
First ensure your controls have a consistent naming format that includes an index number in the name.
Example: Label1, Label2, . . . .
Next you need to store the controls into some type of an array or TList.
Example:
Var
ControlList : TList
. . . .
ControlList := TList.Create;
. . . .
{ Load the controls into the list after they been created }
ControlList.Add (Label1)
ControlList.Add (Label2)
ControlList.Add (Label3)
Here an alternatives to adding the Labels to the list manually.
for I := 1 to 3 do
begin
ControlList.Add(TLabel(FindComponent('Label'+IntToStr(I)));
end;
Now designate some event handler where you will put the code to update the label. This handler routine will first convert the user inputted value to an integer. Them use that value as an index to the control array. Once you have the label designated to be updated, set whatever properties you like.
idx := StrToInt(InputBox.Text);
lbl := TLabel( ControlList[idx])
. . . .
lbl.Color := clBlue;
Check out this link Control Arrays in Delphi for a more detailed description.
-- Update --
Although my previous answer would work, Remy Lebeau comment give me an idea to a better approach. You do not need to store the controls in an array or list, just use the Findcomponent() command to locate the control. Below are two examples demonstrating this concept.
Example using an Edit box OnKeyPress event:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
LabelControl : TLabel;
begin
if ord(Key) = VK_RETURN then
begin
LabelControl := TLabel(FindComponent('Label'+Edit1.Text));
if (LabelControl <> nil) then
LabelControl.Color := clblue;
Key := #0; // prevent continue processing of the WM_CHAR message
end;
end;
Another example using a Button's OnClick event:
procedure TForm1.Button1Click(Sender: TObject);
var
LabelControl : TLabel;
begin
LabelControl := TLabel(FindComponent('Label'+Edit1.Text));
if (LabelControl <> nil) then
begin
LabelControl.Color := clBlue;
end;
end;
Things to note about the code:
In the first example, for the label to be updated, the user must press the enter key after inputting the desired label number.
In the second example, the user must press a button after entering
the number of the label to be updated.
In In both examples, invalid responses are ignored.
As I understand you right, all the Labels already contain a number in their caption.
Then, you could use the Controls array, that already exists in TForm, which contains all controls that belong to the form:
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
// ...
Edit1: TEdit;
procedure Edit1Change(Sender: TObject);
private
public
end;
// ...
{ uses
System.RegularExpressions;
}
// ...
procedure TForm1.Edit1Change(Sender: TObject);
{
// if aLabel.Name instead of aLabel.Caption
// will work for Label1, Label2, Label3, Label4 ...
function TryNameToInt(AName: string; var ANumber: Integer): boolean;
var
aRegEx: TRegEx;
aMatch: TMatch;
aStr: string;
begin
aStr := '';
ANumber := -1;
aRegEx:= TRegEx.Create('[A-Za-z_]+([0-9]+)');
aMatch:= aRegEx.Match(AName);
if aMatch.Success then begin
aStr := aMatch.Groups.Item[1].Value;
end;
Result := TryStrToInt(aStr, ANumber);
end;}
var
aControl: TControl;
aLabel: TLabel;
aNumberEdit: Integer;
aNumberLabel: Integer;
aIdx: Integer;
begin
if TryStrToInt(Edit1.Text, aNumberEdit) then begin
for aIdx := 0 to ControlCount - 1 do begin // Controls is the list of all Controls in the form, ControlCount is the length of this list
aControl := Controls[aIdx];
if aControl is TLabel then begin // this gets only TLabel controls
aLabel := TLabel(aControl);
if TryStrToInt(aLabel.Caption, aNumberLabel)
{or TryNameToInt(aLabel.Name, aNumberLabel)} then begin
if aNumberLabel = aNumberEdit then begin
aLabel.Font.Color := clBlue;
end
else begin
aLabel.Font.Color := clWindowText; // if needed
end;
end;
end;
end;
end;
end;
You can use FindComponent function to do that:
Here I dropped a TButton and TEdit on form, you type the Label number you want to change the font color in Edit and then press the Button. Write this code in OnClick event for the Button:
Var
mColor: TColor;
mLabel: Tlabel;
begin
mColor := clGreen;
mLabel := FindComponent('Label' + Edit1.Text) as TLabel;
if mLabel <> nil then
mLabel.Font.Color := mColor;
end;
or if you don't want to press the Button and want it as you type in Edit, you have to write the code in OnChange event for Edit.
I use last TPngComponents "PngComponents for Delphi 2009 - Delphi 10.2 Tokyo".
Create simple project to show my problem.
Why after the second assign TPngImageCollectionItem object TreeView still paint first assigned image and may be need call some refresh functions?
type
TForm1 = class(TForm)
pilTree: TPngImageList;
pilNoImage: TPngImageList;
pilAllCor: TPngImageList;
tvCor: TTreeView;
pilAllNotCor: TPngImageList;
tvNoCor: TTreeView;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure AddNodes(ATV: TTreeView);
var
nFirst, nChild: TTreeNode;
begin
nFirst := ATV.Items.AddChild(nil, '1');
nChild := ATV.Items.AddChild(nFirst,'2');
nChild.ImageIndex := 1;
nChild.SelectedIndex := 1;
nFirst.Expanded := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
iI: Integer;
ItemAdd: TPngImageCollectionItem;
ANode: TTreeNode;
begin
// Steps working correct
for iI := 0 to 1 do begin
ItemAdd := pilAllCor.PngImages.Add;
ItemAdd.Assign(pilTree.PngImages[iI]);
end;
// Steps working NOT correct
for iI := 0 to 1 do begin
ItemAdd := pilAllNotCor.PngImages.Add;
ItemAdd.Assign(pilNoImage.PngImages[0]);
ItemAdd.Assign(pilTree.PngImages[iI]);
end;
//Setup treeview
tvCor.Images := pilAllCor;
tvNoCor.Images := pilAllNotCor;
AddNodes(tvCor);
AddNodes(tvNoCor);
end;
Example:
The way to add a TPngImage to a TPngImageList is using AddPng and not fiddling around with the collection. This will also update the underlying Windows image list responsible for the actual display of the image.
The correct code should look like:
for iI := 0 to 1 do begin
pilAllCor.AddPng(pilTree.PngImages[iI].PngImage);
end;
If you want to change an existing item you should assign the PngImage property of the collection item:
pilAllCor.PngImages[iI].PngImage := pilTree.PngImages[iI].PngImage;
I'm using VFI (Visual Form Inheritance) and I need to check if a component of an instantiated form belongs to the formclass or to the form superclass.
any ideas ?
unit1
TFormStatus = class(TForm)
cpPanel: TPanel;
lblStatus: TLabel;
end;
unit 2
TFormCodigo = class(TFormStatus)
lblCodigo: TLabel;
end;
frmCodigo: TFormCodigo:
In any instances of frmCodigo I want to detect that lblCodigo is local to TFormCodigo and cpPanel / lblStatus are inherited components;
for i:=0 to Self.ComponentCount-1 do begin
if "InheritedComponent" (Self.Components[i]) then ...
end;
Something like this is possible using RTTI for object properties, but I dont know if it is possible for components.
Thanks.
If I understand you correctly, you need TRttiMember.Parent. For example see this article by Rob Love. You'll need Delphi 2010 or later I think.
In fact this is just part of an excellent series of articles - these articles will also tell you how to get hold of the fields, properties etc. without having to know their names.
Maybe something "stupid" like
function TFormStatus.IsStatusComponent(AComponent: TComponent): Boolean;
begin
Result := (AComponent = cpPanel) or (AComponent = lblStatus);
end;
already fulfils your needs?
In your TFormCordigo you can override ReadState method that is called every time a resource is read for a particular form. After inherited called ComponentCount contains the number of components created up to the current member of hierarchy, so after all you have list of borders for components that you can save elsewhere.
The code below illustrates this approach
procedure TInhTestForm.Button3Click(Sender: TObject);
var
i: integer;
begin
inherited;
Memo1.Lines.Clear;
for i:=0 to ComponentCount-1 do
begin
Memo1.Lines.Add(format('%s inroduced in %s', [Components[i].Name, ComponentParent(i).ClassName]));
end;
end;
function TInhTestForm.ComponentParent(Index: integer): TClass;
var
i, j: integer;
begin
Result:=Nil;
for i:=Low(fComponentBorders) to High(fComponentBorders) do
begin
if Index <= fComponentBorders[i] - 1 then
begin
j:=i;
Result:=Self.ClassType;
while j < High(fComponentBorders) do
begin
Result:=Result.ClassParent;
Inc(j);
end;
break;
end;
end;
end;
procedure TInhTestForm.ReadState(Reader: TReader);
begin
inherited;
SetLength(fComponentBorders, Length(fComponentBorders) + 1);
fComponentBorders[High(fComponentBorders)]:=ComponentCount;
end;
In Form1 I have PageControl. At run time my program creates tab sheets. In each TabSheet I create Form2. In Form2 I have a Memo1 component. How can I add text to Memo1?
You could do something like this:
(PageControl1.Pages[0].Controls[0] as TForm2).Memo1.Lines.Add('text');
If I get right what are you doing,
procedure TForm1.Button1Click(Sender: TObject);
var
View: TForm;
Memo1, Memo2: TMemo;
Page: TTabSheet;
I: Integer;
begin
View:= TForm2.Create(Form1);
View.Parent:= PageControl1.Pages[0];
View.Visible:= True;
View:= TForm2.Create(Form1);
View.Parent:= PageControl1.Pages[1];
View.Visible:= True;
// find the first memo:
Page:= PageControl1.Pages[0];
Memo1:= nil;
for I:= 0 to Page.ControlCount - 1 do begin
if Page.Controls[I] is TForm2 then begin
Memo1:= TForm2(Page.Controls[I]).Memo1;
Break;
end;
end;
Page:= PageControl1.Pages[1];
// find the second memo:
Memo2:= nil;
for I:= 0 to Page.ControlCount - 1 do begin
if Page.Controls[I] is TForm2 then begin
Memo2:= TForm2(Page.Controls[I]).Memo1;
Break;
end;
end;
if Assigned(Memo1) then Memo1.Lines.Add('First Memo');
if Assigned(Memo2) then Memo2.Lines.Add('Second Memo');
end;
I see one big problem with this code--Memo2 is going to have exactly the same value as Memo1 as there's no difference in the search loops. Also, if this code is complete then there's nothing but the form on the page, there's no reason for a search loop at all.
VilleK's answer should compile and run, I don't see what you are asking for.
So, I solved my problem with your help. This is my code:
var
ID, I: integer;
Tekstas: string;
View: TForm2;
Memo: TMemo;
Page: TTabSheet;
begin
...
Page := PageControl.Pages[ID];
for i := 0 to Page.ControlCount - 1 do
begin
(PageControl.Pages[ID].Controls[0] as TKomp_Forma).Memo.Lines.Add('['+TimeToStr(Time)+']'+Duom[ID].Vardas+': '+Tekstas);
end;
end;
Hope this helps someone else