• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

How can I Change Controls' properties progrmatically

I have a table with 4 fields, with some data like as:

Form_name       Control_Object        Property        Value
-------------------------------------------------------------
Form1               Button1                  Visible           False
Form1               Label1                    FontName     Arial
Form1               Button2                  Enabled         True
Form2               Button1                  Enabled         True
Form2               DBGrid1                  Height            200
...                      ...                              ...             ...

How can I Change the properties of these controls based on this table when I open the form?
0
khalil_moghaddam
Asked:
khalil_moghaddam
1 Solution
 
TreppenmeisterCommented:
I have no real idea what you mean - but perhaps something like this?

for lComponentNr := 0 to Form1.ComponentCount - 1 do begin
  lComponent  := Form1.Components[ lComponentNr ];
  lName  := lComponent.Name;

  if lComponent is tLabel then begin
    if lName = 'Label1' then begin
      tLabel( lComponent ).Fontname = Arial;
    end;
  end;

  if lComponent is tButton then begin
    if lName = 'Button1' then begin
      tButton( lComponent ).Visible = false;
    end;
  end;
end;

and so on

Similar to that you can search also through all Forms automatically.

0
 
pritaeasSoftware EngineerCommented:
Hi. You can use FindComponent() to find the forms in Application and then use FindComponent() for that form to find the component you want. Next you can use RTTI to set the property you select to the corresponding value. See the HelpFile and maybe the following link for more details/an example:

http://www.howtodothings.com/showarticle.asp?article=114
0
 
geobulCommented:
Hi,

Try this (works with your example above except for Fontname property because TLabel has no such property in my Delphi 5):

Add one query on a form (ADOQuery1 in this example) and connect it to your database. Write OnShow event for the form as below:

uses TypInfo;

function SetProperty(fForm: TForm; sControl: string; sProperty: string; value: variant): integer;
var
  i: integer;
  PRecProp: PPropInfo;
begin
 result := 0;
 try
  for i := 0 to fForm.ComponentCount - 1 do
    if (fForm.Components[i] is TControl) then
      if (fForm.Components[i]).Name = sControl then begin
        PRecProp := GetPropInfo(fForm.Components[i].ClassInfo, sProperty);
        if Assigned(PRecProp) then
          if (UpperCase(value) = 'FALSE') then begin
            SetOrdProp(fForm.Components[i], PRecProp, integer(False));
          end else begin
            if (UpperCase(value) = 'TRUE') then begin
              SetOrdProp(fForm.Components[i], PRecProp, integer(True));
            end else begin
              case PRecProp^.PropType^.Kind of
              tkInteger: SetOrdProp(fForm.Components[i], PRecProp, StrToInt(value));
              tkString,tkLString,tkWString: SetOrdProp(fForm.Components[i], PRecProp, integer(string(value)));
            end;
          end;
        end;
        break;
      end;
 except
   result := 1;
 end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  with ADOQuery1 do begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT * FROM Controls WHERE');
    SQL.Add('Form_name = ' + QuotedStr(self.Name));
    Open;
    while not eof do begin
      SetProperty(self, FieldByName('Control_Object').AsString, FieldByName('Property').AsString, FieldByName('Value').AsString);
      Next;
    end;
    Close;
  end;
end;

Regards, Geo
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gandalf_the_whiteCommented:
am i right that you started programming and are visually developing your forms?
you activate your objects and if you want to change a property of an object you
open the object inspector and change the value there?

to change a value without the object inspector you first have to drop a button on the
form and make a double click on this button. after that you will be in the forms unit code
in a buttonclick method. (i assume the name of the button is button1)
here you can type the following line:
button1.enabled := False;

this should give a small start.
try http://www.delphibasics.co.uk/ it looks like for beginners to me.

if i did misunderstand your level of delphi knowledge then please
ignore this comment.

regards
0
 
khalil_moghaddamAuthor Commented:
Dear  gandalf_the_white :

Does my question looks so simple?
I know Object ispector and prperties of form too, but I want to change these properties in runtime and based on a Tabe content.

I want my application identify user and change properies of form's component based on user authorities which is in table.

I think geobul's answer is correct and I am checking it.

regards
0
 
geobulCommented:
I've answered the question.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now