Recent

Author Topic: problem with radio buttons!  (Read 1209 times)

speter

  • Full Member
  • ***
  • Posts: 104
problem with radio buttons!
« on: December 03, 2020, 04:22:27 am »
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.   ta_type = (ta_none,
  12.              ta_naked_singles, ta_hidden_singles, ta_naked_pairs, ta_hidden_pairs,
  13.              ta_naked_triples, ta_hidden_triples, ta_pair_projection,
  14.              ta_triple_projection, ta_naked_quads, ta_hidden_quads,
  15.              ta_x_wing, ta_y_wing, ta_single_chains, ta_swordfish, ta_jellyfish,
  16.              ta_squirmbag, ta_medusa, ta_xyz_wing, ta_unique_rectangles,
  17.              ta_alt_inf_chains, ta_forcing_chains,
  18.              ta_finish);
  19.  
  20.   { TForm1 }
  21.  
  22.   TForm1 = class(TForm)
  23.     Memo1: TMemo;
  24.     Panel1: TPanel;
  25.       rb_naked_singles: TRadioButton;
  26.       rb_hidden_singles: TRadioButton;
  27.       rb_naked_pairs: TRadioButton;
  28.       rb_pair_projection: TRadioButton;
  29.       rb_x_wing: TRadioButton;
  30.       rb_hidden_pairs: TRadioButton;
  31.       rb_naked_triples: TRadioButton;
  32.       rb_hidden_triples: TRadioButton;
  33.       rb_triple_projection: TRadioButton;
  34.       rb_naked_quads: TRadioButton;
  35.       rb_hidden_quads: TRadioButton;
  36.  
  37.     Panel2: TPanel;
  38.       rb_single_chains: TRadioButton;
  39.       rb_y_wing: TRadioButton;
  40.       rb_swordfish: TRadioButton;
  41.       rb_jellyfish: TRadioButton;
  42.       rb_squirmbag: TRadioButton;
  43.       rb_medusa: TRadioButton;
  44.       rb_xyz_wing: TRadioButton;
  45.       rb_unique_rectangles: TRadioButton;
  46.       rb_alt_inf_chains: TRadioButton;
  47.       rb_forcing_chains: TRadioButton;
  48.  
  49.     procedure FormCreate(Sender: TObject);
  50.     procedure rbclick(Sender: TObject);
  51.   private
  52.  
  53.   public
  54.     focus_techs : array [ta_naked_singles..ta_finish] of tradiobutton;
  55.  
  56.   end;
  57.  
  58. var
  59.   Form1: TForm1;
  60.  
  61. implementation
  62.  
  63. {$R *.lfm}
  64.  
  65. { TForm1 }
  66.  
  67. procedure TForm1.FormCreate(Sender: TObject);
  68. begin
  69.   memo1.append('form create start');
  70.  
  71.   focus_techs[ta_naked_singles]     := rb_naked_singles;
  72.   focus_techs[ta_hidden_singles]    := rb_hidden_singles;
  73.   focus_techs[ta_naked_pairs]       := rb_naked_pairs;
  74.   focus_techs[ta_pair_projection]   := rb_pair_projection;
  75.   focus_techs[ta_x_wing]            := rb_x_wing;
  76.   focus_techs[ta_hidden_pairs]      := rb_hidden_pairs;
  77.   focus_techs[ta_naked_triples]     := rb_naked_triples;
  78.   focus_techs[ta_hidden_triples]    := rb_hidden_triples;
  79.   focus_techs[ta_triple_projection] := rb_triple_projection;
  80.   focus_techs[ta_naked_quads]       := rb_naked_quads;
  81.   focus_techs[ta_hidden_quads]      := rb_hidden_quads;
  82.  
  83.   focus_techs[ta_single_chains]     := rb_single_chains;
  84.   focus_techs[ta_y_wing]            := rb_y_wing;
  85.   focus_techs[ta_swordfish]         := rb_swordfish;
  86.   focus_techs[ta_jellyfish]         := rb_jellyfish;
  87.   focus_techs[ta_squirmbag]         := rb_squirmbag;
  88.   focus_techs[ta_medusa]            := rb_medusa;
  89.   focus_techs[ta_xyz_wing]          := rb_xyz_wing;
  90.   focus_techs[ta_unique_rectangles] := rb_unique_rectangles;
  91.   focus_techs[ta_alt_inf_chains]    := rb_alt_inf_chains;
  92.   focus_techs[ta_forcing_chains]    := rb_forcing_chains;
  93.   focus_techs[ta_finish]            := rb_naked_singles;     // not used
  94.  
  95.   memo1.append('form create end');
  96. end;
  97.  
  98. procedure TForm1.rbclick(Sender: TObject);
  99. var
  100.   ok : boolean;
  101.   a : ta_type;
  102. begin
  103.   memo1.append('rbclick start');
  104.  
  105.   ok := true;
  106.   for a := ta_naked_singles to ta_finish do
  107.     if focus_techs[a] = nil then
  108.       begin
  109.         memo1.append('focus_techs['+ord(a).tostring+'] = nil');
  110.         ok := false;
  111.       end;
  112.   if ok then
  113.     memo1.append('focus_techs[] all OK');
  114.  
  115.   for a := ta_naked_singles to ta_finish do
  116.     if focus_techs[a] <> nil then
  117.       if focus_techs[a].checked then
  118.         focus_techs[a].checked := false;
  119.  
  120.   a := ta_type(tradiobutton(sender).tag);
  121.   if focus_techs[a] <> nil then
  122.     begin
  123.       focus_techs[a].checked := true;
  124.       memo1.append('setting rb['+ord(a).tostring+']');
  125.     end;
  126.  
  127.     memo1.append('rbclick end');
  128. end;
  129.  
  130. end.

