Solved

How can I Change Controls' properties progrmatically

Posted on 2004-08-02
7
143 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

by:gandalf_the_white
Comment Utility
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
Comment Utility
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
Comment Utility
I've answered the question.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now