I clone a panel and its contents(A image and a checkbox) 20 times.
Sample of the panel being cloned:
This is the procedure used to clone a whole panel:
procedure TForm1.ClonePanel(pObjectName: Tpanel);
var apanel : Tpanel;
Ctrl, Ctrl_: TComponent;
i: integer;
begin
//handle the Control itself first
TComponent(apanel) := CloneComponent(pObjectName);
with apanel do
begin
Left := 24;
Top :=64;
end;
//now handle the childcontrols
for i:= 0 to pObjectName.ControlCount-1 do
begin
Ctrl := TComponent(pObjectName.Controls[i]);
Ctrl_ := CloneComponent(Ctrl);
TControl(Ctrl_).Parent := apanel;
TControl(Ctrl_).Left := TControl(Ctrl).Left;
TControl(Ctrl_).top := TControl(Ctrl).top;
end;
end;
The following is the the code that physically does the cloning(called above):
function TForm1.CloneComponent(AAncestor: TComponent): TComponent;
var
XMemoryStream: TMemoryStream;
XTempName: string;
begin
Result:=nil;
if not Assigned(AAncestor) then
exit;
XMemoryStream:=TMemoryStream.Create;
try
XTempName:= AAncestor.Name;
AAncestor.Name:='clone_' + XTempName + inttostr(panels);
inc(panels);
XMemoryStream.WriteComponent(AAncestor);
AAncestor.Name:=XTempName;
XMemoryStream.Position:=0;
Result:=TComponentClass(AAncestor.ClassType).Create(AAncestor.Owner);
if AAncestor is TControl then TControl(Result).Parent:=TControl(AAncestor).Parent;
XMemoryStream.ReadComponent(Result);
finally
XMemoryStream.Free;
end;
end;
So now I want to use the cloned objects but how do I call them in my code?
For example how can I call the checked function of one of the cloned check boxes?
Thanks for your help :)
Others are right and it is better to use frame but if we want just use your code we must fix it first. there is a problem in your code and that is the Inc(panles); position. you must put this line after loop of for i:= 0 to pObjectName.ControlCount-1 do in the ClonePanle procedure, not in the CloneComponent function.
If you fix that, then you can use FindComponent function to access the components that you want as Marko Paunovic said.
For example the name of the component that you put on the first Panel that you defined as the first instance which other cloned panels are cloned from that is TestCheckBox. If you cloned 20 times the Panel that we talked about; you can access the TCheckBox of the 16th Cloned obejct like this and changing it's caption to whatever you want:
(I suppose that the panels variable was 0, when the program started.)
TCheckBox(FindComponent('clone_TestCheckBox15')).Caption:='aaaaa';
Related
I have ComboBox4,ComboBox1 and Button5
When I click Button5 program should remove component selected in combobox4 from the ComboBox4 and ComboBox1 components' list. But I get list out of bounds error with the following code...
procedure TForm1.Button5Click(Sender: TObject);
var
cat : Integer;
trinti: TComponent;
catT : String;
begin
catT := ComboBox4.Text;
cat := ComboBox4.Items.IndexOf(catT);
trinti := ComboBox4.Components[cat];
ComboBox1.Items.BeginUpdate;
ComboBox4.Items.BeginUpdate;
ComboBox4.RemoveComponent(trinti);
ComboBox1.RemoveComponent(trinti);
ComboBox1.Items.EndUpdate;
ComboBox4.Items.EndUpdate;
removeCat(catT);
end;
Please help :(
The Components property, and the RemoveComponent method are the wrong things to use here. These are for ownership and lifetime management. Typically the only thing on your form that owns anything is the form itself. So using Components on the combo box will always results in an error.
Instead you need to use the Items property of the combo box, and its Delete method. It might look like this:
var
Index: Integer;
....
catT := ComboBox4.Text;
Index := ComboBox4.Items.IndexOf(catT);
if Index <> -1 then
ComboBox4.Items.Delete(Index);
With the code below I am trying to reload a DirectionsResult back into a TGMDirections.
procedure Form2.Button2Click(Sender: TObject);
var
DR: TDirectionsResult;
i: Integer;
begin
DR:= TDirectionsResult.Create(Form1.FDirection, 0);
DR.XMLData.BeginUpdate;
for i:= 0 to Memo1.Lines.Count - 1 do
begin
DR.XMLData.Append(Memo1.Lines[i]);
end;
DR.XMLData.EndUpdate;
ShowMessage(Form1.FDirection.DirectionsResult[0].Routes[0].Leg[0].EndAddress);
end;
All seems well until the ShowMessage where I get a List out of bounds message.
I take it that the DR has not been created or the Memo has not loaded into the DirectionsResult.
Further adaption has confirmed the DirectionsResult[0] does not exist.
Help with the correction would be greatly appreciated.
You can't add a TDirectionsResult to DirectionsResult array programatically, you need to invoke Execute method from TGMDirections object.
However you can do something like this
procedure TForm1.Button1Click(Sender: TObject);
var
DR: TDirectionsResult;
begin
DR:= TDirectionsResult.Create(GMDirection1, 1);
DR.XMLData.Text := Memo1.Lines.Text;
ShowMessage(DR.Routes[0].Leg[0].EndAddress);
end;
That is, you can work without problems with your object and you can access to all properties and methods.
Note the assignation between XMLData and Memo.Lines, don't assign line to line because the control of XML is made on OnChange event of XMLData.
Regards.
So here is my situation. I have a Form (MainMenu) and a Frame (TestFrame). TestFrame is displayed on a TPanel located on MainMenu. Using this code:
frTestFrame := TfrTestFrame.Create(nil);
frTestFrame.Parent := plMain;
frTestFrame.Align := alClient;
frTestFrame.Visible := true;
TestFrame displays fine with no error. TestFrame has a few TEdit boxes on it. A TButton on MainMenu calls a procedure located in TestFrame to check if the TEdit boxes text property is null.
procedure TfmMainMenu.tbCheckClick(Sender: TObject);
begin
frTestFrame.Check;
end;
This function on TestFrame is supposed to go through all the "TEdit" components and use the function GetErrorData that returns a string if the TEdit's text property is null. That string is added to a TStringList and displayed if any TEdit boxes are null.
function TfrTestFrame.Check: Boolean;
var
ErrorList: TStringList;
ErrorString: string;
I: Integer;
begin
ErrorList := TStringList.Create;
for I := 0 to (frTestFrame.ComponentCount - 1) do
begin
if (frTestFrame.Components[I] is TEdit) then
begin
ErrorString := GetErrorData(frTestFrame.Components[I]);
if (ErrorString <> '') then
begin
ErrorList.Add(ErrorString);
end;
end;
end;
if (ErrorList.Count > 0) then
begin
ShowMessage('Please Add The Following Information: ' + #13#10 + ErrorList.Text);
result := false;
end;
result := true;
end;
function TfrTestFrame.GetErrorData(Sender: TObject): string;
var
Editbox: TEdit;
ErrorString: string;
begin
if (Sender is TEdit) then
begin
Editbox := TEdit(Sender);
if (Editbox.Text <> '') then
begin
Editbox.Color := clWindow;
result := '';
end
else
begin
Editbox.Color := clRed;
ErrorString := Editbox.Hint;
result := ErrorString;
end;
end;
end;
The problem is that when it hits the line "for I := 0 to (frTestFrame.ComponentCount - 1) do
" It blows up and I get the error "Access violation at 0x00458... Read of address 0x000..."
I do not know why this error is happening. I can only assume that maybe the Frame is not getting creating. Any help would be great. Thanks in advance.
According to your question, the line
for I := 0 to (frTestFrame.ComponentCount - 1) do
leads to an access violation at address 0x000..... Now, for a start, why won't you tell us the precise error message with the full details? Hiding the address makes it harder!
Anyway, it looks like the address is going to be a value very close to zero. In any case the only explanation for an access violation there is that frTestFrame is invalid. Most likely it is nil.
I note that the code in question is inside a TfrTestFrame method. So why do you use frTestFrame to refer to the object? You are already inside an instance of the object. Do you have multiple global variables named frTestFrame? Perhaps one in the main form unit and one in the frame unit?
You should stop using global variables for your GUI objects. I know that the IDE leads you that way. Resist the temptation to program that way. Abuse of global variables leads to pain and suffering.
Since the code is inside a TfrTestFrame method you can use Self. In all your TfrTestFrame methods remove all references to frTestFrame. Your loop should be like this:
for I := 0 to ComponentCount - 1 do
and the rest of the methods in that class need similar treatment. Note that you don't need to explicitly write Self and it is idiomatic not to.
Finally, I urge you to learn how to use the debugger. It's a wonderful tool and if you would use it, it would have told you what the problem was. Don't be helpless, let the tools help you.
I don't know exactly how to explain my question. Here is my try to explain: the function FindNext(SearchRec) will get me the next file from a directory. In my application I am looking sometimes go to backward a few files from my current SearchRec index. So how can I do that?
So I am looking for the oppose of FindNext(SearchRec) a function like FindBackward(SearchRec)
There's no such function. You'll need to keep track of previous hits in a list, say, and do the back tracking using that list.
I suggest to place them in an array of TSearchRec
SearchRecArr:array of TSearchRec;
Then when you reach a certain file, get the SearchRec that you need from the Array.
for example, this is an example where I placed in some folder 3 text files of names (z, z1, & z2).
then If I reached 'z2.txt' I will read SearchRec 2 steps backwards:
procedure TForm2.Button1Click(Sender: TObject);
var
SearchRec:TSearchRec;
SearchRecArr:array of TSearchRec;
i:integer;
begin
i:=-1;
if FindFirst('C:\Users\zeina.shehab\Desktop\New folder\z*.txt',faAnyFile,SearchRec)=0 then
begin
repeat
SetLength(SearchRecArr,length(SearchRecArr)+1);
SearchRecArr[high(SearchRecArr)]:=SearchRec;
inc(i);
if SearchRec.Name='z2.txt' then
caption:=SearchRecArr[i-2].Name;
until (FindNext(SearchRec)<>0);
end;
end;
I wrote my own function. Here is the code which works very well for me and it is very efficient for thousands of files(because it doesn't slow my playback algorithm).
Procedure GetBackward(var SRInitial:TSearchRec; iForwardSpeed:integer);
var SR:TSearchRec;
iIndex:integer;
vLastFiles:Array of String;
begin
SetLength(vLastFiles,Trunc(iForwardSpeed));
FindFirst(sPath+'*.txt',faAnyFile,SR);
while (FindNext(SR) = 0)and(SR.Name <> SRInitial.Name) do
begin
for iIndex := 0 to high(vLastFiles)-1 do
vLastFiles[iIndex]:=vLast[iIndex+1];
vLastFiles[high(vLastFiles)]:=SR.Name;
end;
//Fewer than ForwardSpeed
if vLastFiles[0] = '' then
begin
Exit;
end;
FindClose(SR);
FindClose(SRInitial);
FindFirst(sPath+'*.'+cbType.Text,faAnyFile,SRInitial);
while (FindNext(SRInitial) = 0)and(SRInitial.Name <> vLastFiles[0]) do
;
end;
The function was modified.
Whenever Skype is in Default View, the TConversationWindow's become children of the tSkMainForm Window.
I am having problems finding out which TConversationWindow is active - however it's not like an MDI interface - only one TConversationWindow is visible, like if it was a Tab/Page.
When I do GetForegroundWindow, Skype's MainForm handle is returned (tSkMainForm). Is there any way that I can get the foreground TConversationWindow within Skype?
This question of mine has screenshots of Skype's Default View, if you need it. :)
EDIT: Here is a screenshot of the Winspector Hierachy:
EDIT2: I tried going thru the windows like this:
procedure TForm1.Button1Click(Sender: TObject);
function GetClassName(Handle: HWND): String;
var
Buffer: array[0..MAX_PATH] of Char;
begin
Windows.GetClassName(Handle, #Buffer, MAX_PATH);
Result := String(Buffer);
end;
Var
Wnd: HWND;
SkypeWnd: HWND;
begin
SkypeWnd := FindWindow('tSkMainForm',nil);
Wnd := GetTopWindow(SkypeWnd);
while (GetClassName(Wnd) <> 'TConversationForm') and (Wnd <> 0) and (not IsWindowVisible(Wnd)) do
begin
Wnd := GetNextWindow(Wnd,GW_HWNDNEXT);
end;
Label1.Caption := GetClassName(Wnd)+' - '+GetHandleText(wnd);
end;
The above is supposed to find the visible window, however when I debug it, it never enters the Begin End within the While loop, and Label1 displays "TChromeMenu - ChromeToolbar". When I remove the IsWindowVisible check, it atleast finds a TConversationForm. What am I doing wrong?
EDIT3: By placing the IsWindowVisible and getClassName check inside the loop, and break when true, I managed to do it. :)
By placing the IsWindowVisible and getClassName check inside the loop, and break when true, I managed to do it. :)