Solved

How can I Change Controls' properties progrmatically

Posted on 2004-08-02
7
147 Views
Last Modified: 2010-04-05
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
Comment
Question by:khalil_moghaddam
7 Comments
 

Expert Comment

by:Treppenmeister
ID: 11691990
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
 
LVL 6

Expert Comment

by:pritaeas
ID: 11692008
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
 
LVL 17

Expert Comment

by:geobul
ID: 11692505
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 11714185
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
 
LVL 1

Author Comment

by:khalil_moghaddam
ID: 12091090
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
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
ID: 13343236
I've answered the question.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question