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

    Join Date
    Dec 2003
    Posts
    12
    Rep Power
    0

    Disabling or enabling a button


    Hello, can someone tell me how to disable or enable a button depending on the name of the button that I have stored in my database but without doing if's in my procedure. What I want is the following:

    Example I have 10 buttons in a form. The name of my buttons are stored in my database. But when my database sends back my result (example only 4 names) I cannot put the name of the button because delphi does not know that that is an object, it says that it is a string which is right because you store the name of the button as a string in your database.

    I do not want to do a loop an 10 buttons and do if name is equal to btn.name
    I would like to do a proedure only with the results

    Any suggestions ? Thank you
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    The Form object has a ComponentCount property and a Components object that contains all the child components. So it is simply a matter of using a for loop to go through the list of components to find the one that you want. Something like this ought to do the trick (Warning: Untested code)
    Code:
    procedure TForm1.EnableButton(sButtonName : string);
    var
        i : integer;
        Btn: TButton;
        Component: TComponent;
    begin
        for i :=  0 to self.ComponentCount - 1 do
        begin
            Component := self.Components[i];
            if (Component is TButton) then
            begin
                Btn := Component as TButton;
                if (Btn.Name = sButtonName) then
                begin
                    Btn.Enabled := True;
                    break;
                end;
            end;
        end;
    end;

    Comments on this post

    • ASAP agrees : usefull
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo