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

x
?
Solved

how to make controls readonly on a form

Posted on 2002-03-21
14
Medium Priority
?
196 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 240 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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