Findchildcontrol and Findcomponent

Source: Internet
Author: User

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?
    1. //controlcount property Read method   
    2. function twincontrol
    3. begin   
    4.   result := 0;  
    5.   if fcontrols <>  Nil then inc (result, fcontrols
    6.   if fwincontrols  <> nil then inc (result,  Fwincontrols
    7. 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?
  1. Procedure Twincontrol.  Insert (Acontrol:tcontrol);
  2. Begin
  3. if Acontrol <> nil Then
  4. begin
  5. if Acontrol is twincontrol Then
  6. begin
  7. Listadd (Fwincontrols, Acontrol); //If Twincontrol is added to the Fwincontrols list
  8. ...
  9. End
  10. Else
  11. Listadd (Fcontrols, Acontrol); //Otherwise add to Fcontrols list
  12. ...
  13. end;
  14. 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?
  1. function Twincontrol.  Findchildcontrol (const controlname: string): Tcontrol;
  2. Var
  3. I:integer;
  4. Begin
  5. Result: = nil;
  6. if Fwincontrols <> nil Then
  7. For I: = 0 to fwincontrols. Count- 1 do
  8. if Comparetext (Twincontrol (Fwincontrols[i]). Name, controlname) = 0 Then
  9. begin
  10. Result: = Tcontrol (Fwincontrols[i]);
  11. Exit;
  12. end;
  13. End

Test

The entire test interface

1. Self.findchildcontrol (Form Call)

Test code:

[Delphi]View Plaincopyprint?
  1. Findchildcontrol Find
  2. Procedure TForm1Btn1click (sender:tobject);
  3. Var
  4. C:tcontrol;
  5. Begin
  6. Memo1.  Clear;
  7. Memo1. Lines. ADD (' Findchildcontrol find ' +edit1.  text+' ... ');
  8. C:=self. Findchildcontrol (Edit1. Text); //Form call
  9. if (c=nil) Then
  10. Memo1. Lines. ADD (' not found! ')
  11. Else
  12. begin
  13. Memo1. Lines. ADD (' Find!  ‘);
  14. Memo1. Lines. ADD (' parent control Name: ' +c. Parent.  Name);
  15. end;
  16. 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?
  1. Findchildcontrol Lookup--parent control call
  2. Procedure TForm1Btn4click (sender:tobject);
  3. Var
  4. C:tcontrol;
  5. Begin
  6. Memo1.  Clear;
  7. Memo1. Lines. ADD (' Findchildcontrol find ' +edit1.  text+' ... ');
  8. C:=panel1. Findchildcontrol (Edit1. Text); //panel1 call
  9. if (c=nil) Then
  10. Memo1. Lines. ADD (' not found! ')
  11. Else
  12. begin
  13. Memo1. Lines. ADD (' Find!  ‘);
  14. Memo1. Lines. ADD (' parent control Name: ' +c. Parent.  Name);
  15. end;
  16. End

Test Result: Find Button11 (Parent container is Panel1)

3, Self.findcomponent

Test code:

[Delphi]View Plaincopyprint?
  1. Findcomponent Find
  2. Procedure TForm1Btn2click (sender:tobject);
  3. Var
  4. C:tcomponent;
  5. Begin
  6. Memo1.  Clear;
  7. Memo1. Lines. ADD (' findcomponent find ' +edit1.  text+' ... ');
  8. C:=self. Findcomponent (Edit1. Text); //Form call
  9. if (c=nil) Then
  10. Memo1. Lines. ADD (' not found! ')
  11. Else
  12. begin
  13. Memo1. Lines. ADD (' Find!  ‘);
  14. Memo1. Lines. ADD (' owner name: ' +c. Owner.  Name);
  15. end;
  16. 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?
  1. Procedure TForm1.  Button6click (Sender:tobject);
  2. Var
  3. Lbl:tlabel;
  4. C:tcomponent;
  5. Begin
  6. Lbl:=tlabel. Create (PANEL2); //dynamically Create and specify owner
  7. LbL.  name:=' Label6 ';
  8. Memo1.  Clear;
  9. Memo1. Lines.  ADD (' findcomponent find Label6 ... ');
  10. C:=panel2. Findcomponent (' Label6 '); Owner calls
  11. if (c=nil) Then
  12. Memo1. Lines. ADD (' not found! ')
  13. Else
  14. begin
  15. Memo1. Lines. ADD (' Find!  ‘);
  16. Memo1. Lines. ADD (' owner name: ' +c. Owner.  Name);
  17. end;
  18. {The following code is also required to display on the form}
  19. LbL. Parent:=panel2; //parent default is nil, so "no display"
  20. LbL. left:=50; //Specify a location relative to the parent
  21. LbL.  top:=50;
  22. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.