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
Solved

how to make controls readonly on a form

Posted on 2002-03-21
14
180 Views
Last Modified: 2010-04-05
I have a form on which i have one panel and one tab control. and inside the panel and tab control i have many controls. Now i want to enable the panel and tab control
but disable the control in both of them.

If i use form.controls then it only takes two control that is panel and tab control

any suggestion. What is the best and efficient way of doing this

:)
rajni
0
Comment
Question by:rajniyadav
  • 7
  • 3
  • 3
  • +1
14 Comments
 
LVL 4

Expert Comment

by:jsweby
ID: 6885157
You should iterate through the Components on the form, and for each component you reach, check its parent and set it to ReadOnly := False if it is the panel or the tabcontrol.

For i := 0 to ComponentCount - 1 Do
  If (Components[i].Parent is Panel1) then
    If (Components[i] is TDBEdit) then
    (Components[i] as TDBEdit).ReadOnly := False ...etc...

J.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6885159
use form.components instead

0
 
LVL 17

Accepted Solution

by:
geobul earned 60 total points
ID: 6885176
function DisableControls(fForm: TForm; const sParent: string): integer;
var
  i: integer;
begin
 result := 0;
 try
  for i := 0 to fForm.ComponentCount - 1 do
    if (fForm.Components[i] is TWinControl) then
      if (fForm.Components[i] as TWinControl).Parent.Name = sParent then
        (fForm.Components[i] as TWinControl).Enabled := false;
 except
   result := 1;
 end;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  DisableControls(Form1,'Panel1');
  DisableControls(Form1,'TabControl1');
end;

Regards, Geo
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 17

Expert Comment

by:geobul
ID: 6885237
About ReadOnly: you could use TypInfo to find out if a control has ReadOnly property before setting its value to true.

uses
 TypInfo;

function MakeReadOnly(fForm: TForm; const sParent: string): 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] as TControl).Parent.Name = sParent then begin
        PRecProp := GetPropInfo(fForm.Components[i].ClassInfo, 'ReadOnly');
        if Assigned(PRecProp) then
          SetOrdProp(fForm.Components[i], PRecProp, Integer(true));
      end;
 except
   result := 1;
 end;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  if MakeReadOnly(Form1,'Panel1') <> 0 then ShowMessage('Error making readonly');
end;

Regards, Geo
0
 

Author Comment

by:rajniyadav
ID: 6887837
geobul

I am using your function but it gives me this error "Invalid Class TypeCast" for
one component. Acutally i am using a third party tab control. and after error it exits the function.
What i want it after error it should continue to check of other controls and enable or disable them.

Is it possible

rajni
0
 

Author Comment

by:rajniyadav
ID: 6887855
hi geobul

i think it is giving the previous error for the labels
Is there any way i can check if the component is lable then donot enable or disable it

do u have any ideas


rajni


0
 

Author Comment

by:rajniyadav
ID: 6887882
hi geobul

I used your code and did this way and it is working

Procedure EnableControls(frmForm : TForm; blnValue : Boolean);
var
  intI : Integer;
begin
  {This function takes a form and then according to the value passed disables
  or enables the controls}
  with frmForm do begin
    for intI := 0 to ComponentCount-1  do
    begin
     {if component is label then don't disable it}
     if not(Components[intI]  is TLabel) then begin
        (Components[intI] as TControl).Enabled := blnValue;
      end;
    end;
  end;
end;

Thanx a lot
rajni
0
 

Author Comment

by:rajniyadav
ID: 6887886
Thanx a lot
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6888217
That looks very similar to my suggestion...

J.
0
 

Author Comment

by:rajniyadav
ID: 6888227
hi jsweby

ya actually u are right byt i misunderstood your code first and tried geobul which worked after making some changes.

sorry for that

0
 

Author Comment

by:rajniyadav
ID: 6888230
jsweby

but u were checking only for the dbedit and not for all the control
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6888248
No, I was giving you an example of where to start, the rest is identical.

J.
0
 
LVL 17

Expert Comment

by:geobul
ID: 6888345
Hi,
TLabel is not a WinControl, so the function could be:

function DisableControls(fForm: TForm; const sParent: string): integer;
var
 i: integer;
begin
result := 0;
try
 for i := 0 to fForm.ComponentCount - 1 do
   if (fForm.Components[i] is TControl) then
     if (fForm.Components[i] as TControl).Parent.Name = sParent then
       (fForm.Components[i] as TControl).Enabled := false;
except
  result := 1;
end;
end;

Regards, Geo
0
 

Author Comment

by:rajniyadav
ID: 6888357
ya i did that
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

792 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