Recent

Author Topic: Problem with Runtime created RadioGroup properties not working...  (Read 4734 times)

local-vision

  • Jr. Member
  • **
  • Posts: 77
Problem with RadioGroup properties not working in runtime created RadioGroup

Create a Radiogroup at run time on Form1. The run time created Radiogroup works just dandy.

When you set properties to hide or disable items like:

Code: Pascal  [Select][+][-]
  1. Rg0.Controls[3].Visible:=False;   // works correctly
  2. Rg0.Controls[4].Enabled:=False; // works correctly

Works fine :)

But.....

Do the same when creating the RadioGroup on a runtime created form and the Visible property no longer works??

Code: Pascal  [Select][+][-]
  1. Rg1.Controls[3].Visible:=False;   // NO longer works correctly??
  2. Rg1.Controls[4].Enabled:=False; // works correctly

Have included the project here as a zipped file.

Update: Instead of a runtime created child Form, now tested using a design time child Form. Same problem. When you use the TRadioGroup on any additional child forms the Visible property fails to work correctly.

« Last Edit: August 22, 2024, 06:32:52 pm by local-vision »

wp

  • Hero Member
  • *****
  • Posts: 12368
Re: Problem with Runtime created RadioGroup properties not working...
« Reply #1 on: August 22, 2024, 06:33:35 pm »
I assume that in your code the radiobutton state change in the modal form is too early. The same happens with the main form when I move your FormShow code into FormCreate.

However, when I add an OnShow event for the modal form and put the State changes there, the Rb1.Controls[3] does disappear.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FrmShow(Sender: TObject);
  2. begin
  3.   Rg1.Controls[0].Enabled:=True;
  4.   Rg1.Controls[1].Enabled:=True;
  5.   Rg1.Controls[2].Enabled:=False;
  6.   Rg1.Controls[3].Visible:=False;
  7.   Rg1.Controls[4].Enabled:=True;
  8. end;
  9.  
  10. procedure TForm1.Button1Click(Sender:TObject);
  11. begin
  12.   Frm:=TForm.Create(Form1);
  13.   ...
  14.   Frm.OnShow := @FrmShow;
  15.   ...

Not sure if this is a bug or an unavoidable consequence of the way the LCL works...
« Last Edit: August 22, 2024, 09:30:28 pm by wp »

Hansvb

  • Hero Member
  • *****
  • Posts: 710
Re: Problem with Runtime created RadioGroup properties not working...
« Reply #2 on: August 22, 2024, 07:26:09 pm »

Building on WP's example. It looks like you have to get the onshow ready and then when show modal does its thing formshow at a better time. Poor explanation, I don't have enough knowledge to explain it right, but see below, this works.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormShow(Sender: TObject);
  16.   private
  17.     procedure DoShowFrm(sender: TObject);
  18.   public
  19.  
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. var Frm:TForm;
  32.     Rg0,Rg1:TRadioGroup;
  33.  
  34. procedure TForm1.FormShow(Sender: TObject);
  35. begin
  36.   Rg0:=TRadioGroup.Create(Form1);
  37.   Rg0.Parent:=Form1;
  38.   Rg0.AutoSize:=True;
  39.  
  40.   Rg0.Caption:='The Main Form Group';
  41.  
  42.   Rg0.Items.Add('First');
  43.   Rg0.Items.Add('Second');
  44.   Rg0.Items.Add('Third');
  45.   Rg0.Items.Add('Forth');
  46.   Rg0.Items.Add('Fifth');
  47.  
  48.   Rg0.Controls[0].Enabled:=True;
  49.   Rg0.Controls[1].Enabled:=True;
  50.   Rg0.Controls[2].Enabled:=False;
  51.   Rg0.Controls[3].Visible:=False;   // works correctly
  52.   Rg0.Controls[4].Enabled:=True;
  53.  
  54.   /////////////////////
  55.  
  56.   Frm:=TForm.Create(Form1);
  57.   Frm.Width:=480;
  58.   Frm.Height:=320;
  59.   Frm.Position:=poMainFormCenter;
  60.   Frm.BorderStyle:=bsDialog;
  61.  
  62.   Frm.OnShow:= @DoShowFrm; //<<<<--- now it works (do not know why)
  63.   Frm.ShowModal;
  64.   FreeAndNil(Frm);
  65. end;
  66.  
  67. procedure TForm1.DoShowFrm(sender: TObject);
  68. begin
  69.   Rg1:=TRadioGroup.Create(Frm);
  70.   Rg1.Parent:=Frm;
  71.   Rg1.AutoSize:=True;
  72.  
  73.   Rg1.Caption:='The Created Form Group';
  74.  
  75.   Rg1.Items.Add('First_');
  76.   Rg1.Items.Add('Second_');
  77.   Rg1.Items.Add('Third_');
  78.   Rg1.Items.Add('Forth_');
  79.   Rg1.Items.Add('Fifth_');
  80.  
  81.   Rg1.Controls[0].Enabled:=True;
  82.   Rg1.Controls[1].Enabled:=True;
  83.   Rg1.Controls[2].Enabled:=False;
  84.   Rg1.Controls[3].Visible:=False;   // still shown, refuses to work correctly
  85.   Rg1.Controls[4].Enabled:=True;
  86. end;
  87.  
  88. end.    
  89.  

