Recent

Author Topic: Problem with CellEdit.ReadOnly property  (Read 1523 times)

kjteng

  • Full Member
  • ***
  • Posts: 194
Problem with CellEdit.ReadOnly property
« on: May 28, 2021, 01:22:16 pm »
I set the CellEdit1.readonly property to True to prevent user from changing the cell content.
However, the moment when the user exit the CellEdit component (eg. click on other component), the CellEdit.ReadOnly property automatically changed to False.
attached sample project.
Please help.
« Last Edit: May 28, 2021, 02:19:21 pm by kjteng »

winni

  • Hero Member
  • *****
  • Posts: 2665
Re: Problem with CellEdit.ReadOnly property
« Reply #1 on: May 28, 2021, 02:10:19 pm »
Hi!

unit2.pas is missing in the zip file.

Winni

kjteng

  • Full Member
  • ***
  • Posts: 194
Re: Problem with CellEdit.ReadOnly property
« Reply #2 on: May 28, 2021, 02:19:58 pm »
Hi!

unit2.pas is missing in the zip file.

Winni

Sorry, my mistake. Reloaded test2.zip

winni

  • Hero Member
  • *****
  • Posts: 2665
Re: Problem with CellEdit.ReadOnly property
« Reply #3 on: May 28, 2021, 02:45:28 pm »
Error:

project1.lpr could not be read

Winni

wp

  • Hero Member
  • *****
  • Posts: 8749
Re: Problem with CellEdit.ReadOnly property
« Reply #4 on: May 28, 2021, 02:47:21 pm »
You must also consider the protection state of the worksheet, there is a method worksheet.Protect(boolvar) with which you can disable editing of the entire worksheet (https://wiki.lazarus.freepascal.org/FPSpreadsheet#Protection). Unfortunately the controls do not cooperate with the worksheet here very well, so you must also protect the visual controls.

This code works for me in your project:
Code: Pascal  [Select][+][-]
  1. procedure TForm2.CheckBox1Change(Sender: TObject);
  2. begin
  3.   if CheckBox1.Checked then
  4.     begin
  5.       gr1.Options := gr1.Options  - [goEditing];
  6.       label1.Caption := '<- Readonly';
  7.     end
  8.   else
  9.     begin
  10.       gr1.Options := gr1.Options  + [goEditing];
  11.       label1.Caption := '<- Editable';
  12.     end;
  13.   sCellEdit1.ReadOnly := CheckBox1.Checked;
  14.   wbs1.Worksheet.Protect(Checkbox1.Checked);
  15. end;
« Last Edit: May 28, 2021, 02:50:28 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

kjteng

  • Full Member
  • ***
  • Posts: 194
Re: Problem with CellEdit.ReadOnly property
« Reply #5 on: May 28, 2021, 03:13:08 pm »
You must also consider the protection state of the worksheet, there is a method worksheet.Protect(boolvar) with which you can disable editing of the entire worksheet (https://wiki.lazarus.freepascal.org/FPSpreadsheet#Protection). Unfortunately the controls do not cooperate with the worksheet here very well, so you must also protect the visual controls.

This code works for me in your project:
Code: Pascal  [Select][+][-]
  1. procedure TForm2.CheckBox1Change(Sender: TObject);
  2. begin
  3.   if CheckBox1.Checked then
  4.     begin
  5.       gr1.Options := gr1.Options  - [goEditing];
  6.       label1.Caption := '<- Readonly';
  7.     end
  8.   else
  9.     begin
  10.       gr1.Options := gr1.Options  + [goEditing];
  11.       label1.Caption := '<- Editable';
  12.     end;
  13.   sCellEdit1.ReadOnly := CheckBox1.Checked;
  14.   wbs1.Worksheet.Protect(Checkbox1.Checked);
  15. end;

Thank to master for your suggestion.  It means there is no way to change this behavior (automatically change of Readonly property)  in the component?  BTW, currently I am using the following code to workaround this problem but not sure whether there will be 'side effect' :

procedure TForm2.sCellEdit1Enter(Sender: TObject);
begin
  sCellEdit1.ReadOnly := Checkbox1.Checked;
end;

wp

  • Hero Member
  • *****
  • Posts: 8749
Re: Problem with CellEdit.ReadOnly property
« Reply #6 on: May 28, 2021, 03:56:30 pm »
The problem is that when I take the CellEdit's ReadOnly seriously there may be a conflict with the grid which maybe can be allowed for editing. And what if only one particular cell would allowed for editing which for all the others it would be disallowed?

This is a very confusing situation (ok - the current situation is even more confusing...).

The readonly properties are determined by the state the worksheet protection as described in the wiki. Look at attached demo file. Here, the entire worksheet is blocked from editing except for the yellow one, you can verify this with spready or the demo_ctrls sample project. So, the basic idea is to leave the ReadOnly property alone and to define protection using the properties of the worksheet from which the visual controls get their data and their behaviour.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018