Solved

how to make controls readonly on a form

Posted on 2002-03-21
14
176 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 78
Magic Software info 18 125
Delphi 2 51
Delphi Ado Query To Converts Joined Columns Into Rows Based on Primary Key 4 41
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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

20 Experts available now in Live!

Get 1:1 Help Now