local-vision

  • Jr. Member
  • **
  • Posts: 77
Re: Problem with Runtime created RadioGroup properties not working...
« Reply #3 on: August 22, 2024, 07:41:30 pm »
Amazing.

Indeed, not sure why it works in that way. I now at least have something to work with. Greatly valued the examples.

Many Thanks to WP and Hansvb :)

For others that may bump into this issue I am including the revised project here as attachment.

wp

  • Hero Member
  • *****
  • Posts: 12368
Re: Problem with Runtime created RadioGroup properties not working...
« Reply #4 on: August 22, 2024, 09:52:21 pm »
The issue can be reproduced in a simpler project with a single form and even with designtime-created radiobuttons: Just add something like
Code: Pascal  [Select][+][-]
  1.   RadioGroup1.Controls[1].Visible := false;
to the OnCreate event of the form. Running this project, the second radiobutton will be visible although it should be hidden. Check out attached project demonstrating this.

I think I found the guilty code: In radiogroup.inc (in folder lcl/include of the Lazarus installation), there is a procedure TCustomRadioGroup.UpdateItems which updates the radiobuttons on several occasions. A bit down this code there is a loop running over all radiobuttons containing the line:
Code: Pascal  [Select][+][-]
  1. ARadioButton.Visible := true;
which overwrites the Visible setting.

Commenting this line out, fixes the issue.

Please test this modification, and try to see whether there are any regressions. If all works fine, I'll write a bug report and apply this fix.

local-vision

  • Jr. Member
  • **
  • Posts: 77
Re: Problem with Runtime created RadioGroup properties not working...
« Reply #5 on: August 22, 2024, 11:37:43 pm »
That is some excellent investigation WP!

Reviewed radiogroup.inc and found the ARadioButton.Visible := true;

Commented it out and saved the file.

Checked the project and now all works as it should.

I can change the Visible item property most anywhere now.

As far as I can see all seems to work fine. I will be doing some more testing in the days to come.

Any issues and I will update them here.

Much Appreciation.

Note: Tested your sample project radiogroupbutton_visible.zip. No issues. worked fine.

Using Lazarus 3.0 on Linux Mint 21.1 Cin



 
« Last Edit: August 23, 2024, 08:55:13 am by local-vision »

local-vision

  • Jr. Member
  • **
  • Posts: 77
Re: Problem with Runtime created RadioGroup properties not working...
« Reply #6 on: August 23, 2024, 12:42:37 pm »
Have No idea if related....

After modifying the file I now get IDE issues.

Messages indicate that it can't find certain units leading to the inability to create methods such as double-click events.

My process of commenting out the section in RadioGroup.inc was to: open the file with root privileges, make the changes and then save the file.


wp

  • Hero Member
  • *****
  • Posts: 12368
Re: Problem with Runtime created RadioGroup properties not working...
« Reply #7 on: August 23, 2024, 03:45:04 pm »
After modification of LCL files it is usually required to rebuild to IDE: "Tools" > "Configure Build Lazarus" > check "Clean all" and "Switch after building to automatically" > "Build". When the IDE restarts after a while the issue should be gone (if not delete the "lib" directory in your project folder).

Filed a bug report: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41091
« Last Edit: August 23, 2024, 04:44:04 pm by wp »

local-vision

  • Jr. Member
  • **
  • Posts: 77
Re: Problem with Runtime created RadioGroup properties not working...
« Reply #8 on: August 23, 2024, 08:08:38 pm »
Indeed. Had eventually attempted to rebuild but that failed. I did not Clean All first.

Will follow your instruction. Appreciate the step through.

Reviewed the Bug report. All looks good and thanks again for the input. :)

 

TinyPortal © 2005-2018