Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: How can i do this?

  1. #1

    How can i do this?

    I have 10 buttons with name like Button1,Button2....

    How can i make a simple procudere like

    Procedure showbuttons(howmany: byte);
    var
    i: integer;
    begin
    for i := 0 to howmany do
    button(i).visible := true; ?
    end;

    Thanks.... I think is simply but i stack... My brain is stack...

  2. #2
    I think there is a TForm.FindControl() method that returns a control by name like so:

    Code:
    Var
      Button: TButton;
    begin
      Button := FindControl('Button1') as TButton;
    cheers,
    Paul

  3. #3
    Quote Originally Posted by paul_nicholls View Post
    I think there is a TForm.FindControl() method that returns a control by name like so:

    Code:
    Var
      Button: TButton;
    begin
      Button := FindControl('Button1') as TButton;
    cheers,
    Paul
    Thank you Paul for anwser but
    "Find Control" wants a handle control not a string
    FindControl(Handle: HWnd)

    Can i use the findComponet function ?
    But how i can get the control of the componet so i can make it visible?

    Thanks again...

  4. #4
    Just use this:

    Code:
      component := findComponent('somestring');
      if component is TControl then
        TControl(component).Visible := True;
    Hope that helps.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  5. #5
    Quote Originally Posted by chronozphere View Post
    Just use this:

    Code:
      component := findComponent('somestring');
      if component is TControl then
        TControl(component).Visible := True;
    Hope that helps.
    When i run your code i get the error that findcomponet not exists.
    I give a var that component is a Tcomponent and change the line in
    component.findcomponent('button'+IntToStr(i));
    It compiles run ok but no visible button hmmm why?

    Thank you for the help....

    Update
    I made this and it works

    for i := 0 to howmany do
    begin
    With TBitBtn(FindComponent('BitBtn'+inttostr(i))) do
    Visible := true;
    end;

    But it works only in In the main form if take the code and put it in an empty unit not working
    Any thoughts....
    Last edited by azrael11; 13-02-2011 at 05:37 PM.

  6. #6
    Ok i solve this too.

    I found a really good function from Torry's page.
    here it is...

    Code:
    function FindComponentEx(const Name: string): TComponent;
    var
      FormName: string;
      CompName: string;
      P: Integer;
      Found: Boolean;
      Form: TForm;
      I: Integer;
    begin
      // Split up in a valid form and a valid component name
      P := Pos('.', Name);
      if P = 0 then
      begin
        raise Exception.Create('No valid form name given');
      end;
      FormName := Copy(Name, 1, P - 1);
      CompName := Copy(Name, P + 1, High(Integer));
      Found    := False;
      // find the form
      for I := 0 to Screen.FormCount - 1 do
      begin
        Form := Screen.Forms[I];
        // case insensitive comparing
        if AnsiSameText(Form.Name, FormName) then
        begin
          Found := True;
          Break;
        end;
      end;
      if Found then
      begin
        for I := 0 to Form.ComponentCount - 1 do
        begin
          Result := Form.Components[I];
          if AnsiSameText(Result.Name, CompName) then Exit;
        end;
      end;
      Result := nil;
    end;
    This function gives you the flexibility to found any component in any form... an use it as you like

    Thank you all for the anwsers... This Case is SOLVED...

  7. #7
    Hmm how 'bout this one:

    Code:
    procedure renameAllLabels();
    var i : byte;
    begin
    for i := 0 to ComponentCount - 1 do
      if Components[i] is TLabel then
        (Components[i] as TLabel).caption := 'Hello';
    end;
    It does roughly the same as your example. Perhaps not so flexible, but less code.

  8. #8
    Quote Originally Posted by Traveler View Post
    Hmm how 'bout this one:

    Code:
    procedure renameAllLabels();
    var i : byte;
    begin
    for i := 0 to ComponentCount - 1 do
      if Components[i] is TLabel then
        (Components[i] as TLabel).caption := 'Hello';
    end;
    It does roughly the same as your example. Perhaps not so flexible, but less code.
    This is a nice small gold peace of code...

  9. #9
    How about
    Application.FindComponent(string)

  10. #10
    Quote Originally Posted by User137 View Post
    How about
    Application.FindComponent(string)
    Hello User137
    Application.FindComponent(String) works only from the form that have the desire component.
    If you want to control the components from an independent unit use the above code and dont forget to have the corresponding in uses....

    Thanks my friend for the anwser...

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •