problem with radio buttons! lazarus / 31-LCL ---------------------------------------------------------------------- speter · 2020-12-03 03:22 G'Day Folks, I have a problem with some radio buttons! The 21 radio buttons are in 2 panels; so I have an event handler for the OnClick event to handle setting .checked. But, when I click on a radio button, the event handler is called repeatedly. I could sort-of understand this happening with the OnChange event (but I'm not using that...). If anyone can work out what I've done wrong I'd appreciate it! The Project is in the attached zip; and (incase you don't want to bother with the zip) the code is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type  ta_type = (ta_none,             ta_naked_singles, ta_hidden_singles, ta_naked_pairs, ta_hidden_pairs,             ta_naked_triples, ta_hidden_triples, ta_pair_projection,             ta_triple_projection, ta_naked_quads, ta_hidden_quads,             ta_x_wing, ta_y_wing, ta_single_chains, ta_swordfish, ta_jellyfish,           ta_squirmbag, ta_medusa, ta_xyz_wing, ta_unique_rectangles,             ta_alt_inf_chains, ta_forcing_chains,             ta_finish);   { TForm1 }   TForm1 = class(TForm)    Memo1: TMemo;    Panel1: TPanel;      rb_naked_singles: TRadioButton;      rb_hidden_singles: TRadioButton;      rb_naked_pairs: TRadioButton;    rb_pair_projection: TRadioButton;      rb_x_wing: TRadioButton;    rb_hidden_pairs: TRadioButton;      rb_naked_triples: TRadioButton;    rb_hidden_triples: TRadioButton;      rb_triple_projection: TRadioButton;      rb_naked_quads: TRadioButton;      rb_hidden_quads: TRadioButton;     Panel2: TPanel;      rb_single_chains: TRadioButton;      rb_y_wing: TRadioButton;      rb_swordfish: TRadioButton;      rb_jellyfish: TRadioButton;      rb_squirmbag: TRadioButton;      rb_medusa: TRadioButton;      rb_xyz_wing: TRadioButton;      rb_unique_rectangles: TRadioButton;      rb_alt_inf_chains: TRadioButton;      rb_forcing_chains: TRadioButton;     procedure FormCreate(Sender: TObject);    procedure rbclick(Sender: TObject);  private   public    focus_techs : array [ta_naked_singles..ta_finish] of tradiobutton;   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  memo1.append('form create start');   focus_techs[ta_naked_singles]     := rb_naked_singles;  focus_techs[ta_hidden_singles]    := rb_hidden_singles;  focus_techs[ta_naked_pairs]       := rb_naked_pairs;  focus_techs[ta_pair_projection]   := rb_pair_projection;  focus_techs[ta_x_wing]            := rb_x_wing;  focus_techs[ta_hidden_pairs]      := rb_hidden_pairs;  focus_techs[ta_naked_triples]     := rb_naked_triples;  focus_techs[ta_hidden_triples]    := rb_hidden_triples;  focus_techs[ta_triple_projection] := rb_triple_projection;  focus_techs[ta_naked_quads]       := rb_naked_quads;  focus_techs[ta_hidden_quads]      := rb_hidden_quads;   focus_techs[ta_single_chains]     := rb_single_chains;  focus_techs[ta_y_wing]            := rb_y_wing;  focus_techs[ta_swordfish]         := rb_swordfish;  focus_techs[ta_jellyfish]         := rb_jellyfish;  focus_techs[ta_squirmbag]         := rb_squirmbag;  focus_techs[ta_medusa]            := rb_medusa;  focus_techs[ta_xyz_wing]          := rb_xyz_wing;  focus_techs[ta_unique_rectangles] := rb_unique_rectangles;  focus_techs[ta_alt_inf_chains]    := rb_alt_inf_chains;  focus_techs[ta_forcing_chains]    := rb_forcing_chains;  focus_techs[ta_finish]            := rb_naked_singles;     // not used   memo1.append('form create end');end; procedure TForm1.rbclick(Sender: TObject);var  ok : boolean;  a : ta_type;begin  memo1.append('rbclick start');   ok := true;  for a := ta_naked_singles to ta_finish do    if focus_techs[a] = nil then      begin        memo1.append('focus_techs['+ord(a).tostring+'] = nil');      ok := false;      end;  if ok then    memo1.append('focus_techs[] all OK');   for a := ta_naked_singles to ta_finish do    if focus_techs[a] <> nil then      if focus_techs[a].checked then        focus_techs[a].checked := false;   a := ta_type(tradiobutton(sender).tag);  if focus_techs[a] <> nil then    begin      focus_techs[a].checked := true;      memo1.append('setting rb['+ord(a).tostring+']');    end;     memo1.append('rbclick end');end; end. cheers S. ---------------------------------------------------------------------- speter · 2020-12-03 03:50 Interestingly, I *fixed* my problem; but I still don't know what was the cause... Replacing the guts on my onclick event handler (lines 115-118): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   for a := ta_naked_singles to ta_finish do    if focus_techs[a] <> nil then    if focus_techs[a].checked then        focus_techs[a].checked := false;with (rbtag : ta_type) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   rbtag := ta_type(tradiobutton(sender).tag);  for a := ta_naked_singles to ta_finish do    if (focus_techs[a] <> nil) and focus_techs[a].checked and (a <> rbtag) then      focus_techs[a].checked := false; sorts it out; but I'd still _really_ like to know what the problem is/was. cheers S. ---------------------------------------------------------------------- wp · 2020-12-03 09:47 Of course I cannot say for sure because you do not present compilable source. But I guess you see two OnChange events with every click on a radiobutton. Since radiobuttons are grouped exclusively in a panel only one of them can be checked. This means that when you click another radiobutton of this same group the previousely checked button becomes unchecked (--> first OnChange) and the newly click button becomes checked (--> 2nd OnChange). ---------------------------------------------------------------------- speter · 2020-12-03 22:36 Thanks for replying wp. Quote from: wp on December 03, 2020, 10:47:13 amOf course I cannot say for sure because you do not present compilable source. Is there a problem with the project zip included with my first post? Quote from: wp on December 03, 2020, 10:47:13 amBut I guess you see two OnChange events with every click on a radiobutton. Since radiobuttons are grouped exclusively in a panel only one of them can be checked. This means that when you click another radiobutton of this same group the previousely checked button becomes unchecked (--> first OnChange) and the newly click button becomes checked (--> 2nd OnChange). There are NO event handlers for OnChange (only OnClick). Do you mean that an OnChange event will trigger OnClick? cheers S. ---------------------------------------------------------------------- Sieben · 2020-12-03 23:08 Just downloaded your sample project - and there are no repeated calls for me here. But I wonder what's your click handler for anyway? When I remove it or insert an Exit as first line the behaviour is just the same: the clicked radiobutton gets checked while the previously checked automatically becomes unchecked. As wp already stated, all RadioButtons on a Panel are mutually exclusive. ---------------------------------------------------------------------- winni · 2020-12-03 23:34 Hi! Just tested your project1. * No repeatedly calls to the event handler * Correct behaviour of the two groups of radiobuttons Before we assume it is a widget set or OS problem: Is it a problem with an old bouncing mouse? My test was on Lin64, fpc3.04, La 2.0.10 For exact such kind of code  the RadioGroup was invented. the constant ta_str array moves to the Radiogroup.items of the then two Radiougroups. To simplify the code. Winni ---------------------------------------------------------------------- wp · 2020-12-03 23:35 Quote from: speter on December 03, 2020, 11:36:37 pmIs there a problem with the project zip included with my first post? Oh sorry, I did not see it... Quote from: wp on December 03, 2020, 10:47:13 amBut I guess you see two OnChange events with every click on a radiobutton. Since Do you mean that an OnChange event will trigger OnClick? Play with the attached demo. You see that changing the Checked property triggers an OnChange event (that's clear) plus an OnClick event even if this is done by code! That's one of the stupid faults that we inherit from Delphi. So, when your OnClick code sets the focus_techs[a].checked := true you will trigger another OnClick event, and your code may end in an endless loop (if the change already has not yet been executed). I see this because your program hangs for me (Win10) when I do the first click on a radiobutton. I do not understand what you want to achieve with this OnClick handler. Make sure that only one radiobutton of each panel is checked? This is not recessary because this feature is built-in: All radiobuttons of the same container are mutually interlocked. So, if you have two independent groups of radiobuttons you only must put them into different containers, panels as you do - that's correct. This way checking one button of the first group does not affect the checked states of the second group. There's no need of an OnClick handler to achieve this. ---------------------------------------------------------------------- winni · 2020-12-04 00:12 Hi! It seems you try to step slowly into fpc/Lazarus. To simplify your code I took your design but added two RadioGroups with your items. To show you how Lazarus can work. Winni ---------------------------------------------------------------------- speter · 2020-12-04 00:25 G'Day Folks, Many thanks to wp, winni & sieben, for checking this out. It seems wp found an issue ("your program hangs for me"); whereas winni & sieben found no problems. Firstly, my "logic" is that I want the radio buttons to behave as if they were all in the same panel. Since I have them in 2 panels (for layout reasons), I need to intervene. If the current checked radio button is in the second panel and the use clicks a radio button in the first panel, the one in the second panel is not un-checked ('cause it is not in panel1). That's why I added the OnClick event handler. Quote from: wp on December 04, 2020, 12:35:36 am... changing the Checked property triggers an OnChange event (that's clear) plus an OnClick event even if this is done by code! ...I knew this; but I did NOT know that Quote from: wp on December 04, 2020, 12:35:36 am... when your OnClick code sets the focus_techs[a].checked := true you will trigger another OnClick event, and your code may end in an endless loop ... Quote from: winni on December 04, 2020, 12:34:31 am... Before we assume it is a widget set or OS problem: Is it a problem with an old bouncing mouse? Sorry for not mentioning my os/laz ver before! I am using Windows 10; Lazarus 2.0.8 (FPC 3.0.4). I don't think my mouse is an issue. Wini & Sieben: can you please confirm that you were trying the initial project I included in my original post - without the corrections I mentioned in my second post!? Winni : I've been using Pascal since 1984 and Delphi since 1995; so I _do_ have some experience... cheers S. Edit: I just re-read wp's statment: Quote from: wp on December 04, 2020, 12:35:36 am... changing the Checked property triggers an OnChange event (that's clear) plus an OnClick event even if this is done by code! ...I (previously) thought wp wrote: clicking a radio-button triggers OnChange and OnClick; But wp actually wrote: changing .checked (by clicking or by code) triggers OnChange _and_ OnClick. Is this correct?? ---------------------------------------------------------------------- Sieben · 2020-12-04 00:36 QuoteWini & Sieben: can you please confirm that you were trying the initial project I included in my original post - without the corrections I mentioned in my second post!? Confirmed... QuoteIf the current checked radio button is in the second panel and the use clicks a radio button in the first panel, the one in the second panel is not un-checked ('cause it is not in panel1). That's why I added the OnClick event handler. That does not work here - there are always two RadioButtons checked, whether the handler is involved or not. An option would be to indeed use two RadioGroups, for you can set ItemIndex := -1 for the group that should not have a check. ---------------------------------------------------------------------- winni · 2020-12-04 00:39 Hi! Yes - I used your original code from project1. (What an imaginative  name. I've got hundreds of them. Not one from me). If you would have looked into my code you would have seen that I left your original code untouched. If ... Winni ---------------------------------------------------------------------- winni · 2020-12-04 00:44 Hi! Aaaah ... If you want only one item to be enabled then you should know since Delphi 1995: Throw all your stuff in one RadioGroup and you are shure that only one item is selected. If you want zero items selected then read  Siebens hint above. Winni ---------------------------------------------------------------------- Sieben · 2020-12-04 00:45 Regarding layout - TRadioGroup also has a columns property... ---------------------------------------------------------------------- winni · 2020-12-04 00:49 Quote from: Sieben on December 04, 2020, 01:45:10 amRegarding layout - TRadioGroup also has a columns property... For shure. But he knows that. He's doing Delphi since '95 Winni ---------------------------------------------------------------------- speter · 2020-12-04 01:02 Quote from: winni on December 04, 2020, 01:49:02 amQuote from: Sieben on December 04, 2020, 01:45:10 amRegarding layout - TRadioGroup also has a columns property... For shure. But he knows that. He's doing Delphi since '95 Winni Thanks Sieben & Winni. I didn't know about the columns property. Live and Learn. BTW, I checked wp's statement about the OnClick event being triggered by changing .checked and found it to be true (which I surprised me). cheers S. ---------------------------------------------------------------------- wp · 2020-12-04 09:34 If you don't want the TRadioGroup border and caption you can also get an automatic two-column layout in a panel by using the ChildSizing property. In fact, that's the way how TRadioGroup does the layout internally. Just set ChildSizing.Layout to cclLeftToRightThenTopToBottom and  .ControlsPerLine to 2. The distance between the columns is determined by .HorizontalSpace, left/right margin to the container border by .LeftRightSpacing. Additional control to positioning is given by Enlarge|ShrinkHorizontal|Vertical (too lazy to describe...). Important: the radiobuttons must not be aligned (Align = alNone), the can only have their standard anchors ([akLeft, akTop]). The order of the radiobuttons is determined by the order in which they were added; later rearrangement is possible by calling the "Z Order" context menu. If you finally set the AutoSize of the parent to true you have a very robust layout of the radiobuttons so that they will never overlap. ---------------------------------------------------------------------- speter · 2020-12-06 04:24 Quote from: wp on December 04, 2020, 10:34:19 amIf you don't want the TRadioGroup border and caption you can also get an automatic two- column layout in a panelThanks, but I am really liking the way the TRadioGroup simplifies my radio buttons (& checkboxes). Prior to the comments in this thread I had used Panels instead. The program I am working on had 21 radio buttons and 47 checkboxes; and now has 3 groups (each with 21 items) and 5 checkboxes; which is a huge improvement! cheers S. ---------------------------------------------------------------------- jamie · 2020-12-06 13:58 I wouldn't want to be the person that has to read all of those check boxes and radio buttons... I envision a change in strategy soon ---------------------------------------------------------------------- speter · 2020-12-06 22:35 Quote from: jamie on December 06, 2020, 02:58:10 pmI wouldn't want to be the person that has to read all of those check boxes and radio buttons... I envision a change in strategy soon  They are in 4 tabs and the 3 x 21 all have the same captions; so it is not as bad as it sounds. It allows me to change the (sudoku solving techniques) being used. cheers S. ---------------------------------------------------------------------- winni · 2020-12-06 22:43 Hi! I think if you want to code some Sudoko stuff you should use a TStringGrid. In the FixedRows and the FixedCold you can write your caption stuff. And in the other fields you can let the computer play Sudoku. Winni