Solved

how to make controls readonly on a form

Posted on 2002-03-21
14
188 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
[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
  • 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
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 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

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!

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…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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