The first two days of the code encountered to use the Findchildcontrol method to get the specified name of the Tspeedbutton button, the results of a half-day is no result (foundation is not solid, hehe), so hurriedly search the next, remedial about the use of these two methods.
The Findchildcontrol method of the Twincontrol class looks in Fwincontrols for a component (inherited from the Twincontrol class) that returns the specified name in the visual form. The method can determine whether the current control contains (contain) a child control of the specified name that inherits from the Twincontrol class, and the result is related to the parent property of the control that you specified to find. If no return nil (NULL) is found, the method finds only the immediate child controls of the current control and does not iterate over the child controls that look for child controls.
The Findcomponent method of the Tcomponent class finds the component that returns the specified name in fcomponents. This method determines whether the current component owns (OWN) a component with the specified name, and the result is related to the owner property specified at the time that the component that specified the lookup was created. The created component on the form designer has its owner as a form, so this method is typically called using self.findcomponent. The method parameter is not case-sensitive.
The Parent property is the parents container for the specified control, and the control can only be displayed and moved within the scope of the parent container. The owners property is the owner of the specified component, which is responsible for the creation and release of the component. Adding a component in the Form editor defaults to setting the Owner property to the form it belongs to, so you can definitely find it with the findcomponent of the form. If you create a component dynamically, you must specify its owner and parent before you can call the appropriate method to find it. And, if you put it on the Twincontrol inheritance class, then you can find it with Findchildcontrol, otherwise you won't find it.
Findcomponent is very simple, the following emphasis on Findchildcontrol this tangled method "Note 1".
Here are a few basic classes to understand first:
1, tcomponent all components base class
2. Tcontrol Runtime visual components (base classes for Twincontrol and Tgraphiccontrol)
3. Twincontrol the base class for components that are visible and have forms
Twincontrol is the base class of the Windows Control Library, and the controls inherited from the Twincontrol have control handles, that is, inside windows with unique tags that can be found dynamically indexed.
Delphi uses two lists to maintain the control's child controls: Fwincontrols and Fcontrols. The former holds a control that has a handle, that is, a control that inherits from Twincontrol, which holds a control that has no handles and generally inherits from the Tgraphiccontrol control. So the Controlcount property value of the control is the sum of Fwincontrols.count and Fcontrols.count.
[Delphi]View Plaincopyprint?
- //controlcount property Read method
- function twincontrol
- begin
- result := 0;  
- if fcontrols <> Nil then inc (result, fcontrols
- if fwincontrols <> nil then inc (result, Fwincontrols
- end;
When you create a child control, the Insert method is called to add the child control to the two lists of the parent container.
[Delphi]View Plaincopyprint?
- Procedure Twincontrol. Insert (Acontrol:tcontrol);
- Begin
- if Acontrol <> nil Then
- begin
- if Acontrol is twincontrol Then
- begin
- Listadd (Fwincontrols, Acontrol); //If Twincontrol is added to the Fwincontrols list
- ...
- End
- Else
- Listadd (Fcontrols, Acontrol); //Otherwise add to Fcontrols list
- ...
- end;
- End
Now look at the implementation code of Findchildcontrol, you can see that Findchildcontrol only looked for the Fwincontrols list, which is what it says it only looks for components that are visible and have forms, that is, components that inherit from the Twincontrol class.
[Delphi]View Plaincopyprint?
- function Twincontrol. Findchildcontrol (const controlname: string): Tcontrol;
- Var
- I:integer;
- Begin
- Result: = nil;
- if Fwincontrols <> nil Then
- For I: = 0 to fwincontrols. Count- 1 do
- if Comparetext (Twincontrol (Fwincontrols[i]). Name, controlname) = 0 Then
- begin
- Result: = Tcontrol (Fwincontrols[i]);
- Exit;
- end;
- End
Test
The entire test interface
1. Self.findchildcontrol (Form Call)
Test code:
[Delphi]View Plaincopyprint?
- Findchildcontrol Find
- Procedure TForm1Btn1click (sender:tobject);
- Var
- C:tcontrol;
- Begin
- Memo1. Clear;
- Memo1. Lines. ADD (' Findchildcontrol find ' +edit1. text+' ... ');
- C:=self. Findchildcontrol (Edit1. Text); //Form call
- if (c=nil) Then
- Memo1. Lines. ADD (' not found! ')
- Else
- begin
- Memo1. Lines. ADD (' Find! ‘);
- Memo1. Lines. ADD (' parent control Name: ' +c. Parent. Name);
- end;
- End
Test results:
(1) Find Button1 (Inherit from Twincontrol)
(2) Find Label1 (Inherit from Tgraphiccontrol)
(3) Find Button11 (parent container is Panel1)
2, Panel1.findchildcontrol
Test code:
[Delphi]View Plaincopyprint?
- Findchildcontrol Lookup--parent control call
- Procedure TForm1Btn4click (sender:tobject);
- Var
- C:tcontrol;
- Begin
- Memo1. Clear;
- Memo1. Lines. ADD (' Findchildcontrol find ' +edit1. text+' ... ');
- C:=panel1. Findchildcontrol (Edit1. Text); //panel1 call
- if (c=nil) Then
- Memo1. Lines. ADD (' not found! ')
- Else
- begin
- Memo1. Lines. ADD (' Find! ‘);
- Memo1. Lines. ADD (' parent control Name: ' +c. Parent. Name);
- end;
- End
Test Result: Find Button11 (Parent container is Panel1)
3, Self.findcomponent
Test code:
[Delphi]View Plaincopyprint?
- Findcomponent Find
- Procedure TForm1Btn2click (sender:tobject);
- Var
- C:tcomponent;
- Begin
- Memo1. Clear;
- Memo1. Lines. ADD (' findcomponent find ' +edit1. text+' ... ');
- C:=self. Findcomponent (Edit1. Text); //Form call
- if (c=nil) Then
- Memo1. Lines. ADD (' not found! ')
- Else
- begin
- Memo1. Lines. ADD (' Find! ‘);
- Memo1. Lines. ADD (' owner name: ' +c. Owner. Name);
- end;
- End
Test results
(1) Find Button1 (parent container is Form1), Button11 (parent container is Panel1)
(2) Find Label1 (Inherit Tcustomlabel→tgraphiccontrol), SpeedButton1 (inheritance Tgraphiccontrol), Timer1 (inheritance tcomponent)
4, Panel1.findcomponent
Test code:
[Delphi]View Plaincopyprint?
- Procedure TForm1. Button6click (Sender:tobject);
- Var
- Lbl:tlabel;
- C:tcomponent;
- Begin
- Lbl:=tlabel. Create (PANEL2); //dynamically Create and specify owner
- LbL. name:=' Label6 ';
- Memo1. Clear;
- Memo1. Lines. ADD (' findcomponent find Label6 ... ');
- C:=panel2. Findcomponent (' Label6 '); Owner calls
- if (c=nil) Then
- Memo1. Lines. ADD (' not found! ')
- Else
- begin
- Memo1. Lines. ADD (' Find! ‘);
- Memo1. Lines. ADD (' owner name: ' +c. Owner. Name);
- end;
- {The following code is also required to display on the form}
- LbL. Parent:=panel2; //parent default is nil, so "no display"
- LbL. left:=50; //Specify a location relative to the parent
- LbL. top:=50;
- End
Test results:
Note 1: The tangle is not because of the method itself, but at the beginning I wrote this demo to test the use of this method and its results, at that time did not pay attention to Tlabel is not inherited from Twincontrol (hehe, usually not pay attention to these, Naturally think these ordinary controls are inherited from Twincontrol), the result is estimated that the compiler problem, with Self.findchildcontrol can query to Label1, with Panel1.findchildcontrol can also query to the label ( Before the label is tested), But in the Tabsheet.findchildcontrol can not find the corresponding label, and the compiler can not set a breakpoint in the Findchildcontrol, and later on the Internet to see that a switch to open the compiler, but the results of the implementation is the same. The next day, inadvertently see Tlabel not inherit from the Twincontrol, this is depressing, how can I find out? Open the demo immediately after the new run, the results of such a sudden can not be found, it is an egg pain. At last, the principle of this method and its realization process are verified.
http://blog.csdn.net/tht2009/article/details/6954880
Findchildcontrol and Findcomponent