Recent

Author Topic: Loading Dynamic Radio Group gives Extra Button  (Read 2492 times)

Badger

  • Full Member
  • ***
  • Posts: 144
Loading Dynamic Radio Group gives Extra Button
« on: March 26, 2019, 10:31:14 pm »
I'm migrating a large Project from Delphi and  I'm having trouble with the following Code - mode for the unit is {$mode objfpc}{$H+}

Code: Pascal  [Select][+][-]
  1. procedure TSelectCatForm.FormActivate(Sender: TObject);
  2. var
  3.  
  4.   x,y:integer;
  5.   dummy:String;
  6. begin
  7.   RadioGroup1.ItemIndex:=-1;;
  8.   RadioGroup1.Items.Clear;//get rid of old radio buttons
  9.   SetLength(Buttons,0);
  10.  
  11.   for x:=0 to Length(Matrix)-1 do
  12.   begin
  13.     Setlength(Buttons,x+1);
  14.     Buttons[x]:=TRadioButton.Create(Self);
  15.     Buttons[x].Parent:=RadioGroup1;
  16.     Buttons[x].Left:=30;
  17.     Buttons[x].Top:= 30+(x*30);
  18.     Buttons[x].OnClick:=@RadButClick;
  19.     dummy:='';
  20.     y:=1;
  21.     While Matrix[x,0][y]<>'|' do  //Get Category from Matrix
  22.     begin
  23.       dummy:=dummy+Matrix[x,0][y];
  24.       inc(y);
  25.     end;
  26.     RadioGroup1.Items.AddObject( dummy, Buttons[x] );
  27.    
  28.   end;
  29.  
  30.   //Set Up Form
  31.   RadioGroup1.Height:=(Length(Matrix)+1)*30;
  32.   Go.Top:=RadioGroup1.Top+RadioGroup1.Height+30;
  33.   Go.Enabled:=False;
  34.   Cancel.Top:=RadioGroup1.Top+RadioGroup1.Height+30;
  35.   SelectCatForm.Height:=Go.Top+90;
  36.   RadioGroup1.ItemIndex:=-1;
  37.  
  38. end;

In this trial run there is only one category in the Matrix.
Although the x loop only runs once, two buttons appear in the RadioGroup.  The first has no text and the second has the expected text.
If the form is called a second time there are two blank buttons then a third button with the correct text. Call it again and there are four.

Is there a problem with the Lazarus component or am I doing something wrong - I strongly suspect the latter!!
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Loading Dynamic Radio Group gives Extra Button
« Reply #1 on: March 26, 2019, 10:51:04 pm »
possibly a windows timing message error..

insert a line after the CLEAR statement of this

Application.ProcessMessages;

I tested this over here using a  couple of buttons to clear and populate the Group, it seems to work ok.
but the group is a Windows control and may need to have the message handlers updated..

Something to try..
The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Loading Dynamic Radio Group gives Extra Button
« Reply #2 on: March 26, 2019, 11:47:01 pm »
A TRadioGroup is a self-contained control that manages its radiobuttons internally, whose number depends only on its Items.Count property.

You are trying to interfere with that internal button creation and management by adding radiobuttons to the control that you create manually, which won't work as you expect. Your Buttons array is completely unnecessary, and the wrong approach altogether. Remove all reference to Buttons.

Stick to using RadioGroup1.Items.AddObject() to add buttons to the control, and RadioGroup1.Items.Clear to remove them.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Loading Dynamic Radio Group gives Extra Button
« Reply #3 on: March 27, 2019, 12:58:57 am »
Stick to using RadioGroup1.Items.AddObject() to add buttons to the control, and RadioGroup1.Items.Clear to remove them.

In fact, you should not even be using Items.AddObject(), use Items.Add() instead.

Try this:

Code: Pascal  [Select][+][-]
  1. procedure TSelectCatForm.FormActivate(Sender: TObject);
  2. var
  3.   x, y: integer;
  4. begin
  5.   RadioGroup1.ItemIndex := -1;
  6.  
  7.   RadioGroup1.Items.BeginUpdate;
  8.   try
  9.     RadioGroup1.Items.Clear; //get rid of old radio buttons
  10.  
  11.     for x := 0 to Length(Matrix)-1 do
  12.     begin
  13.       y := Pos('|', Matrix[x,0]);
  14.       RadioGroup1.Items.Add( Copy(Matrix[x,0], 1, y-1) );
  15.     end;
  16.   finally
  17.     RadioGroup1.Items.EndUpdate;
  18.   end;
  19.  
  20.   //Set Up Form
  21.   ...
  22. end;
  23.  
« Last Edit: March 27, 2019, 01:05:10 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Loading Dynamic Radio Group gives Extra Button
« Reply #4 on: March 27, 2019, 01:18:56 am »
Thanks Folks
After I had posted my request I went back and did some more investigating and found that the Embarcadero forums recommended using the create button method but the Lazarus didn't so I changed it and it worked.

One other small question,I'm not sure it warrants a separate  thread.  I have a string grid on another form and when I look at my ToDo list there are a number of Todo items such as 'check range', 'fix rows too' and 'cell coords of last time editor was visible' connected to Unit Grids.  Do I need to address these? - ie do the grids work OK without doing anything? 
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Loading Dynamic Radio Group gives Extra Button
« Reply #5 on: March 27, 2019, 01:55:03 am »
After I had posted my request I went back and did some more investigating and found that the Embarcadero forums recommended using the create button method but the Lazarus didn't so I changed it and it worked.

The way described above is how Embarcadero's TRadioGroup works, too.  You SHOULD NOT create TRadioButton objects in a TRadioGroup manually, unless you want to take direct control over them (control positioning, assign tags, etc), but even then, there are ways to do that without interferring with how TRadioGroup creates its own TRadioButton objects.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018