Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I Change Controls' properties progrmatically

Posted on 2004-08-02
7
Medium Priority
?
152 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 750 total points
ID: 13343236
I've answered the question.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

610 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