Solved

how to make controls readonly on a form

Posted on 2002-03-21
14
172 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
Comment Utility
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
Comment Utility
use form.components instead

0
 
LVL 17

Accepted Solution

by:
geobul earned 60 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:rajniyadav
Comment Utility
Thanx a lot
0
 
LVL 4

Expert Comment

by:jsweby
Comment Utility
That looks very similar to my suggestion...

J.
0
 

Author Comment

by:rajniyadav
Comment Utility
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
Comment Utility
jsweby

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

Expert Comment

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

J.
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
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
Comment Utility
ya i did that
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi 10 Seattle Dataset Actions 5 71
PDF library for Delphi 2 89
delphi exception 7 58
how can i search if string exist in array ? 3 26
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

11 Experts available now in Live!

Get 1:1 Help Now