Solved

How can I Change Controls' properties progrmatically

Posted on 2004-08-02
7
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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