cheers
S. :)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.8 / FPC 3.0.4 / Windows 10 (64bit)

speter

  • Full Member
  • ***
  • Posts: 104
Re: problem with radio buttons!
« Reply #1 on: December 03, 2020, 04:50:19 am »
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][+][-]
  1.   for a := ta_naked_singles to ta_finish do
  2.     if focus_techs[a] <> nil then
  3.       if focus_techs[a].checked then
  4.         focus_techs[a].checked := false;
with (rbtag : ta_type)
Code: Pascal  [Select][+][-]
  1.   rbtag := ta_type(tradiobutton(sender).tag);
  2.   for a := ta_naked_singles to ta_finish do
  3.     if (focus_techs[a] <> nil) and focus_techs[a].checked and (a <> rbtag) then
  4.       focus_techs[a].checked := false;

sorts it out; but I'd still _really_ like to know what the problem is/was. :)

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.8 / FPC 3.0.4 / Windows 10 (64bit)

wp

  • Hero Member
  • *****
  • Posts: 7966
Re: problem with radio buttons!
« Reply #2 on: December 03, 2020, 10:47:13 am »
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).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

speter

  • Full Member
  • ***
  • Posts: 104
Re: problem with radio buttons!
« Reply #3 on: December 03, 2020, 11:36:37 pm »
Thanks for replying wp.

Of 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?


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).

There are NO event handlers for OnChange (only OnClick).
Do you mean that an OnChange event will trigger OnClick?

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.8 / FPC 3.0.4 / Windows 10 (64bit)

Sieben

  • Full Member
  • ***
  • Posts: 196
Re: problem with radio buttons!
« Reply #4 on: December 04, 2020, 12:08:32 am »
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.
« Last Edit: December 04, 2020, 12:30:21 am by Sieben »
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

winni

  • Hero Member
  • *****
  • Posts: 2122
Re: problem with radio buttons!
« Reply #5 on: December 04, 2020, 12:34:31 am »
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

  • Hero Member
  • *****
  • Posts: 7966
Re: problem with radio buttons!
« Reply #6 on: December 04, 2020, 12:35:36 am »
Is there a problem with the project zip included with my first post?
Oh sorry, I did not see it...

But 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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

winni

  • Hero Member
  • *****
  • Posts: 2122
Re: problem with radio buttons!
« Reply #7 on: December 04, 2020, 01:12:02 am »
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

  • Full Member
  • ***
  • Posts: 104
Re: problem with radio buttons!
« Reply #8 on: December 04, 2020, 01:25:18 am »
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.

... 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

... 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 ...

... 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:
... 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??
« Last Edit: December 04, 2020, 01:36:39 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.8 / FPC 3.0.4 / Windows 10 (64bit)

Sieben

  • Full Member
  • ***
  • Posts: 196
Re: problem with radio buttons!
« Reply #9 on: December 04, 2020, 01:36:16 am »
Quote
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!?

Confirmed...

Quote
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.

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.
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

winni

  • Hero Member
  • *****
  • Posts: 2122
Re: problem with radio buttons!
« Reply #10 on: December 04, 2020, 01:39:10 am »
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

  • Hero Member
  • *****
  • Posts: 2122
Re: problem with radio buttons!
« Reply #11 on: December 04, 2020, 01:44:24 am »
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

  • Full Member
  • ***
  • Posts: 196
Re: problem with radio buttons!
« Reply #12 on: December 04, 2020, 01:45:10 am »
Regarding layout - TRadioGroup also has a columns property...
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

winni

  • Hero Member
  • *****
  • Posts: 2122
Re: problem with radio buttons!
« Reply #13 on: December 04, 2020, 01:49:02 am »
Regarding layout - TRadioGroup also has a columns property...

For shure.
But he knows that.
He's doing Delphi since '95

Winni

speter

  • Full Member
  • ***
  • Posts: 104
Re: problem with radio buttons!
« Reply #14 on: December 04, 2020, 02:02:23 am »
Regarding 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.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.8 / FPC 3.0.4 / Windows 10 (64bit)

 

TinyPortal © 2005-2018