#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0

    FindComponent problem


    Solved. There was nothing wrong with the code.

    I have 4 edit boxes named:

    Player1_Edit
    Player2_Edit
    Player3_Edit
    Player4_Edit

    I am doing this the short way and using FindComponent (I have other edit boxes which will also need it).

    If box is empty on form show, the box should be filled with corrosponding text "Playerx" where x is a number. The first part of this code works fine. It finds the boxes. But it is NOT updating them. Why?


    Code:
    for i:=1 to 4 do
      if TEdit(FindComponent('Player'+inttostr(i)+'_Edit')).text='' then
      TEdit(FindComponent('Player'+inttostr(i)+'_Edit')).text:='Player'+inttostr(i);
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    Code:
    for i:=1 to 4 do
      if TEdit(FindComponent('Player'+inttostr(i)+'_Edit')).text='' then
      TEdit(FindComponent('Player'+inttostr(i)+'_Edit')).text:='Player'+inttostr(i);
    I couldn't find anything obviously wrong with the code so I tested it, and works as expected. The only other possibility is that the edit controls are not empty making the if statement fail, even though it finds the control.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    I've worked it out. Ugh.

    For some reason I am using a TLMDedit not a Tedit. I did make this project a long time ago so... didn't see I had changed it.

    The code is fine. I do wish this findcomponent thing gave an error though... it should have done given the boxes are not Tedit.

    Anyway thanks!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    I do wish this findcomponent thing gave an error though... it should have done given the boxes are not Tedit.
    Not if TLMDedit is a descendant of TEdit.
    However, in that case one would assume it would work unless TLMDedit overrides the setText method somehow.

    Clive
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    55
    Rep Power
    6

    Thumbs up


    Read this before:

    http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Controls_TWinControl.html


    http://delphi.about.com/od/vclusing/a/parent_enabled.htm

    In essence, the TWinControl is the base class for all Delphi control that can accept focus. For example, TEdit is a descendant of the TWinControl, TLabel is not.

    Another way for your needed:

    procedure or functions
    //////////////////////////////
    var
    xCmp:TComponent;
    For I:= n1 TO nZ do
    begin
    xCmp:= nil; //for better control of "conditions"
    xCmp:= FindComponent(format('Player%d_edit',[i]));
    if (xCmp<>nil) and (xCmp is TCustomEdit) then
    xCmp.Text := Format('Player%d', [i]);
    end;

IMN logo majestic logo threadwatch logo seochat tools logo