Jump to content
Lainkes

Set form in read only mode

Recommended Posts

Hello,

 

If a record is closed, I need to make the form with all the info read only.

Is this possible in one commend?

Or do I have to disable all the components one by one?

 

Thanks

 

Alain

Share this post


Link to post

A more details could help. One solution can be to place a panel onto the form where you place your UI controls. If the record is lock set the enabled proprty of the panel to false.

Share this post


Link to post

OK, thanks this is what I was looking for.

But one little problem.

I have a PageControl. And when I disable the panel, I cannot switch between the different pages of the Pagecontrol.

 

Share this post


Link to post

In that case you should use the Enabled property of each TabSheet.

Share this post


Link to post

I also thought that.

But once you disable the panel, nothing can be changed.

 

Share this post


Link to post

Set the DB to read only in viewing mode to prevent inadvertent record changes.  Have the user log in when updating a record.   

Share this post


Link to post
33 minutes ago, Lainkes said:

But once you disable the panel, nothing can be changed.

Well, you have to enable all parent controls of the PageControl to let the user change tabs.

 

In most of the cases disabling a whole form is not what you actually need. Often it boils down to disable individual components unless you can group some with TPanel or TTabSheet. There is no common approach to that as it usually depends on your UI design - which we cannot see.

Share this post


Link to post
Posted (edited)

Another thing to consider is the visual aspect.

 

When you disable a single control it may have a change in its visual representation as is the case with a button or an edit box that goes gray/grey and for good reason because that gives an indication to the user that it has been disabled. If you want that level of indication then you will have to disable each visually relevant control by itself. Disabling a parent container control does not affect the child controls visually.

 

Disabling a few controls are easy and just a few lines but if you have many then you can loop through them and disable them that way. Have a look at the Controls and ControlCount properties of the parent containers (Form, Panel, TabSheet, GroupBox).

 

 

Edited by PeaShooter_OMO

Share this post


Link to post

Sometimes it is useful to make a list of these components.

Share this post


Link to post

I managed to loop with ControlCount.

 

First I disable all controls on my panel.

Then I enable my PageControl.

And as last I loop all mu controls on my PageControl and set them disabled.

            // SET ALL COMPONENTS TO READONLY
              // Disable first all components
              for I := 0 to RzPanel1.ControlCount - 1 do
                RzPanel1.Controls[I].Enabled := False;
              // Enable TabSheets
              PageControl1.Enabled := True;
              // Disable components in PageControl
              for I := 0 to PageControl1.ControlCount - 1 do
                PageControl1.Controls[I].Enabled := False;

Thanks for the help and hints.

 

Alain

Share this post


Link to post
15 hours ago, Lainkes said:

I managed to loop with ControlCount.

 

First I disable all controls on my panel.

Then I enable my PageControl.

And as last I loop all mu controls on my PageControl and set them disabled.


            // SET ALL COMPONENTS TO READONLY
              // Disable first all components
              for I := 0 to RzPanel1.ControlCount - 1 do
                RzPanel1.Controls[I].Enabled := False;
              // Enable TabSheets
              PageControl1.Enabled := True;
              // Disable components in PageControl
              for I := 0 to PageControl1.ControlCount - 1 do
                PageControl1.Controls[I].Enabled := False;

Thanks for the help and hints.

 

Alain

Use GetControls() new since Delphi 11 I believe. Just as an example for me to disable TcxEdit's only. But you can do your own stuff:

for var ctrl in frmMain.GetControls([ceftAll]) do
    begin
      if (ctrl is TcxEdit) then
      begin
        TcxEdit( ctrl).Enabled := false;
      end;
    end;

 

  • Thanks 1

Share this post


Link to post
Posted (edited)

Sorry, you're quite confused here.

 

On the TPageControl / TRzPageControl, the "pages" are TTabSheet types. They are NOT TPanel types.

 

That's why TTabSheet.Enabled turns the tabs on and off -- it's to hide/show the TABSHEET (including its TAB). Not what's on it.

 

You can drop a TPanel / TRzPanel on a tabsheet and then set it's Enabled property to get the desired effect without all the other rigamarole.

 

Be sure to set the panel's Alignment to Client so it fill the entire tabsheet.

 

Iterating over a TPageControl / TRzPageControl lets you access the Pages array property, which is an array of TTabSheet. 

 

I don't think that's what you want.

 

 

From a more global perspective, there are typically two or three different forms I recommend for handling CRUD actions on DB records: one is for entering NEW record data; one is for VIEWING / EDITING an existing record's data; and a third one is to confirm DELETING a record. I won't go into detail here as to why, but after you've dealt with this a few hundred times, you'll understand why. Yes, you can only use ONE form, but the amount of logic needed to handle all three cases can get crazy. 

 

(If you just need to work with one single record, then you can do all of the CRUD stuff on one form. But when you introduce computed fields, lookup fields, child records listed on the form, and other common stuff, you will quickly find yourself going nuts trying to get everything to work on one single form for all possible cases. For me, it's easier to just make two or three distinct forms. The Delete can be a simple confirmation dialog. And record viewing / retrieval can be done with the Edit form, just make the fields read-only for viewing and enable them for editing when the user enables Edit mode.)

 

 

 

Edited by David Schwartz

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×